| import ( | import ( | ||||
| "biukop.com/sfm/loan" | "biukop.com/sfm/loan" | ||||
| "encoding/json" | |||||
| log "github.com/sirupsen/logrus" | log "github.com/sirupsen/logrus" | ||||
| "net/http" | "net/http" | ||||
| ) | ) | ||||
| } | } | ||||
| apiV1SendJson(b, w, r, ss) | apiV1SendJson(b, w, r, ss) | ||||
| } | } | ||||
| func decodeJsonBrokerEdit(r *http.Request) (ret loan.Broker, e error) { | |||||
| decoder := json.NewDecoder(r.Body) | |||||
| //decoder.DisallowUnknownFields() | |||||
| e = decoder.Decode(&ret) | |||||
| if e != nil { | |||||
| log.Error("failed decoding json for Filtering full_loan_summary ", e.Error()) | |||||
| return | |||||
| } | |||||
| return | |||||
| } | |||||
| func apiV1BrokerPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { | |||||
| id := r.URL.Path[len(apiV1Prefix+"broker/"):] | |||||
| b := loan.Broker{} | |||||
| e := b.Read(id) | |||||
| if e != nil { | |||||
| log.Error("cannot find broker by id", id, e.Error()) | |||||
| apiV1Client404Error(w, r, ss) | |||||
| return | |||||
| } | |||||
| input, e := decodeJsonBrokerEdit(r) | |||||
| if e != nil { | |||||
| log.Error("cannot find broker by id", id, e.Error()) | |||||
| apiV1Client404Error(w, r, ss) | |||||
| return | |||||
| } | |||||
| b.BSB = input.BSB | |||||
| b.ACC = input.ACC | |||||
| b.Display = input.Display | |||||
| b.First = input.First | |||||
| b.Last = input.Last | |||||
| b.License = input.License | |||||
| b.Organization = input.Organization | |||||
| e = b.Write() | |||||
| if e != nil { | |||||
| log.Error("failed to save broker ", b, e.Error()) | |||||
| apiV1Client404Error(w, r, ss) | |||||
| return | |||||
| } | |||||
| apiV1SendJson(b, w, r, ss) | |||||
| } |
| package main | |||||
| import ( | |||||
| "biukop.com/sfm/loan" | |||||
| "encoding/json" | |||||
| log "github.com/sirupsen/logrus" | |||||
| "net/http" | |||||
| ) | |||||
| type changePass struct { | |||||
| OldPassword string | |||||
| NewPass string | |||||
| NewPass1 string | |||||
| } | |||||
| func decodeJsonChangePass(r *http.Request) (ret changePass, e error) { | |||||
| decoder := json.NewDecoder(r.Body) | |||||
| //decoder.DisallowUnknownFields() | |||||
| e = decoder.Decode(&ret) | |||||
| if e != nil { | |||||
| log.Error("failed decoding json change password ", e.Error()) | |||||
| return | |||||
| } | |||||
| return | |||||
| } | |||||
| func apiV1ChangePass(w http.ResponseWriter, r *http.Request, ss *loan.Session) { | |||||
| id := r.URL.Path[len(apiV1Prefix+"change-pass/"):] | |||||
| u := loan.User{} | |||||
| e := u.Read(id) | |||||
| if e != nil { | |||||
| log.Error("cannot change pass for user ", id, e.Error()) | |||||
| apiV1Client403Error(w, r, ss) | |||||
| return | |||||
| } | |||||
| input, e := decodeJsonChangePass(r) | |||||
| if e != nil { | |||||
| log.Error("cannot decode json for change password ", id, e.Error()) | |||||
| apiV1Client403Error(w, r, ss) | |||||
| return | |||||
| } | |||||
| 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) | |||||
| e = u.Write() | |||||
| if e != nil { | |||||
| log.Error("fail to update new pass ", id, e.Error()) | |||||
| apiV1Server500Error(w, r) | |||||
| return | |||||
| } | |||||
| apiV1SendJson(true, w, r, ss) | |||||
| } |
| Pass string `json:"p"` | 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) { | func apiV1Login(w http.ResponseWriter, r *http.Request, ss *loan.Session) { | ||||
| res := apiV1ResponseBlank() | res := apiV1ResponseBlank() | ||||
| u, e := ss.GetUser() | u, e := ss.GetUser() | ||||
| if e == nil { | if e == nil { | ||||
| res.add("user", u.People) | res.add("user", u.People) | ||||
| if ss.GetRole() == "broker" { | |||||
| broker := loan.Broker{} | |||||
| e := broker.Read(ss.User) | |||||
| if e != nil { | |||||
| log.Error("fail to retrieve broker for session ", ss, e.Error()) | |||||
| } else { | |||||
| ue := userExtra{ | |||||
| Enabled: broker.Enabled, | |||||
| Login: broker.Login, | |||||
| BSB: broker.BSB, | |||||
| ACC: broker.ACC, | |||||
| License: broker.License, | |||||
| Organization: broker.Organization, | |||||
| } | |||||
| res.add("userExtra", ue) | |||||
| } | |||||
| } | |||||
| } | } | ||||
| res.add("Biukop-Session", ss.Id) | res.add("Biukop-Session", ss.Id) | ||||
| res.add("Biukop-Mid", ss.Get("Biukop-Mid")) | res.add("Biukop-Mid", ss.Get("Biukop-Mid")) |
| {"DELETE", "loan/", apiV1LoanSingleDelete}, | {"DELETE", "loan/", apiV1LoanSingleDelete}, | ||||
| {"GET", "people/", apiV1PeopleGet}, | {"GET", "people/", apiV1PeopleGet}, | ||||
| {"GET", "broker/", apiV1BrokerGet}, | {"GET", "broker/", apiV1BrokerGet}, | ||||
| {"POST", "broker/", apiV1BrokerPost}, | |||||
| {"POST", "change-pass/", apiV1ChangePass}, | |||||
| {"POST", "loan/basic/", apiV1LoanSinglePostBasic}, | {"POST", "loan/basic/", apiV1LoanSinglePostBasic}, | ||||
| {"GET", "avatar/", apiV1Avatar}, | {"GET", "avatar/", apiV1Avatar}, | ||||
| {"POST", "avatar/", apiV1AvatarPost}, | {"POST", "avatar/", apiV1AvatarPost}, | ||||
| {"DELETE", "loan/", apiV1LoanSingleDelete}, | {"DELETE", "loan/", apiV1LoanSingleDelete}, | ||||
| {"GET", "people/", apiV1PeopleGet}, | {"GET", "people/", apiV1PeopleGet}, | ||||
| {"GET", "broker/", apiV1BrokerGet}, | {"GET", "broker/", apiV1BrokerGet}, | ||||
| {"POST", "broker/", apiV1BrokerPost}, | |||||
| {"POST", "change-pass/", apiV1ChangePass}, | |||||
| {"POST", "loan/basic/", apiV1LoanSinglePostBasic}, | {"POST", "loan/basic/", apiV1LoanSinglePostBasic}, | ||||
| {"GET", "avatar/", apiV1Avatar}, | {"GET", "avatar/", apiV1Avatar}, | ||||
| {"POST", "avatar/", apiV1AvatarPost}, | {"POST", "avatar/", apiV1AvatarPost}, |