2018-02-20 08:25:16 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2018-02-20 08:25:16 -06:00
|
|
|
"time"
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/apierrors"
|
2018-02-20 08:25:16 -06:00
|
|
|
"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"
|
2022-06-30 08:31:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2018-02-20 08:25:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
2020-09-07 10:06:11 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2018-02-20 08:25:16 -06:00
|
|
|
)
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) GetFolderPermissionList(c *models.ReqContext) response.Response {
|
2022-02-16 07:15:44 -06:00
|
|
|
folder, err := hs.folderService.GetFolderByUID(c.Req.Context(), c.SignedInUser, c.OrgId, web.Params(c.Req)[":uid"])
|
2018-02-20 08:25:16 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2021-08-25 08:11:22 -05:00
|
|
|
return apierrors.ToFolderErrorResponse(err)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2021-09-23 10:43:32 -05:00
|
|
|
g := guardian.New(c.Req.Context(), folder.Id, c.OrgId, c.SignedInUser)
|
2018-02-20 08:25:16 -06:00
|
|
|
|
2018-02-26 13:14:21 -06:00
|
|
|
if canAdmin, err := g.CanAdmin(); err != nil || !canAdmin {
|
2022-06-30 08:31:54 -05:00
|
|
|
return apierrors.ToFolderErrorResponse(dashboards.ErrFolderAccessDenied)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
acl, err := g.GetACL()
|
2018-02-20 08:25:16 -06:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get folder permissions", err)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
filteredACLs := make([]*models.DashboardACLInfoDTO, 0, len(acl))
|
2018-02-20 08:25:16 -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-02-20 08:25:16 -06:00
|
|
|
perm.FolderId = folder.Id
|
|
|
|
perm.DashboardId = 0
|
|
|
|
|
2018-04-04 08:51:19 -05:00
|
|
|
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 != "" {
|
2020-03-04 05:57:20 -06:00
|
|
|
perm.Url = models.GetDashboardFolderUrl(perm.IsFolder, perm.Uid, perm.Slug)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
2020-11-24 05:10:32 -06:00
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
filteredACLs = append(filteredACLs, perm)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
return response.JSON(http.StatusOK, filteredACLs)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) UpdateFolderPermissions(c *models.ReqContext) response.Response {
|
2022-07-18 08:14:58 -05:00
|
|
|
apiCmd := dtos.UpdateDashboardACLCommand{}
|
2021-11-29 03:18:01 -06:00
|
|
|
if err := web.Bind(c.Req, &apiCmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-02-16 07:15:44 -06:00
|
|
|
folder, err := hs.folderService.GetFolderByUID(c.Req.Context(), c.SignedInUser, c.OrgId, web.Params(c.Req)[":uid"])
|
2018-02-20 08:25:16 -06:00
|
|
|
if err != nil {
|
2021-08-25 08:11:22 -05:00
|
|
|
return apierrors.ToFolderErrorResponse(err)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2021-09-23 10:43:32 -05:00
|
|
|
g := guardian.New(c.Req.Context(), folder.Id, c.OrgId, c.SignedInUser)
|
2018-02-26 13:14:21 -06:00
|
|
|
canAdmin, err := g.CanAdmin()
|
2018-02-20 11:11:50 -06:00
|
|
|
if err != nil {
|
2021-08-25 08:11:22 -05:00
|
|
|
return apierrors.ToFolderErrorResponse(err)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 11:11:50 -06:00
|
|
|
if !canAdmin {
|
2022-06-30 08:31:54 -05:00
|
|
|
return apierrors.ToFolderErrorResponse(dashboards.ErrFolderAccessDenied)
|
2018-02-20 11:11:50 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
var items []*models.DashboardACL
|
2018-02-20 08:25:16 -06:00
|
|
|
for _, item := range apiCmd.Items {
|
2022-07-18 08:14:58 -05:00
|
|
|
items = append(items, &models.DashboardACL{
|
2020-11-17 10:09:14 -06:00
|
|
|
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(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-02-20 08:25:16 -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:14:21 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Error while checking folder permissions", err)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Cannot remove own admin permission for a folder", nil)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2022-04-25 03:42:09 -05:00
|
|
|
if !hs.AccessControl.IsDisabled() {
|
2022-07-18 08:14:58 -05:00
|
|
|
old, err := g.GetACL()
|
2022-03-03 08:05:47 -06:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Error while checking dashboard permissions", err)
|
|
|
|
}
|
2022-03-30 08:14:26 -05:00
|
|
|
if err := hs.updateDashboardAccessControl(c.Req.Context(), c.OrgId, folder.Uid, true, items, old); err != nil {
|
2022-03-03 08:05:47 -06:00
|
|
|
return response.Error(500, "Failed to create permission", err)
|
|
|
|
}
|
|
|
|
return response.Success("Dashboard permissions updated")
|
|
|
|
}
|
|
|
|
|
2022-07-06 13:42:39 -05:00
|
|
|
if err := hs.DashboardService.UpdateDashboardACL(c.Req.Context(), folder.Id, items); err != nil {
|
2022-07-18 08:14:58 -05:00
|
|
|
if errors.Is(err, models.ErrDashboardACLInfoMissing) {
|
|
|
|
err = models.ErrFolderACLInfoMissing
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDashboardPermissionDashboardEmpty) {
|
2020-03-04 05:57:20 -06:00
|
|
|
err = models.ErrFolderPermissionFolderEmpty
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
if errors.Is(err, models.ErrFolderACLInfoMissing) || errors.Is(err, models.ErrFolderPermissionFolderEmpty) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(409, err.Error(), err)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to create permission", err)
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, util.DynMap{
|
2020-09-07 10:06:11 -05:00
|
|
|
"message": "Folder permissions updated",
|
|
|
|
"id": folder.Id,
|
|
|
|
"title": folder.Title,
|
|
|
|
})
|
2018-02-20 08:25:16 -06:00
|
|
|
}
|