Pārlūkot izejas kodu

profile consolidated user + user extra installed for everyone.

master
sp pirms 4 gadiem
vecāks
revīzija
1504ad7399
8 mainītis faili ar 172 papildinājumiem un 33 dzēšanām
  1. +20
    -15
      apiV1Avatar.go
  2. +7
    -5
      apiV1ChangePass.go
  3. +63
    -0
      apiV1PeopleList.go
  4. +60
    -0
      apiV1User.go
  5. +15
    -12
      apiV1login.go
  6. +4
    -0
      apiv1.go
  7. +1
    -1
      go.mod
  8. +2
    -0
      go.sum

+ 20
- 15
apiV1Avatar.go Parādīt failu

@@ -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)
}

+ 7
- 5
apiV1ChangePass.go Parādīt failu

@@ -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)

+ 63
- 0
apiV1PeopleList.go Parādīt failu

@@ -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
}
}

+ 60
- 0
apiV1User.go Parādīt failu

@@ -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)
}
}
}

+ 15
- 12
apiV1login.go Parādīt failu

@@ -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)

+ 4
- 0
apiv1.go Parādīt failu

@@ -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},

+ 1
- 1
go.mod Parādīt failu

@@ -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
)

+ 2
- 0
go.sum Parādīt failu

@@ -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=

Notiek ielāde…
Atcelt
Saglabāt