user.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. "github.com/grafana/grafana/pkg/middleware"
  5. m "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/setting"
  7. "github.com/grafana/grafana/pkg/util"
  8. )
  9. // GET /api/user (current authenticated user)
  10. func GetSignedInUser(c *middleware.Context) Response {
  11. return getUserUserProfile(c.UserId)
  12. }
  13. // GET /api/user/:id
  14. func GetUserById(c *middleware.Context) Response {
  15. return getUserUserProfile(c.ParamsInt64(":id"))
  16. }
  17. func getUserUserProfile(userId int64) Response {
  18. query := m.GetUserProfileQuery{UserId: userId}
  19. if err := bus.Dispatch(&query); err != nil {
  20. return ApiError(500, "Failed to get user", err)
  21. }
  22. return Json(200, query.Result)
  23. }
  24. // POST /api/user
  25. func UpdateSignedInUser(c *middleware.Context, cmd m.UpdateUserCommand) Response {
  26. cmd.UserId = c.UserId
  27. return handleUpdateUser(cmd)
  28. }
  29. // POST /api/users/:id
  30. func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) Response {
  31. cmd.UserId = c.ParamsInt64(":id")
  32. return handleUpdateUser(cmd)
  33. }
  34. func handleUpdateUser(cmd m.UpdateUserCommand) Response {
  35. if len(cmd.Login) == 0 {
  36. cmd.Login = cmd.Email
  37. if len(cmd.Login) == 0 {
  38. return ApiError(400, "Validation error, need specify either username or email", nil)
  39. }
  40. }
  41. if err := bus.Dispatch(&cmd); err != nil {
  42. return ApiError(500, "failed to update user", err)
  43. }
  44. return ApiSuccess("User updated")
  45. }
  46. // GET /api/user/orgs
  47. func GetSignedInUserOrgList(c *middleware.Context) Response {
  48. return getUserOrgList(c.UserId)
  49. }
  50. // GET /api/user/:id/orgs
  51. func GetUserOrgList(c *middleware.Context) Response {
  52. return getUserOrgList(c.ParamsInt64(":id"))
  53. }
  54. func getUserOrgList(userId int64) Response {
  55. query := m.GetUserOrgListQuery{UserId: userId}
  56. if err := bus.Dispatch(&query); err != nil {
  57. return ApiError(500, "Faile to get user organziations", err)
  58. }
  59. return Json(200, query.Result)
  60. }
  61. func validateUsingOrg(userId int64, orgId int64) bool {
  62. query := m.GetUserOrgListQuery{UserId: userId}
  63. if err := bus.Dispatch(&query); err != nil {
  64. return false
  65. }
  66. // validate that the org id in the list
  67. valid := false
  68. for _, other := range query.Result {
  69. if other.OrgId == orgId {
  70. valid = true
  71. }
  72. }
  73. return valid
  74. }
  75. // POST /api/user/using/:id
  76. func UserSetUsingOrg(c *middleware.Context) Response {
  77. orgId := c.ParamsInt64(":id")
  78. if !validateUsingOrg(c.UserId, orgId) {
  79. return ApiError(401, "Not a valid organization", nil)
  80. }
  81. cmd := m.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgId}
  82. if err := bus.Dispatch(&cmd); err != nil {
  83. return ApiError(500, "Failed change active organization", err)
  84. }
  85. return ApiSuccess("Active organization changed")
  86. }
  87. // GET /profile/switch-org/:id
  88. func ChangeActiveOrgAndRedirectToHome(c *middleware.Context) {
  89. orgId := c.ParamsInt64(":id")
  90. if !validateUsingOrg(c.UserId, orgId) {
  91. NotFoundHandler(c)
  92. }
  93. cmd := m.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgId}
  94. if err := bus.Dispatch(&cmd); err != nil {
  95. NotFoundHandler(c)
  96. }
  97. c.Redirect(setting.AppSubUrl + "/")
  98. }
  99. func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) Response {
  100. userQuery := m.GetUserByIdQuery{Id: c.UserId}
  101. if err := bus.Dispatch(&userQuery); err != nil {
  102. return ApiError(500, "Could not read user from database", err)
  103. }
  104. passwordHashed := util.EncodePassword(cmd.OldPassword, userQuery.Result.Salt)
  105. if passwordHashed != userQuery.Result.Password {
  106. return ApiError(401, "Invalid old password", nil)
  107. }
  108. if len(cmd.NewPassword) < 4 {
  109. return ApiError(400, "New password too short", nil)
  110. }
  111. cmd.UserId = c.UserId
  112. cmd.NewPassword = util.EncodePassword(cmd.NewPassword, userQuery.Result.Salt)
  113. if err := bus.Dispatch(&cmd); err != nil {
  114. return ApiError(500, "Failed to change user password", err)
  115. }
  116. return ApiSuccess("User password changed")
  117. }
  118. // GET /api/users
  119. func SearchUsers(c *middleware.Context) Response {
  120. query := m.SearchUsersQuery{Query: "", Page: 0, Limit: 1000}
  121. if err := bus.Dispatch(&query); err != nil {
  122. return ApiError(500, "Failed to fetch users", err)
  123. }
  124. return Json(200, query.Result)
  125. }