userapi.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package api
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/go-chi/chi"
  6. "github.com/go-chi/render"
  7. "github.com/willie68/schematic-service-go/dao"
  8. "github.com/willie68/schematic-service-go/model"
  9. )
  10. // TagsRoutes getting all routes for the tags endpoint
  11. func UsersRoutes() *chi.Mux {
  12. router := chi.NewRouter()
  13. router.Get("/", GetUserEndpoint)
  14. router.Post("/", PostUserEndpoint)
  15. router.Put("/{username}", PutUserEndpoint)
  16. router.Delete("/{username}", DeleteUserEndpoint)
  17. return router
  18. }
  19. //PutUserEndpoint getting all tags back. No paging...
  20. func GetUserEndpoint(response http.ResponseWriter, req *http.Request) {
  21. username, _, _ := req.BasicAuth()
  22. user, ok := dao.GetStorage().GetUser(username)
  23. user.Password = ""
  24. user.NewPassword = ""
  25. if !ok {
  26. Msg(response, http.StatusInternalServerError, "")
  27. return
  28. }
  29. render.JSON(response, req, user)
  30. }
  31. //PutUserEndpoint getting all tags back. No paging...
  32. func PostUserEndpoint(response http.ResponseWriter, req *http.Request) {
  33. var user model.User
  34. err := render.DefaultDecoder(req, &user)
  35. if err != nil {
  36. Msg(response, http.StatusBadRequest, err.Error())
  37. return
  38. }
  39. adminusername, _, _ := req.BasicAuth()
  40. admin, ok := dao.GetStorage().GetUser(adminusername)
  41. if !ok {
  42. Msg(response, http.StatusInternalServerError, "")
  43. return
  44. }
  45. if !admin.Admin {
  46. Msg(response, http.StatusForbidden, "permission denied")
  47. return
  48. }
  49. err = dao.GetStorage().AddUser(user)
  50. if err != nil {
  51. Msg(response, http.StatusBadRequest, err.Error())
  52. return
  53. }
  54. Msg(response, http.StatusCreated, fmt.Sprintf("user \"%s\" created sucessfully", user.Name))
  55. }
  56. //PutUserEndpoint getting all tags back. No paging...
  57. func PutUserEndpoint(response http.ResponseWriter, req *http.Request) {
  58. username := chi.URLParam(req, "username")
  59. var user model.User
  60. err := render.DefaultDecoder(req, &user)
  61. if err != nil {
  62. Msg(response, http.StatusBadRequest, err.Error())
  63. return
  64. }
  65. if username != user.Name {
  66. Msg(response, http.StatusBadRequest, "username should be identically")
  67. return
  68. }
  69. adminusername, _, _ := req.BasicAuth()
  70. admin, ok := dao.GetStorage().GetUser(adminusername)
  71. if !ok {
  72. Msg(response, http.StatusInternalServerError, "")
  73. return
  74. }
  75. if (adminusername != username) && !admin.Admin {
  76. Msg(response, http.StatusForbidden, "permission denied")
  77. return
  78. }
  79. err = dao.GetStorage().ChangePWD(username, user.NewPassword, user.Password)
  80. if err != nil {
  81. Msg(response, http.StatusBadRequest, err.Error())
  82. return
  83. }
  84. return
  85. }
  86. //PutUserEndpoint getting all tags back. No paging...
  87. func DeleteUserEndpoint(response http.ResponseWriter, req *http.Request) {
  88. username := chi.URLParam(req, "username")
  89. adminusername, _, _ := req.BasicAuth()
  90. admin, ok := dao.GetStorage().GetUser(adminusername)
  91. if !ok {
  92. Msg(response, http.StatusInternalServerError, "")
  93. return
  94. }
  95. if !admin.Admin {
  96. Msg(response, http.StatusForbidden, "permission denied")
  97. return
  98. }
  99. err := dao.GetStorage().DeleteUser(username)
  100. if err != nil {
  101. Msg(response, http.StatusBadRequest, err.Error())
  102. return
  103. }
  104. return
  105. }