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"
|
2022-11-11 07:28:24 -06:00
|
|
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
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
|
|
|
|
)
|
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
|
// swagger:route GET /folders/{folder_uid}/permissions folder_permissions getFolderPermissionList
|
|
|
|
|
//
|
|
|
|
|
// Gets all existing permissions for the folder with the given `uid`.
|
|
|
|
|
//
|
|
|
|
|
// Responses:
|
|
|
|
|
// 200: getFolderPermissionListResponse
|
|
|
|
|
// 401: unauthorisedError
|
|
|
|
|
// 403: forbiddenError
|
|
|
|
|
// 404: notFoundError
|
|
|
|
|
// 500: internalServerError
|
2021-01-15 07:43:20 -06:00
|
|
|
|
func (hs *HTTPServer) GetFolderPermissionList(c *models.ReqContext) response.Response {
|
2022-11-11 07:28:24 -06:00
|
|
|
|
uid := web.Params(c.Req)[":uid"]
|
2022-11-23 03:13:47 -06:00
|
|
|
|
folder, err := hs.folderService.Get(c.Req.Context(), &folder.GetFolderQuery{OrgID: c.OrgID, UID: &uid, SignedInUser: c.SignedInUser})
|
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
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 07:28:24 -06: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
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 07:28:24 -06:00
|
|
|
|
perm.FolderId = folder.ID
|
2018-02-20 08:25:16 -06:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
|
// swagger:route POST /folders/{folder_uid}/permissions folder_permissions updateFolderPermissions
|
|
|
|
|
//
|
|
|
|
|
// Updates permissions for a folder. This operation will remove existing permissions if they’re not included in the request.
|
|
|
|
|
//
|
|
|
|
|
// Responses:
|
|
|
|
|
// 200: okResponse
|
|
|
|
|
// 401: unauthorisedError
|
|
|
|
|
// 403: forbiddenError
|
|
|
|
|
// 404: notFoundError
|
|
|
|
|
// 500: internalServerError
|
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-11-11 07:28:24 -06:00
|
|
|
|
uid := web.Params(c.Req)[":uid"]
|
2022-11-23 03:13:47 -06:00
|
|
|
|
folder, err := hs.folderService.Get(c.Req.Context(), &folder.GetFolderQuery{OrgID: c.OrgID, UID: &uid, SignedInUser: c.SignedInUser})
|
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
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 07:28:24 -06: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-11-28 06:10:24 -06:00
|
|
|
|
items := make([]*models.DashboardACL, 0, len(apiCmd.Items))
|
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{
|
2022-08-11 06:28:55 -05:00
|
|
|
|
OrgID: c.OrgID,
|
2022-11-11 07:28:24 -06:00
|
|
|
|
DashboardID: folder.ID,
|
2020-11-17 10:09:14 -06:00
|
|
|
|
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-11-11 07:28:24 -06: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-11-11 07:28:24 -06: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",
|
2022-11-11 07:28:24 -06:00
|
|
|
|
"id": folder.ID,
|
2020-09-07 10:06:11 -05:00
|
|
|
|
"title": folder.Title,
|
|
|
|
|
})
|
2018-02-20 08:25:16 -06:00
|
|
|
|
}
|
2022-07-27 08:54:37 -05:00
|
|
|
|
|
|
|
|
|
// swagger:parameters getFolderPermissionList
|
|
|
|
|
type GetFolderPermissionListParams struct {
|
|
|
|
|
// in:path
|
|
|
|
|
// required:true
|
|
|
|
|
FolderUID string `json:"folder_uid"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// swagger:parameters updateFolderPermissions
|
|
|
|
|
type UpdateFolderPermissionsParams struct {
|
|
|
|
|
// in:path
|
|
|
|
|
// required:true
|
|
|
|
|
FolderUID string `json:"folder_uid"`
|
|
|
|
|
// in:body
|
|
|
|
|
// required:true
|
|
|
|
|
Body dtos.UpdateDashboardACLCommand
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// swagger:response getFolderPermissionListResponse
|
|
|
|
|
type GetFolderPermissionsResponse struct {
|
|
|
|
|
// in: body
|
|
|
|
|
Body []*models.DashboardACLInfoDTO `json:"body"`
|
|
|
|
|
}
|