2017-05-08 08:35:34 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2017-06-09 14:56:13 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2017-05-08 08:35:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetDashboardAcl(c *middleware.Context) Response {
|
2017-06-19 12:47:44 -05:00
|
|
|
dashId := c.ParamsInt64(":id")
|
2017-05-08 08:35:34 -05:00
|
|
|
|
2017-06-19 12:47:44 -05:00
|
|
|
guardian := guardian.NewDashboardGuardian(dashId, c.OrgId, c.SignedInUser)
|
|
|
|
|
|
|
|
if canView, err := guardian.CanView(); err != nil || !canView {
|
|
|
|
return dashboardGuardianResponse(err)
|
2017-05-08 08:35:34 -05:00
|
|
|
}
|
|
|
|
|
2017-06-19 12:47:44 -05:00
|
|
|
query := m.GetDashboardPermissionsQuery{DashboardId: dashId}
|
2017-05-08 08:35:34 -05:00
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
return ApiError(500, "Failed to get Dashboard ACL", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, &query.Result)
|
|
|
|
}
|
2017-05-22 03:36:47 -05:00
|
|
|
|
2017-06-09 14:56:13 -05:00
|
|
|
func PostDashboardAcl(c *middleware.Context, cmd m.AddOrUpdateDashboardPermissionCommand) Response {
|
2017-06-19 12:47:44 -05:00
|
|
|
dashId := c.ParamsInt64(":id")
|
|
|
|
|
|
|
|
guardian := guardian.NewDashboardGuardian(dashId, c.OrgId, c.SignedInUser)
|
|
|
|
if canSave, err := guardian.CanSave(); err != nil || !canSave {
|
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
2017-06-09 14:56:13 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
2017-06-19 12:47:44 -05:00
|
|
|
cmd.DashboardId = dashId
|
2017-06-09 14:56:13 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2017-06-14 16:45:30 -05:00
|
|
|
if err == m.ErrDashboardPermissionUserOrUserGroupEmpty {
|
|
|
|
return ApiError(409, err.Error(), err)
|
2017-06-09 14:56:13 -05:00
|
|
|
}
|
|
|
|
return ApiError(500, "Failed to create permission", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.M_Api_Dashboard_Acl_Create.Inc(1)
|
|
|
|
|
|
|
|
return Json(200, &util.DynMap{
|
|
|
|
"permissionId": cmd.Result.Id,
|
|
|
|
"message": "Permission created",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:36:47 -05:00
|
|
|
func DeleteDashboardAclByUser(c *middleware.Context) Response {
|
2017-06-19 12:47:44 -05:00
|
|
|
dashId := c.ParamsInt64(":id")
|
|
|
|
userId := c.ParamsInt64(":userId")
|
|
|
|
|
|
|
|
guardian := guardian.NewDashboardGuardian(dashId, c.OrgId, c.SignedInUser)
|
|
|
|
if canSave, err := guardian.CanSave(); err != nil || !canSave {
|
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := m.RemoveDashboardPermissionCommand{DashboardId: dashId, UserId: userId, OrgId: c.OrgId}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
|
return ApiError(500, "Failed to delete permission for user", err)
|
|
|
|
}
|
2017-05-22 03:36:47 -05:00
|
|
|
|
|
|
|
return Json(200, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteDashboardAclByUserGroup(c *middleware.Context) Response {
|
2017-06-19 12:47:44 -05:00
|
|
|
dashId := c.ParamsInt64(":id")
|
|
|
|
userGroupId := c.ParamsInt64(":userGroupId")
|
|
|
|
|
|
|
|
guardian := guardian.NewDashboardGuardian(dashId, c.OrgId, c.SignedInUser)
|
|
|
|
if canSave, err := guardian.CanSave(); err != nil || !canSave {
|
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := m.RemoveDashboardPermissionCommand{DashboardId: dashId, UserGroupId: userGroupId, OrgId: c.OrgId}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
|
return ApiError(500, "Failed to delete permission for user", err)
|
|
|
|
}
|
2017-05-22 03:36:47 -05:00
|
|
|
|
|
|
|
return Json(200, "")
|
|
|
|
}
|