admin_users.go 3.5 KB

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