admin_users.go 3.4 KB

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