|
- package main
-
- import (
- "biukop.com/sfm/loan"
- "encoding/json"
- "github.com/brianvoe/gofakeit/v6"
- log "github.com/sirupsen/logrus"
- "io/ioutil"
- "net/http"
- )
-
- type UserExtra struct {
- 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
- }
-
- // create a new user from people
- func apiV1UserPut(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
- id := r.URL.Path[len(apiV1Prefix+"user/"):] //remove prefix
-
- p := loan.People{}
- e := p.Read(id)
- if e != nil { // no such people
- log.Error("cannot create User without people, id=", id, e.Error())
- apiV1Client403Error(w, r, ss)
- return
- }
-
- u := loan.User{}
- u.Id = id
- u.Login = id + "@local"
- u.Enabled = false
- u.Token = gofakeit.LetterN(45)
- u.SetPass("")
- e = u.Write()
-
- if e != nil {
- log.Error("Failed to upgrade user ", id)
- apiV1Server500Error(w, r)
- return
- }
-
- ue := UserExtra{}
- ue.Login = u.Login
- ue.Role = "user"
- ue.License = ""
- ue.Organization = ""
- ue.BSB = ""
- ue.ACC = ""
-
- apiV1SendJson(ue, w, r, ss)
- }
-
- func apiV1UserDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
- id := r.URL.Path[len(apiV1Prefix+"user/"):] //remove prefix
-
- if id == "" {
- apiV1Client403Error(w, r, ss)
- return
- }
- e := loan.DeleteUser(id)
- if e != nil {
- log.Error("Failed to delete user ", id)
- apiV1Server500Error(w, r)
- return
- }
-
- ue := UserExtra{}
- ue.Login = ""
- ue.Role = "people"
- ue.License = ""
- ue.Organization = ""
- ue.BSB = ""
- ue.ACC = ""
- apiV1SendJson(ue, w, r, ss)
- }
-
- 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.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, w, r, ss)
- }
- }
- }
-
- 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)
- }
-
- func apiV1UserExGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
- id := r.URL.Path[len(apiV1Prefix+"user-ex/"):]
- ret := loan.UserEx{}
- e := ret.Read(id)
- if e != nil {
- log.Error("read people error", id, e.Error())
- apiV1Client404Error(w, r, ss)
- return
- }
- apiV1SendJson(ret, w, r, ss)
- }
-
- func apiV1UserExList(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
- filter := ""
- keys, ok := r.URL.Query()["filter"]
- if ok && len(keys) >= 1 {
- filter = keys[0]
- }
- data := loan.GetUserExList(filter)
- apiV1SendJson(data, w, r, ss)
- }
|