You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.5KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. "encoding/json"
  5. "github.com/brianvoe/gofakeit/v6"
  6. log "github.com/sirupsen/logrus"
  7. "net/http"
  8. )
  9. type UserExtra struct {
  10. Enabled bool
  11. Login string
  12. BSB string
  13. ACC string
  14. License string
  15. Organization string
  16. Role string
  17. }
  18. func decodeJsonUserExtra(r *http.Request) (ret UserExtra, e error) {
  19. decoder := json.NewDecoder(r.Body)
  20. //decoder.DisallowUnknownFields()
  21. e = decoder.Decode(&ret)
  22. if e != nil {
  23. log.Error("failed decoding PayIn for updating", e.Error())
  24. return
  25. }
  26. return
  27. }
  28. // create a new user from people
  29. func apiV1UserPut(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  30. id := r.URL.Path[len(apiV1Prefix+"user/"):] //remove prefix
  31. p := loan.People{}
  32. e := p.Read(id)
  33. if e != nil { // no such people
  34. log.Error("cannot create User without people, id=", id, e.Error())
  35. apiV1Client403Error(w, r, ss)
  36. return
  37. }
  38. u := loan.User{}
  39. u.Id = id
  40. u.Login = id + "@local"
  41. u.Enabled = false
  42. u.Token = gofakeit.LetterN(45)
  43. u.SetPass("")
  44. e = u.Write()
  45. if e != nil {
  46. log.Error("Failed to upgrade user ", id)
  47. apiV1Server500Error(w, r)
  48. return
  49. }
  50. ue := UserExtra{}
  51. ue.Login = u.Login
  52. ue.Enabled = u.Enabled
  53. ue.Role = "user"
  54. ue.License = ""
  55. ue.Organization = ""
  56. ue.BSB = ""
  57. ue.ACC = ""
  58. apiV1SendJson(ue, w, r, ss)
  59. }
  60. func apiV1UserDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  61. id := r.URL.Path[len(apiV1Prefix+"user/"):] //remove prefix
  62. if id == "" {
  63. apiV1Client403Error(w, r, ss)
  64. return
  65. }
  66. e := loan.DeleteUser(id)
  67. if e != nil {
  68. log.Error("Failed to delete user ", id)
  69. apiV1Server500Error(w, r)
  70. return
  71. }
  72. ue := UserExtra{}
  73. ue.Login = ""
  74. ue.Enabled = false
  75. ue.Role = "people"
  76. ue.License = ""
  77. ue.Organization = ""
  78. ue.BSB = ""
  79. ue.ACC = ""
  80. apiV1SendJson(ue, w, r, ss)
  81. }
  82. func apiV1UserPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  83. id := r.URL.Path[len(apiV1Prefix+"user/"):] //remove prefix
  84. input, e := decodeJsonUserExtra(r)
  85. log.Println(input)
  86. if e != nil {
  87. apiV1Client404Error(w, r, ss)
  88. return
  89. } else {
  90. u := loan.User{}
  91. e = u.Read(id)
  92. if e != nil {
  93. log.Error("cannot save basic loan", e.Error())
  94. apiV1SendJson(" [ Error Occurred ] : "+e.Error(), w, r, ss)
  95. return
  96. }
  97. u.Enabled = input.Enabled
  98. u.Login = input.Login
  99. e = u.Write()
  100. if e != nil {
  101. log.Error("cannot save basic loan", e.Error())
  102. apiV1SendJson(" [ Error Occurred ] : "+e.Error(), w, r, ss)
  103. } else {
  104. apiV1SendJson(input, w, r, ss)
  105. }
  106. }
  107. }