2017-05-08 08:35:34 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
2017-06-21 18:02:03 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2017-05-08 08:35:34 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
|
|
|
)
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) GetDashboardPermissionList(c *models.ReqContext) response.Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
dashID := c.ParamsInt64(":dashboardId")
|
2017-05-08 08:35:34 -05:00
|
|
|
|
2021-06-15 09:08:27 -05:00
|
|
|
_, rsp := getDashboardHelper(c.Req.Context(), c.OrgId, dashID, "")
|
2018-02-12 02:26:09 -06:00
|
|
|
if rsp != nil {
|
|
|
|
return rsp
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
g := guardian.New(dashID, c.OrgId, c.SignedInUser)
|
2017-06-19 12:47:44 -05:00
|
|
|
|
2018-02-26 13:15:57 -06:00
|
|
|
if canAdmin, err := g.CanAdmin(); err != nil || !canAdmin {
|
2017-06-19 12:47:44 -05:00
|
|
|
return dashboardGuardianResponse(err)
|
2017-05-08 08:35:34 -05:00
|
|
|
}
|
|
|
|
|
2021-04-28 07:42:18 -05:00
|
|
|
acl, err := g.GetACLWithoutDuplicates()
|
2017-06-22 16:10:43 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get dashboard permissions", err)
|
2017-05-08 08:35:34 -05:00
|
|
|
}
|
|
|
|
|
2020-11-24 05:10:32 -06:00
|
|
|
filteredAcls := make([]*models.DashboardAclInfoDTO, 0, len(acl))
|
2018-02-05 07:28:24 -06:00
|
|
|
for _, perm := range acl {
|
2021-02-12 09:08:18 -06:00
|
|
|
if perm.UserId > 0 && dtos.IsHiddenUser(perm.UserLogin, c.SignedInUser, hs.Cfg) {
|
2020-11-24 05:10:32 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-03-29 04:32:24 -05:00
|
|
|
perm.UserAvatarUrl = dtos.GetGravatarUrl(perm.UserEmail)
|
2018-04-04 08:50:45 -05:00
|
|
|
|
|
|
|
if perm.TeamId > 0 {
|
|
|
|
perm.TeamAvatarUrl = dtos.GetGravatarUrlWithDefault(perm.TeamEmail, perm.Team)
|
|
|
|
}
|
2018-02-05 07:28:24 -06:00
|
|
|
if perm.Slug != "" {
|
2020-03-04 05:57:20 -06:00
|
|
|
perm.Url = models.GetDashboardFolderUrl(perm.IsFolder, perm.Uid, perm.Slug)
|
2018-02-05 07:28:24 -06:00
|
|
|
}
|
2020-11-24 05:10:32 -06:00
|
|
|
|
|
|
|
filteredAcls = append(filteredAcls, perm)
|
2018-02-05 07:28:24 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, filteredAcls)
|
2017-05-08 08:35:34 -05:00
|
|
|
}
|
2017-05-22 03:36:47 -05:00
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) UpdateDashboardPermissions(c *models.ReqContext, apiCmd dtos.UpdateDashboardAclCommand) response.Response {
|
2020-11-18 08:36:41 -06:00
|
|
|
if err := validatePermissionsUpdate(apiCmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, err.Error(), err)
|
2020-11-18 08:36:41 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
dashID := c.ParamsInt64(":dashboardId")
|
2017-06-19 12:47:44 -05:00
|
|
|
|
2021-06-15 09:08:27 -05:00
|
|
|
_, rsp := getDashboardHelper(c.Req.Context(), c.OrgId, dashID, "")
|
2018-02-12 02:26:09 -06:00
|
|
|
if rsp != nil {
|
|
|
|
return rsp
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
g := guardian.New(dashID, c.OrgId, c.SignedInUser)
|
2018-02-26 13:15:57 -06:00
|
|
|
if canAdmin, err := g.CanAdmin(); err != nil || !canAdmin {
|
2017-06-19 12:47:44 -05:00
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
var items []*models.DashboardAcl
|
2017-06-21 18:02:03 -05:00
|
|
|
for _, item := range apiCmd.Items {
|
2021-03-17 10:06:10 -05:00
|
|
|
items = append(items, &models.DashboardAcl{
|
2020-11-17 10:09:14 -06:00
|
|
|
OrgID: c.OrgId,
|
|
|
|
DashboardID: dashID,
|
|
|
|
UserID: item.UserID,
|
|
|
|
TeamID: item.TeamID,
|
2017-06-21 18:02:03 -05:00
|
|
|
Role: item.Role,
|
|
|
|
Permission: item.Permission,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-24 05:10:32 -06:00
|
|
|
hiddenACL, err := g.GetHiddenACL(hs.Cfg)
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Error while retrieving hidden permissions", err)
|
2020-11-24 05:10:32 -06:00
|
|
|
}
|
2021-03-17 10:06:10 -05:00
|
|
|
items = append(items, hiddenACL...)
|
2020-11-24 05:10:32 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
if okToUpdate, err := g.CheckPermissionBeforeUpdate(models.PERMISSION_ADMIN, items); err != nil || !okToUpdate {
|
2018-01-18 07:30:04 -06:00
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, guardian.ErrGuardianPermissionExists) || errors.Is(err, guardian.ErrGuardianOverride) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, err.Error(), err)
|
2018-02-26 13:15:57 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Error while checking dashboard permissions", err)
|
2018-01-18 07:30:04 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Cannot remove own admin permission for a folder", nil)
|
2018-01-18 07:30:04 -06:00
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
if err := updateDashboardACL(hs, dashID, items); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDashboardAclInfoMissing) ||
|
|
|
|
errors.Is(err, models.ErrDashboardPermissionDashboardEmpty) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(409, err.Error(), err)
|
2017-06-09 14:56:13 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to create permission", err)
|
2017-06-09 14:56:13 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Dashboard permissions updated")
|
2017-06-09 14:56:13 -05:00
|
|
|
}
|
2020-11-18 08:36:41 -06:00
|
|
|
|
|
|
|
func validatePermissionsUpdate(apiCmd dtos.UpdateDashboardAclCommand) error {
|
|
|
|
for _, item := range apiCmd.Items {
|
2020-11-18 09:16:58 -06:00
|
|
|
if (item.UserID > 0 || item.TeamID > 0) && item.Role != nil {
|
2020-11-18 08:36:41 -06:00
|
|
|
return models.ErrPermissionsWithRoleNotAllowed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|