admin_users.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/api/dtos"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/middleware"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/util"
  8. )
  9. func AdminSearchUsers(c *middleware.Context) {
  10. query := m.SearchUsersQuery{Query: "", Page: 0, Limit: 1000}
  11. if err := bus.Dispatch(&query); err != nil {
  12. c.JsonApiErr(500, "Failed to fetch users", err)
  13. return
  14. }
  15. c.JSON(200, query.Result)
  16. }
  17. func AdminGetUser(c *middleware.Context) {
  18. userId := c.ParamsInt64(":id")
  19. query := m.GetUserByIdQuery{Id: userId}
  20. if err := bus.Dispatch(&query); err != nil {
  21. c.JsonApiErr(500, "Failed to fetch user", err)
  22. return
  23. }
  24. result := m.UserDTO{
  25. Name: query.Result.Name,
  26. Email: query.Result.Email,
  27. Login: query.Result.Login,
  28. }
  29. c.JSON(200, result)
  30. }
  31. func AdminCreateUser(c *middleware.Context, form dtos.AdminCreateUserForm) {
  32. cmd := m.CreateUserCommand{
  33. Login: form.Login,
  34. Email: form.Email,
  35. Password: form.Password,
  36. Name: form.Name,
  37. }
  38. if len(cmd.Login) == 0 {
  39. cmd.Login = cmd.Email
  40. if len(cmd.Login) == 0 {
  41. c.JsonApiErr(400, "Validation error, need specify either username or email", nil)
  42. return
  43. }
  44. }
  45. if len(cmd.Password) < 4 {
  46. c.JsonApiErr(400, "Password is missing or too short", nil)
  47. return
  48. }
  49. if err := bus.Dispatch(&cmd); err != nil {
  50. c.JsonApiErr(500, "failed to create user", err)
  51. return
  52. }
  53. c.JsonOK("User created")
  54. }
  55. func AdminUpdateUser(c *middleware.Context, form dtos.AdminUpdateUserForm) {
  56. userId := c.ParamsInt64(":id")
  57. cmd := m.UpdateUserCommand{
  58. UserId: userId,
  59. Login: form.Login,
  60. Email: form.Email,
  61. Name: form.Name,
  62. }
  63. if len(cmd.Login) == 0 {
  64. cmd.Login = cmd.Email
  65. if len(cmd.Login) == 0 {
  66. c.JsonApiErr(400, "Validation error, need specify either username or email", nil)
  67. return
  68. }
  69. }
  70. if err := bus.Dispatch(&cmd); err != nil {
  71. c.JsonApiErr(500, "failed to update user", err)
  72. return
  73. }
  74. c.JsonOK("User updated")
  75. }
  76. func AdminUpdateUserPassword(c *middleware.Context, form dtos.AdminUpdateUserPasswordForm) {
  77. userId := c.ParamsInt64(":id")
  78. if len(form.Password) < 4 {
  79. c.JsonApiErr(400, "New password too short", nil)
  80. return
  81. }
  82. userQuery := m.GetUserByIdQuery{Id: userId}
  83. if err := bus.Dispatch(&userQuery); err != nil {
  84. c.JsonApiErr(500, "Could not read user from database", err)
  85. return
  86. }
  87. passwordHashed := util.EncodePassword(form.Password, userQuery.Result.Salt)
  88. cmd := m.ChangeUserPasswordCommand{
  89. UserId: userId,
  90. NewPassword: passwordHashed,
  91. }
  92. if err := bus.Dispatch(&cmd); err != nil {
  93. c.JsonApiErr(500, "Failed to update user password", err)
  94. return
  95. }
  96. c.JsonOK("User password updated")
  97. }
  98. func AdminDeleteUser(c *middleware.Context) {
  99. userId := c.ParamsInt64(":id")
  100. cmd := m.DeleteUserCommand{UserId: userId}
  101. if err := bus.Dispatch(&cmd); err != nil {
  102. c.JsonApiErr(500, "Failed to delete user", err)
  103. return
  104. }
  105. c.JsonOK("User deleted")
  106. }