dashboard_acl.go 2.9 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/services/guardian"
  8. "github.com/grafana/grafana/pkg/util"
  9. )
  10. func GetDashboardAcl(c *middleware.Context) Response {
  11. dash, rsp := getDashboardHelper(c.OrgId, "", c.ParamsInt64(":id"))
  12. if rsp != nil {
  13. return rsp
  14. }
  15. guardian := guardian.NewDashboardGuardian(dash, c.SignedInUser)
  16. canView, err := guardian.CanView(dashboardId, c.OrgRole, c.IsGrafanaAdmin, c.OrgId, c.UserId)
  17. if err != nil {
  18. return ApiError(500, "Failed to get Dashboard ACL", err)
  19. } else if !hasPermission {
  20. return ApiError(403, "Does not have access to this Dashboard ACL")
  21. }
  22. query := m.GetDashboardPermissionsQuery{DashboardId: dashboardId}
  23. if err := bus.Dispatch(&query); err != nil {
  24. return ApiError(500, "Failed to get Dashboard ACL", err)
  25. }
  26. return Json(200, &query.Result)
  27. }
  28. func PostDashboardAcl(c *middleware.Context, cmd m.AddOrUpdateDashboardPermissionCommand) Response {
  29. cmd.OrgId = c.OrgId
  30. cmd.DashboardId = c.ParamsInt64(":id")
  31. if err := bus.Dispatch(&cmd); err != nil {
  32. if err == m.ErrDashboardPermissionUserOrUserGroupEmpty {
  33. return ApiError(409, err.Error(), err)
  34. }
  35. return ApiError(500, "Failed to create permission", err)
  36. }
  37. metrics.M_Api_Dashboard_Acl_Create.Inc(1)
  38. return Json(200, &util.DynMap{
  39. "permissionId": cmd.Result.Id,
  40. "message": "Permission created",
  41. })
  42. }
  43. func DeleteDashboardAclByUser(c *middleware.Context) Response {
  44. dashboardId := c.ParamsInt64(":id")
  45. userId := c.ParamsInt64(":userId")
  46. cmd := m.RemoveDashboardPermissionCommand{DashboardId: dashboardId, UserId: userId, OrgId: c.OrgId}
  47. hasPermission, err := guardian.CanDeleteFromAcl(dashboardId, c.OrgRole, c.IsGrafanaAdmin, c.OrgId, c.UserId)
  48. if err != nil {
  49. return ApiError(500, "Failed to delete from Dashboard ACL", err)
  50. }
  51. if !hasPermission {
  52. return Json(403, util.DynMap{"status": "Forbidden", "message": "Does not have access to this Dashboard ACL"})
  53. }
  54. if err := bus.Dispatch(&cmd); err != nil {
  55. return ApiError(500, "Failed to delete permission for user", err)
  56. }
  57. return Json(200, "")
  58. }
  59. func DeleteDashboardAclByUserGroup(c *middleware.Context) Response {
  60. dashboardId := c.ParamsInt64(":id")
  61. userGroupId := c.ParamsInt64(":userGroupId")
  62. cmd := m.RemoveDashboardPermissionCommand{DashboardId: dashboardId, UserGroupId: userGroupId, OrgId: c.OrgId}
  63. hasPermission, err := guardian.CanDeleteFromAcl(dashboardId, c.OrgRole, c.IsGrafanaAdmin, c.OrgId, c.UserId)
  64. if err != nil {
  65. return ApiError(500, "Failed to delete from Dashboard ACL", err)
  66. }
  67. if !hasPermission {
  68. return Json(403, util.DynMap{"status": "Forbidden", "message": "Does not have access to this Dashboard ACL"})
  69. }
  70. if err := bus.Dispatch(&cmd); err != nil {
  71. return ApiError(500, "Failed to delete permission for user", err)
  72. }
  73. return Json(200, "")
  74. }