userapi.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package api
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/go-chi/chi"
  6. "github.com/go-chi/render"
  7. "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/dao"
  8. "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/model"
  9. )
  10. // UsersRoutes routes to the user interface
  11. func UsersRoutes() *chi.Mux {
  12. router := chi.NewRouter()
  13. router.Get("/me", GetMeEndpoint)
  14. router.With(RoleCheck([]string{"admin"})).Get("/", GetUsersEndpoint)
  15. router.With(RoleCheck([]string{"admin"})).Post("/", PostUserEndpoint)
  16. router.With(RoleCheck([]string{"admin"})).Get("/{username}", GetUserEndpoint)
  17. router.With(RoleCheck([]string{"admin"})).Put("/{username}", PutUserEndpoint)
  18. router.With(RoleCheck([]string{"admin"})).Delete("/{username}", DeleteUserEndpoint)
  19. return router
  20. }
  21. //GetMeEndpoint getting all user infos
  22. func GetMeEndpoint(response http.ResponseWriter, request *http.Request) {
  23. username, _, _ := request.BasicAuth()
  24. user, ok := dao.GetStorage().GetUser(username)
  25. if !ok {
  26. render.Render(response, request, ErrInternalServer(errors.New("")))
  27. return
  28. }
  29. user.Password = ""
  30. user.NewPassword = ""
  31. user.Salt = []byte{}
  32. render.JSON(response, request, user)
  33. }
  34. //GetUsersEndpoint getting all user infos
  35. func GetUsersEndpoint(response http.ResponseWriter, request *http.Request) {
  36. users, err := dao.GetStorage().GetUsers()
  37. if err != nil {
  38. render.Render(response, request, ErrInternalServer(err))
  39. return
  40. }
  41. myUsers := make([]model.User, 0)
  42. for _, user := range users {
  43. user.Password = ""
  44. user.NewPassword = ""
  45. user.Salt = []byte{}
  46. myUsers = append(myUsers, user)
  47. }
  48. render.JSON(response, request, myUsers)
  49. }
  50. //GetUserEndpoint getting a user info
  51. func GetUserEndpoint(response http.ResponseWriter, request *http.Request) {
  52. username := chi.URLParam(request, "username")
  53. user, ok := dao.GetStorage().GetUser(username)
  54. user.Password = ""
  55. user.NewPassword = ""
  56. user.Salt = []byte{}
  57. if !ok {
  58. render.Render(response, request, ErrInternalServer(errors.New("")))
  59. return
  60. }
  61. render.JSON(response, request, user)
  62. }
  63. //PostUserEndpoint adding a new user
  64. func PostUserEndpoint(response http.ResponseWriter, request *http.Request) {
  65. var user model.User
  66. err := render.DefaultDecoder(request, &user)
  67. if err != nil {
  68. render.Render(response, request, ErrInvalidRequest(err))
  69. return
  70. }
  71. adminusername, _, _ := request.BasicAuth()
  72. admin, ok := dao.GetStorage().GetUser(adminusername)
  73. if !ok {
  74. render.Render(response, request, ErrInternalServer(errors.New("")))
  75. return
  76. }
  77. if !admin.Admin {
  78. render.Render(response, request, ErrForbidden)
  79. return
  80. }
  81. idm := dao.GetIDM()
  82. user, err = idm.AddUser(user)
  83. if err != nil {
  84. render.Render(response, request, ErrInvalidRequest(err))
  85. return
  86. }
  87. user.Salt = []byte{}
  88. user.Password = "#####"
  89. user.NewPassword = ""
  90. render.Status(request, http.StatusCreated)
  91. render.JSON(response, request, user)
  92. }
  93. //PutUserEndpoint change password of user
  94. func PutUserEndpoint(response http.ResponseWriter, request *http.Request) {
  95. username := chi.URLParam(request, "username")
  96. var user model.User
  97. err := render.DefaultDecoder(request, &user)
  98. if err != nil {
  99. render.Render(response, request, ErrInvalidRequest(err))
  100. return
  101. }
  102. if username != user.Name {
  103. render.Render(response, request, ErrInvalidRequest(errors.New("username should be identically")))
  104. return
  105. }
  106. adminusername, _, _ := request.BasicAuth()
  107. admin, ok := dao.GetStorage().GetUser(adminusername)
  108. if !ok {
  109. render.Render(response, request, ErrInternalServer(errors.New("")))
  110. return
  111. }
  112. if (adminusername != username) && !admin.Admin {
  113. render.Render(response, request, ErrForbidden)
  114. return
  115. }
  116. idm := dao.GetIDM()
  117. err = idm.ChangePWD(username, user.NewPassword, user.Password)
  118. if err != nil {
  119. render.Render(response, request, ErrInvalidRequest(err))
  120. return
  121. }
  122. user.Salt = []byte{}
  123. user.Password = "#####"
  124. user.NewPassword = ""
  125. render.JSON(response, request, user)
  126. }
  127. //DeleteUserEndpoint deleting a user
  128. func DeleteUserEndpoint(response http.ResponseWriter, request *http.Request) {
  129. username := chi.URLParam(request, "username")
  130. adminusername, _, _ := request.BasicAuth()
  131. admin, ok := dao.GetStorage().GetUser(adminusername)
  132. if !ok {
  133. render.Render(response, request, ErrInternalServer(errors.New("")))
  134. return
  135. }
  136. if !admin.Admin {
  137. render.Render(response, request, ErrForbidden)
  138. return
  139. }
  140. idm := dao.GetIDM()
  141. err := idm.DeleteUser(username)
  142. if err != nil {
  143. render.Render(response, request, ErrInvalidRequest(err))
  144. return
  145. }
  146. return
  147. }