grafana/pkg/api/folder_permission.go

114 lines
2.8 KiB
Go
Raw Normal View History

2018-02-20 08:25:16 -06:00
package api
import (
"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/dashboards"
"github.com/grafana/grafana/pkg/services/guardian"
)
2018-03-07 10:54:50 -06:00
func GetFolderPermissionList(c *m.ReqContext) Response {
2018-02-20 08:25:16 -06:00
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
2018-03-22 16:13:46 -05:00
folder, err := s.GetFolderByUID(c.Params(":uid"))
2018-02-20 08:25:16 -06:00
if err != nil {
return toFolderError(err)
}
g := guardian.New(folder.Id, c.OrgId, c.SignedInUser)
2018-02-20 08:25:16 -06:00
if canAdmin, err := g.CanAdmin(); err != nil || !canAdmin {
2018-02-20 08:25:16 -06:00
return toFolderError(m.ErrFolderAccessDenied)
}
acl, err := g.GetAcl()
2018-02-20 08:25:16 -06:00
if err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to get folder permissions", err)
2018-02-20 08:25:16 -06:00
}
for _, perm := range acl {
perm.FolderId = folder.Id
perm.DashboardId = 0
perm.UserAvatarUrl = dtos.GetGravatarUrl(perm.UserEmail)
if perm.TeamId > 0 {
perm.TeamAvatarUrl = dtos.GetGravatarUrlWithDefault(perm.TeamEmail, perm.Team)
}
2018-02-20 08:25:16 -06:00
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)
2018-02-20 08:25:16 -06:00
}
2018-03-07 10:54:50 -06:00
func UpdateFolderPermissions(c *m.ReqContext, apiCmd dtos.UpdateDashboardAclCommand) Response {
2018-02-20 08:25:16 -06:00
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
2018-03-22 16:13:46 -05:00
folder, err := s.GetFolderByUID(c.Params(":uid"))
2018-02-20 08:25:16 -06:00
if err != nil {
return toFolderError(err)
}
g := guardian.New(folder.Id, c.OrgId, c.SignedInUser)
canAdmin, err := g.CanAdmin()
2018-02-20 11:11:50 -06:00
if err != nil {
2018-02-20 08:25:16 -06:00
return toFolderError(err)
}
2018-02-20 11:11:50 -06:00
if !canAdmin {
return toFolderError(m.ErrFolderAccessDenied)
}
2018-02-20 08:25:16 -06:00
cmd := m.UpdateDashboardAclCommand{}
cmd.DashboardId = folder.Id
for _, item := range apiCmd.Items {
cmd.Items = append(cmd.Items, &m.DashboardAcl{
OrgId: c.OrgId,
DashboardId: folder.Id,
UserId: item.UserId,
TeamId: item.TeamId,
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 {
2018-02-20 08:25:16 -06:00
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 folder permissions", err)
2018-02-20 08:25:16 -06:00
}
2018-03-22 16:13:46 -05:00
return Error(403, "Cannot remove own admin permission for a folder", nil)
2018-02-20 08:25:16 -06:00
}
if err := bus.Dispatch(&cmd); err != nil {
if err == m.ErrDashboardAclInfoMissing {
err = m.ErrFolderAclInfoMissing
}
if err == m.ErrDashboardPermissionDashboardEmpty {
err = m.ErrFolderPermissionFolderEmpty
}
if err == m.ErrFolderAclInfoMissing || err == m.ErrFolderPermissionFolderEmpty {
2018-03-22 16:13:46 -05:00
return Error(409, err.Error(), err)
2018-02-20 08:25:16 -06:00
}
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to create permission", err)
2018-02-20 08:25:16 -06:00
}
2018-03-22 16:13:46 -05:00
return Success("Folder permissions updated")
2018-02-20 08:25:16 -06:00
}