grafana/pkg/api/dashboard_permission.go

96 lines
2.4 KiB
Go
Raw Normal View History

package api
import (
2017-06-21 18:02:03 -05:00
"time"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/guardian"
)
2018-03-07 10:54:50 -06:00
func GetDashboardPermissionList(c *m.ReqContext) Response {
2018-03-22 06:37:35 -05:00
dashID := c.ParamsInt64(":dashboardId")
2018-03-22 06:37:35 -05:00
_, rsp := getDashboardHelper(c.OrgId, "", dashID, "")
if rsp != nil {
return rsp
}
2018-03-22 06:37:35 -05:00
g := guardian.New(dashID, c.OrgId, c.SignedInUser)
if canAdmin, err := g.CanAdmin(); err != nil || !canAdmin {
return dashboardGuardianResponse(err)
}
acl, err := g.GetAcl()
2017-06-22 16:10:43 -05:00
if err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to get dashboard permissions", err)
}
for _, perm := range acl {
perm.UserAvatarUrl = dtos.GetGravatarUrl(perm.UserEmail)
if perm.TeamId > 0 {
perm.TeamAvatarUrl = dtos.GetGravatarUrlWithDefault(perm.TeamEmail, perm.Team)
}
if perm.Slug != "" {
perm.Url = m.GetDashboardFolderUrl(perm.IsFolder, perm.Uid, perm.Slug)
}
}
2018-03-22 16:13:46 -05:00
return JSON(200, acl)
}
2017-05-22 03:36:47 -05:00
2018-03-07 10:54:50 -06:00
func UpdateDashboardPermissions(c *m.ReqContext, apiCmd dtos.UpdateDashboardAclCommand) Response {
2018-03-22 06:37:35 -05:00
dashID := c.ParamsInt64(":dashboardId")
2018-03-22 06:37:35 -05:00
_, rsp := getDashboardHelper(c.OrgId, "", dashID, "")
if rsp != nil {
return rsp
}
2018-03-22 06:37:35 -05:00
g := guardian.New(dashID, c.OrgId, c.SignedInUser)
if canAdmin, err := g.CanAdmin(); err != nil || !canAdmin {
return dashboardGuardianResponse(err)
}
2017-06-21 18:02:03 -05:00
cmd := m.UpdateDashboardAclCommand{}
2018-03-22 06:37:35 -05:00
cmd.DashboardId = dashID
2017-06-09 14:56:13 -05:00
2017-06-21 18:02:03 -05:00
for _, item := range apiCmd.Items {
cmd.Items = append(cmd.Items, &m.DashboardAcl{
OrgId: c.OrgId,
2018-03-22 06:37:35 -05:00
DashboardId: dashID,
2017-06-21 18:02:03 -05:00
UserId: item.UserId,
2017-12-11 10:46:05 -06:00
TeamId: item.TeamId,
2017-06-21 18:02:03 -05:00
Role: item.Role,
Permission: item.Permission,
Created: time.Now(),
Updated: time.Now(),
})
}
if okToUpdate, err := g.CheckPermissionBeforeUpdate(m.PERMISSION_ADMIN, cmd.Items); err != nil || !okToUpdate {
if err != nil {
if err == guardian.ErrGuardianPermissionExists ||
err == guardian.ErrGuardianOverride {
2018-03-22 16:13:46 -05:00
return Error(400, err.Error(), err)
}
2018-03-22 16:13:46 -05:00
return Error(500, "Error while checking dashboard permissions", err)
}
2018-03-22 16:13:46 -05:00
return Error(403, "Cannot remove own admin permission for a folder", nil)
}
2017-06-09 14:56:13 -05:00
if err := bus.Dispatch(&cmd); err != nil {
if err == m.ErrDashboardAclInfoMissing || err == m.ErrDashboardPermissionDashboardEmpty {
2018-03-22 16:13:46 -05:00
return Error(409, err.Error(), err)
2017-06-09 14:56:13 -05:00
}
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to create permission", err)
2017-06-09 14:56:13 -05:00
}
2018-03-22 16:13:46 -05:00
return Success("Dashboard permissions updated")
2017-06-09 14:56:13 -05:00
}