From a7e77ed1bfb7c0d073d2378beeb46ac295628d66 Mon Sep 17 00:00:00 2001 From: sp Date: Thu, 1 Apr 2021 06:01:09 +1100 Subject: [PATCH] list of peole works, user enabled move to people --- apiV1Avatar.go | 3 +++ apiV1BrokerList.go | 2 -- apiV1PeopleList.go | 4 +--- apiV1User.go | 37 +++++++++++++++++++++++++++++++++---- apiV1login.go | 3 --- apiv1.go | 2 ++ 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/apiV1Avatar.go b/apiV1Avatar.go index 71fe3a1..71c9e1e 100644 --- a/apiV1Avatar.go +++ b/apiV1Avatar.go @@ -70,6 +70,9 @@ func apiV1Avatar(w http.ResponseWriter, r *http.Request, ss *loan.Session) { avatar = defaultAvatar // production mode } } + if avatar == "" { // to prevent empty avatar in case of db error + avatar = defaultAvatar + } // Data is the base64 encoded image // The actual image starts after the "," diff --git a/apiV1BrokerList.go b/apiV1BrokerList.go index 40d9907..06d130c 100644 --- a/apiV1BrokerList.go +++ b/apiV1BrokerList.go @@ -51,7 +51,6 @@ func apiV1BrokerDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) ue := UserExtra{} ue.Login = "" - ue.Enabled = false ue.License = "" ue.Organization = "" ue.BSB = "" @@ -95,7 +94,6 @@ func apiV1BrokerPut(w http.ResponseWriter, r *http.Request, ss *loan.Session) { ue := UserExtra{} ue.Login = b.Login - ue.Enabled = b.Enabled ue.Role = "broker" ue.License = b.License ue.Organization = b.Organization diff --git a/apiV1PeopleList.go b/apiV1PeopleList.go index 06a21d7..10c0ade 100644 --- a/apiV1PeopleList.go +++ b/apiV1PeopleList.go @@ -62,6 +62,7 @@ func apiV1PeoplePost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { p.First = input.First p.Last = input.Last p.Display = input.Display + p.Enabled = input.Enabled if ss.GetRole() == "admin" { p.Title = input.Title @@ -116,7 +117,6 @@ func apiV1PeopleExtraGet(w http.ResponseWriter, r *http.Request, ss *loan.Sessio u := loan.User{} e := u.Read(id) ret.Login = u.Login - ret.Enabled = u.Enabled if e != nil { log.Error("cannot find user by id", id) @@ -138,7 +138,6 @@ func apiV1PeopleExtraGet(w http.ResponseWriter, r *http.Request, ss *loan.Sessio return } ret.Login = b.Login - ret.Enabled = b.Enabled ret.BSB = b.BSB ret.ACC = b.ACC ret.Organization = b.Organization @@ -149,7 +148,6 @@ func apiV1PeopleExtraGet(w http.ResponseWriter, r *http.Request, ss *loan.Sessio u := loan.User{} e := u.Read(id) ret.Login = u.Login - ret.Enabled = u.Enabled if e != nil { log.Error("cannot find admin by id", id) diff --git a/apiV1User.go b/apiV1User.go index 0057324..96c9232 100644 --- a/apiV1User.go +++ b/apiV1User.go @@ -5,11 +5,11 @@ import ( "encoding/json" "github.com/brianvoe/gofakeit/v6" log "github.com/sirupsen/logrus" + "io/ioutil" "net/http" ) type UserExtra struct { - Enabled bool Login string BSB string ACC string @@ -57,7 +57,6 @@ func apiV1UserPut(w http.ResponseWriter, r *http.Request, ss *loan.Session) { ue := UserExtra{} ue.Login = u.Login - ue.Enabled = u.Enabled ue.Role = "user" ue.License = "" ue.Organization = "" @@ -83,7 +82,6 @@ func apiV1UserDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) { ue := UserExtra{} ue.Login = "" - ue.Enabled = false ue.Role = "people" ue.License = "" ue.Organization = "" @@ -110,7 +108,6 @@ func apiV1UserPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { return } - u.Enabled = input.Enabled u.Login = input.Login e = u.Write() @@ -122,3 +119,35 @@ func apiV1UserPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { } } } + +func apiV1UserEnable(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + id := r.URL.Path[len(apiV1Prefix+"user-enable/"):] //remove prefix + p := loan.People{} + + body, e := ioutil.ReadAll(r.Body) + if e != nil { + log.Error("invalid request body for enable/disable people ", id, " err= ", e.Error()) + apiV1Client403Error(w, r, ss) + return + } + + e = p.Read(id) + if e != nil { + log.Error("failed to read user by id ", id, e.Error()) + apiV1Client403Error(w, r, ss) + return + } + + p.Enabled = string(body) == "true" + if id == "0" { + p.Enabled = true + } // can not disable default admin + + e = p.Write() + if e != nil { + log.Error("failed to enable user ", id, e.Error()) + apiV1Client403Error(w, r, ss) + return + } + apiV1SendJson(p.Enabled, w, r, ss) +} diff --git a/apiV1login.go b/apiV1login.go index e89c868..4fa16ac 100644 --- a/apiV1login.go +++ b/apiV1login.go @@ -77,7 +77,6 @@ func apiV1Login(w http.ResponseWriter, r *http.Request, ss *loan.Session) { func getUserExtraForLogin(u loan.User, ss *loan.Session) (ret UserExtra) { if ss.GetRole() == "user" { ret = UserExtra{ - Enabled: true, Login: u.Login, BSB: "", ACC: "", @@ -94,7 +93,6 @@ func getUserExtraForLogin(u loan.User, ss *loan.Session) (ret UserExtra) { log.Error("fail to retrieve broker for session ", ss, e.Error()) } else { ret = UserExtra{ - Enabled: broker.Enabled, Login: broker.Login, BSB: broker.BSB, ACC: broker.ACC, @@ -107,7 +105,6 @@ func getUserExtraForLogin(u loan.User, ss *loan.Session) (ret UserExtra) { if ss.GetRole() == "admin" { ret = UserExtra{ - Enabled: true, Login: u.Login, BSB: "", ACC: "", diff --git a/apiv1.go b/apiv1.go index 9d7eb40..d5e030e 100644 --- a/apiv1.go +++ b/apiv1.go @@ -50,6 +50,7 @@ func setupApiV1Handler() []apiV1HandlerMap { {"POST", "user/", apiV1UserPost}, {"PUT", "user/", apiV1UserPut}, {"DELETE", "user/", apiV1UserDelete}, + {"POST", "user-enable/", apiV1UserEnable}, {"GET", "broker/", apiV1BrokerGet}, {"POST", "broker/", apiV1BrokerPost}, @@ -95,6 +96,7 @@ func setupApiV1Handler() []apiV1HandlerMap { {"POST", "user/", apiV1UserPost}, {"PUT", "user/", apiV1UserPut}, {"DELETE", "user/", apiV1UserDelete}, + {"POST", "user-enable/", apiV1UserEnable}, {"GET", "broker/", apiV1BrokerGet}, {"POST", "broker/", apiV1BrokerPost},