user_group.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. "github.com/grafana/grafana/pkg/metrics"
  5. "github.com/grafana/grafana/pkg/middleware"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/util"
  8. )
  9. // POST /api/user-groups
  10. func CreateUserGroup(c *middleware.Context, cmd m.CreateUserGroupCommand) Response {
  11. cmd.OrgId = c.OrgId
  12. if err := bus.Dispatch(&cmd); err != nil {
  13. if err == m.ErrUserGroupNameTaken {
  14. return ApiError(409, "User Group name taken", err)
  15. }
  16. return ApiError(500, "Failed to create User Group", err)
  17. }
  18. metrics.M_Api_UserGroup_Create.Inc(1)
  19. return Json(200, &util.DynMap{
  20. "userGroupId": cmd.Result.Id,
  21. "message": "User Group created",
  22. })
  23. }
  24. // PUT /api/user-groups/:userGroupId
  25. func UpdateUserGroup(c *middleware.Context, cmd m.UpdateUserGroupCommand) Response {
  26. cmd.Id = c.ParamsInt64(":userGroupId")
  27. if err := bus.Dispatch(&cmd); err != nil {
  28. if err == m.ErrUserGroupNameTaken {
  29. return ApiError(400, "User Group name taken", err)
  30. }
  31. return ApiError(500, "Failed to update User Group", err)
  32. }
  33. return ApiSuccess("User Group updated")
  34. }
  35. // DELETE /api/user-groups/:userGroupId
  36. func DeleteUserGroupById(c *middleware.Context) Response {
  37. if err := bus.Dispatch(&m.DeleteUserGroupCommand{Id: c.ParamsInt64(":userGroupId")}); err != nil {
  38. if err == m.ErrUserGroupNotFound {
  39. return ApiError(404, "Failed to delete User Group. ID not found", nil)
  40. }
  41. return ApiError(500, "Failed to update User Group", err)
  42. }
  43. return ApiSuccess("User Group deleted")
  44. }
  45. // GET /api/user-groups/search
  46. func SearchUserGroups(c *middleware.Context) Response {
  47. perPage := c.QueryInt("perpage")
  48. if perPage <= 0 {
  49. perPage = 1000
  50. }
  51. page := c.QueryInt("page")
  52. if page < 1 {
  53. page = 1
  54. }
  55. query := m.SearchUserGroupsQuery{
  56. Query: c.Query("query"),
  57. Name: c.Query("name"),
  58. Page: page,
  59. Limit: perPage,
  60. }
  61. if err := bus.Dispatch(&query); err != nil {
  62. return ApiError(500, "Failed to search User Groups", err)
  63. }
  64. query.Result.Page = page
  65. query.Result.PerPage = perPage
  66. return Json(200, query.Result)
  67. }
  68. // GET /api/user-groups/:userGroupId
  69. func GetUserGroupById(c *middleware.Context) Response {
  70. query := m.GetUserGroupByIdQuery{Id: c.ParamsInt64(":userGroupId")}
  71. if err := bus.Dispatch(&query); err != nil {
  72. if err == m.ErrUserGroupNotFound {
  73. return ApiError(404, "User Group not found", err)
  74. }
  75. return ApiError(500, "Failed to get User Group", err)
  76. }
  77. return Json(200, &query.Result)
  78. }