From 1504ad7399bfadce443c79943694f5da1d0d4431 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 29 Mar 2021 01:59:25 +1100 Subject: [PATCH] profile consolidated user + user extra installed for everyone. --- apiV1Avatar.go | 35 +++++++++++++++----------- apiV1ChangePass.go | 12 +++++---- apiV1PeopleList.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++ apiV1User.go | 60 +++++++++++++++++++++++++++++++++++++++++++ apiV1login.go | 27 +++++++++++--------- apiv1.go | 4 +++ go.mod | 2 +- go.sum | 2 ++ 8 files changed, 172 insertions(+), 33 deletions(-) create mode 100644 apiV1User.go diff --git a/apiV1Avatar.go b/apiV1Avatar.go index ac527c1..71fe3a1 100644 --- a/apiV1Avatar.go +++ b/apiV1Avatar.go @@ -49,6 +49,9 @@ func apiV1Avatar(w http.ResponseWriter, r *http.Request, ss *loan.Session) { prefix := apiV1Prefix + "avatar/" id := r.URL.Path[len(prefix):] + fakeAvatar := randomDummyImage() // get some fakeAvatar first + avatar := fakeAvatar + //check local file first path := config.getAvatarPath() + id if fileExists(path) { @@ -57,27 +60,32 @@ func apiV1Avatar(w http.ResponseWriter, r *http.Request, ss *loan.Session) { } // Data is the base64 encoded image - //check database - ppl := loan.People{} - fakeAvatar, e := ppl.ReadAvatar(id) - - if e != nil { + // check database + ppl := loan.People{Id: id} + avatar, e := ppl.ReadAvatar() // read avatar + if e != nil { // not able to read if config.Debug { - fakeAvatar = randomDummyImage() + avatar = fakeAvatar // debug mode } else { - fakeAvatar = defaultAvatar + avatar = defaultAvatar // production mode } } // Data is the base64 encoded image // The actual image starts after the "," - i := strings.Index(fakeAvatar, ",") + i := strings.Index(avatar, ",") // pass reader to NewDecoder - imgData := base64.NewDecoder(base64.StdEncoding, strings.NewReader(fakeAvatar[i+1:])) + imgData := base64.NewDecoder(base64.StdEncoding, strings.NewReader(avatar[i+1:])) //send out - w.Header().Set("Content-Type", "image/png") + w.Header().Set("Content-Type", getMimeType(avatar)) io.Copy(w, imgData) +} +func getMimeType(avatar string) string { + // data:image/png;base64,some-data + start := strings.Index(avatar, ":") + end := strings.Index(avatar, ";") + return avatar[start+1 : end] } func fileExists(path string) bool { @@ -105,15 +113,12 @@ func apiV1AvatarPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { apiV1Client403Error(w, r, ss) return } - p := loan.People{} - p.Read(id) - p.Avatar = string(body) - e = p.Write() + p := loan.People{Id: id} // not reading everything, just set Id for write avatar only. + e = p.WriteAvatar(string(body)) if e != nil { log.Error("cannot write avatar ", id, " err ", e.Error()) apiV1Server500Error(w, r) return } - apiV1SendJson(true, w, r, ss) } diff --git a/apiV1ChangePass.go b/apiV1ChangePass.go index a2dfa42..818207b 100644 --- a/apiV1ChangePass.go +++ b/apiV1ChangePass.go @@ -43,11 +43,13 @@ func apiV1ChangePass(w http.ResponseWriter, r *http.Request, ss *loan.Session) { return } - e = u.VerifyPass(input.OldPassword) - if e != nil { - log.Error("wrong old password ", id, e.Error()) - apiV1Client403Error(w, r, ss) - return + if ss.GetRole() != "admin" { + e = u.VerifyPass(input.OldPassword) + if e != nil { + log.Error("wrong old password ", id, e.Error()) + apiV1Client403Error(w, r, ss) + return + } } u.SetPass(input.NewPass) diff --git a/apiV1PeopleList.go b/apiV1PeopleList.go index c62b515..f71f344 100644 --- a/apiV1PeopleList.go +++ b/apiV1PeopleList.go @@ -2,6 +2,7 @@ package main import ( "biukop.com/sfm/loan" + "database/sql" "encoding/json" log "github.com/sirupsen/logrus" "net/http" @@ -75,3 +76,65 @@ func apiV1PeoplePost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { } apiV1SendJson(p, w, r, ss) } + +func apiV1PeopleExtraGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + id := r.URL.Path[len(apiV1Prefix+"people-extra/"):] + ret := UserExtra{} + ret.Role = loan.GetRoleById(id) + + switch ret.Role { + case "people": + apiV1SendJson(ret, w, r, ss) + return + + case "user": + 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) + apiV1Client404Error(w, r, ss) + return + } + apiV1SendJson(ret, w, r, ss) + return + + case "broker": + b := loan.Broker{} + e := b.Read(id) + if e != nil { // this is broker + if e != sql.ErrNoRows { + log.Error("cannot find user by id", id) + } + apiV1Client404Error(w, r, ss) + ret.Role = "user" + return + } + ret.Login = b.Login + ret.Enabled = b.Enabled + ret.BSB = b.BSB + ret.ACC = b.ACC + ret.Organization = b.Organization + ret.License = b.License + apiV1SendJson(ret, w, r, ss) + return + case "admin": + 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) + apiV1Client404Error(w, r, ss) + return + } + apiV1SendJson(ret, w, r, ss) + return + default: + apiV1SendJson(ret, w, r, ss) + return + } +} diff --git a/apiV1User.go b/apiV1User.go new file mode 100644 index 0000000..3f8ebbd --- /dev/null +++ b/apiV1User.go @@ -0,0 +1,60 @@ +package main + +import ( + "biukop.com/sfm/loan" + "encoding/json" + log "github.com/sirupsen/logrus" + "net/http" +) + +type UserExtra struct { + Enabled bool + Login string + BSB string + ACC string + License string + Organization string + Role string +} + +func decodeJsonUserExtra(r *http.Request) (ret UserExtra, e error) { + decoder := json.NewDecoder(r.Body) + //decoder.DisallowUnknownFields() + e = decoder.Decode(&ret) + if e != nil { + log.Error("failed decoding PayIn for updating", e.Error()) + return + } + return +} + +func apiV1UserPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + id := r.URL.Path[len(apiV1Prefix+"user/"):] //remove prefix + + input, e := decodeJsonUserExtra(r) + log.Println(input) + + if e != nil { + apiV1Client404Error(w, r, ss) + return + } else { + u := loan.User{} + e = u.Read(id) + if e != nil { + log.Error("cannot save basic loan", e.Error()) + apiV1SendJson(" [ Error Occurred ] : "+e.Error(), w, r, ss) + return + } + + u.Enabled = input.Enabled + u.Login = input.Login + + e = u.Write() + if e != nil { + log.Error("cannot save basic loan", e.Error()) + apiV1SendJson(" [ Error Occurred ] : "+e.Error(), w, r, ss) + } else { + apiV1SendJson(input.Login, w, r, ss) + } + } +} diff --git a/apiV1login.go b/apiV1login.go index 160b8bd..0cd20f5 100644 --- a/apiV1login.go +++ b/apiV1login.go @@ -14,15 +14,6 @@ type loginForm struct { Pass string `json:"p"` } -type userExtra struct { - Enabled int - Login string - BSB string - ACC string - License string - Organization string -} - func apiV1Login(w http.ResponseWriter, r *http.Request, ss *loan.Session) { res := apiV1ResponseBlank() @@ -60,7 +51,7 @@ func apiV1Login(w http.ResponseWriter, r *http.Request, ss *loan.Session) { res.add("role", ss.GetRole()) u, e := ss.GetUser() if e == nil { - res.add("user", u.People) + res.add("User", u.People) if ss.GetRole() == "broker" { broker := loan.Broker{} @@ -68,7 +59,7 @@ func apiV1Login(w http.ResponseWriter, r *http.Request, ss *loan.Session) { if e != nil { log.Error("fail to retrieve broker for session ", ss, e.Error()) } else { - ue := userExtra{ + ue := UserExtra{ Enabled: broker.Enabled, Login: broker.Login, BSB: broker.BSB, @@ -76,8 +67,20 @@ func apiV1Login(w http.ResponseWriter, r *http.Request, ss *loan.Session) { License: broker.License, Organization: broker.Organization, } - res.add("userExtra", ue) + res.add("UserExtra", ue) + } + } + + if ss.GetRole() == "admin" { + ue := UserExtra{ + Enabled: true, + Login: u.Login, + BSB: "", + ACC: "", + License: "", + Organization: "SFM", } + res.add("UserExtra", ue) } } res.add("Biukop-Session", ss.Id) diff --git a/apiv1.go b/apiv1.go index 9596030..6e7526c 100644 --- a/apiv1.go +++ b/apiv1.go @@ -41,6 +41,7 @@ func setupApiV1Handler() []apiV1HandlerMap { {"DELETE", "loan/", apiV1LoanSingleDelete}, {"GET", "loan-by-client/", apiV1LoanByClient}, {"GET", "people/", apiV1PeopleGet}, + {"GET", "people-extra/", apiV1PeopleExtraGet}, {"POST", "people/", apiV1PeoplePost}, {"GET", "broker/", apiV1BrokerGet}, {"POST", "broker/", apiV1BrokerPost}, @@ -54,6 +55,7 @@ func setupApiV1Handler() []apiV1HandlerMap { {"GET", "broker-list/", apiV1BrokerList}, {"POST", "sync-people/", apiV1SyncPeople}, {"POST", "payIn/", apiV1PayInPost}, + {"POST", "user/", apiV1UserPost}, {"DELETE", "payIn/", apiV1PayInDelete}, {"GET", "user-reward/", apiV1UserReward}, {"GET", "login", apiV1DumpRequest}, @@ -73,6 +75,7 @@ func setupApiV1Handler() []apiV1HandlerMap { {"DELETE", "loan/", apiV1LoanSingleDelete}, {"GET", "loan-by-client/", apiV1LoanByClient}, {"GET", "people/", apiV1PeopleGet}, + {"GET", "people-extra/", apiV1PeopleExtraGet}, {"POST", "people/", apiV1PeoplePost}, {"GET", "broker/", apiV1BrokerGet}, {"POST", "broker/", apiV1BrokerPost}, @@ -86,6 +89,7 @@ func setupApiV1Handler() []apiV1HandlerMap { {"GET", "broker-list/", apiV1BrokerList}, {"POST", "sync-people/", apiV1SyncPeople}, {"POST", "payIn/", apiV1PayInPost}, + {"POST", "user/", apiV1UserPost}, {"DELETE", "payIn/", apiV1PayInDelete}, {"GET", "user-reward/", apiV1UserReward}, {"GET", "login", apiV1EmptyResponse}, diff --git a/go.mod b/go.mod index b785722..3d01398 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,6 @@ require ( biukop.com/sfm/loan v0.0.0-00010101000000-000000000000 github.com/brianvoe/gofakeit/v6 v6.0.1 github.com/gorilla/websocket v1.4.2 - github.com/sirupsen/logrus v1.7.0 + github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.2.2 ) diff --git a/go.sum b/go.sum index 828fb15..4d75a80 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=