grafana/pkg/api/folder_permission.go

119 lines
3.0 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"
"github.com/grafana/grafana/pkg/models"
2018-02-20 08:25:16 -06:00
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/util"
2018-02-20 08:25:16 -06:00
)
func GetFolderPermissionList(c *models.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 {
return toFolderError(models.ErrFolderAccessDenied)
2018-02-20 08:25:16 -06:00
}
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 = models.GetDashboardFolderUrl(perm.IsFolder, perm.Uid, perm.Slug)
2018-02-20 08:25:16 -06:00
}
}
2018-03-22 16:13:46 -05:00
return JSON(200, acl)
2018-02-20 08:25:16 -06:00
}
func UpdateFolderPermissions(c *models.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(models.ErrFolderAccessDenied)
2018-02-20 11:11:50 -06:00
}
cmd := models.UpdateDashboardAclCommand{}
cmd.DashboardID = folder.Id
2018-02-20 08:25:16 -06:00
for _, item := range apiCmd.Items {
cmd.Items = append(cmd.Items, &models.DashboardAcl{
OrgID: c.OrgId,
DashboardID: folder.Id,
UserID: item.UserID,
TeamID: item.TeamID,
2018-02-20 08:25:16 -06:00
Role: item.Role,
Permission: item.Permission,
Created: time.Now(),
Updated: time.Now(),
})
}
if okToUpdate, err := g.CheckPermissionBeforeUpdate(models.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 == models.ErrDashboardAclInfoMissing {
err = models.ErrFolderAclInfoMissing
2018-02-20 08:25:16 -06:00
}
if err == models.ErrDashboardPermissionDashboardEmpty {
err = models.ErrFolderPermissionFolderEmpty
2018-02-20 08:25:16 -06:00
}
if err == models.ErrFolderAclInfoMissing || err == models.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
}
return JSON(200, util.DynMap{
"message": "Folder permissions updated",
"id": folder.Id,
"title": folder.Title,
})
2018-02-20 08:25:16 -06:00
}