2018-02-20 08:25:16 -06:00
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-24 08:37:54 -05:00
|
|
|
|
"context"
|
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"
|
2024-01-24 05:39:11 -06:00
|
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2023-08-24 08:37:54 -05:00
|
|
|
|
"github.com/grafana/grafana/pkg/services/auth/identity"
|
2023-01-27 01:50:36 -06:00
|
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-06-30 08:31:54 -05:00
|
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2023-11-22 07:20:22 -06:00
|
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards/dashboardaccess"
|
2022-11-11 07:28:24 -06:00
|
|
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
2023-08-24 08:37:54 -05:00
|
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
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
|
2023-01-27 01:50:36 -06:00
|
|
|
|
func (hs *HTTPServer) GetFolderPermissionList(c *contextmodel.ReqContext) response.Response {
|
2022-11-11 07:28:24 -06:00
|
|
|
|
uid := web.Params(c.Req)[":uid"]
|
2023-10-06 04:34:36 -05:00
|
|
|
|
folder, err := hs.folderService.Get(c.Req.Context(), &folder.GetFolderQuery{OrgID: c.SignedInUser.GetOrgID(), 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
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
|
acl, err := hs.getFolderACL(c.Req.Context(), c.SignedInUser, folder)
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 07:58:47 -06:00
|
|
|
|
filteredACLs := make([]*dashboards.DashboardACLInfoDTO, 0, len(acl))
|
2018-02-20 08:25:16 -06:00
|
|
|
|
for _, perm := range acl {
|
2023-01-20 07:58:47 -06:00
|
|
|
|
if perm.UserID > 0 && dtos.IsHiddenUser(perm.UserLogin, c.SignedInUser, hs.Cfg) {
|
2020-11-24 05:10:32 -06:00
|
|
|
|
continue
|
|
|
|
|
}
|
2024-01-24 05:39:11 -06:00
|
|
|
|
metrics.MFolderIDsAPICount.WithLabelValues(metrics.GetFolderPermissionList).Inc()
|
2023-11-15 09:29:20 -06:00
|
|
|
|
// nolint:staticcheck
|
2023-01-20 07:58:47 -06:00
|
|
|
|
perm.FolderID = folder.ID
|
|
|
|
|
perm.DashboardID = 0
|
2018-02-20 08:25:16 -06:00
|
|
|
|
|
2024-01-23 05:36:22 -06:00
|
|
|
|
perm.UserAvatarURL = dtos.GetGravatarUrl(hs.Cfg, perm.UserEmail)
|
2018-04-04 08:51:19 -05:00
|
|
|
|
|
2023-01-20 07:58:47 -06:00
|
|
|
|
if perm.TeamID > 0 {
|
2024-01-23 05:36:22 -06:00
|
|
|
|
perm.TeamAvatarURL = dtos.GetGravatarUrlWithDefault(hs.Cfg, perm.TeamEmail, perm.Team)
|
2018-04-04 08:51:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 08:25:16 -06:00
|
|
|
|
if perm.Slug != "" {
|
2023-01-20 07:58:47 -06:00
|
|
|
|
perm.URL = dashboards.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
|
2023-01-27 01:50:36 -06:00
|
|
|
|
func (hs *HTTPServer) UpdateFolderPermissions(c *contextmodel.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"]
|
2023-10-06 04:34:36 -05:00
|
|
|
|
folder, err := hs.folderService.Get(c.Req.Context(), &folder.GetFolderQuery{OrgID: c.SignedInUser.GetOrgID(), 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
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 07:58:47 -06:00
|
|
|
|
items := make([]*dashboards.DashboardACL, 0, len(apiCmd.Items))
|
2018-02-20 08:25:16 -06:00
|
|
|
|
for _, item := range apiCmd.Items {
|
2023-01-20 07:58:47 -06:00
|
|
|
|
items = append(items, &dashboards.DashboardACL{
|
2023-10-06 04:34:36 -05:00
|
|
|
|
OrgID: c.SignedInUser.GetOrgID(),
|
2023-11-20 14:44:51 -06:00
|
|
|
|
DashboardID: folder.ID, // nolint:staticcheck
|
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(),
|
|
|
|
|
})
|
2024-01-24 05:39:11 -06:00
|
|
|
|
metrics.MFolderIDsAPICount.WithLabelValues(metrics.UpdateFolderPermissions).Inc()
|
2018-02-20 08:25:16 -06:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
|
acl, err := hs.getFolderACL(c.Req.Context(), c.SignedInUser, folder)
|
2020-11-24 05:10:32 -06:00
|
|
|
|
if err != nil {
|
2023-08-24 08:37:54 -05:00
|
|
|
|
return response.Error(http.StatusInternalServerError, "Error while checking folder permissions", err)
|
2020-11-24 05:10:32 -06:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
|
items = append(items, hs.filterHiddenACL(c.SignedInUser, acl)...)
|
2018-02-20 08:25:16 -06:00
|
|
|
|
|
2023-10-06 04:34:36 -05:00
|
|
|
|
if err := hs.updateDashboardAccessControl(c.Req.Context(), c.SignedInUser.GetOrgID(), folder.UID, true, items, acl); err != nil {
|
2023-08-24 08:37:54 -05:00
|
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to create permission", err)
|
2018-02-20 08:25:16 -06:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
|
return response.Success("Folder permissions updated")
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-22 07:20:22 -06:00
|
|
|
|
var folderPermissionMap = map[string]dashboardaccess.PermissionType{
|
|
|
|
|
"View": dashboardaccess.PERMISSION_VIEW,
|
|
|
|
|
"Edit": dashboardaccess.PERMISSION_EDIT,
|
|
|
|
|
"Admin": dashboardaccess.PERMISSION_ADMIN,
|
2023-08-24 08:37:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (hs *HTTPServer) getFolderACL(ctx context.Context, user identity.Requester, folder *folder.Folder) ([]*dashboards.DashboardACLInfoDTO, error) {
|
|
|
|
|
permissions, err := hs.folderPermissionsService.GetPermissions(ctx, user, folder.UID)
|
2023-07-17 11:21:01 -05:00
|
|
|
|
if err != nil {
|
2023-08-24 08:37:54 -05:00
|
|
|
|
return nil, err
|
2022-03-03 08:05:47 -06:00
|
|
|
|
}
|
2023-08-24 08:37:54 -05:00
|
|
|
|
|
|
|
|
|
acl := make([]*dashboards.DashboardACLInfoDTO, 0, len(permissions))
|
|
|
|
|
for _, p := range permissions {
|
|
|
|
|
if !p.IsManaged {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var role *org.RoleType
|
|
|
|
|
if p.BuiltInRole != "" {
|
|
|
|
|
tmp := org.RoleType(p.BuiltInRole)
|
|
|
|
|
role = &tmp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
permission := folderPermissionMap[hs.folderPermissionsService.MapActions(p)]
|
|
|
|
|
|
|
|
|
|
acl = append(acl, &dashboards.DashboardACLInfoDTO{
|
|
|
|
|
OrgID: folder.OrgID,
|
2023-11-20 14:44:51 -06:00
|
|
|
|
DashboardID: folder.ID, // nolint:staticcheck
|
2023-08-24 08:37:54 -05:00
|
|
|
|
FolderUID: folder.ParentUID,
|
|
|
|
|
Created: p.Created,
|
|
|
|
|
Updated: p.Updated,
|
|
|
|
|
UserID: p.UserId,
|
|
|
|
|
UserLogin: p.UserLogin,
|
|
|
|
|
UserEmail: p.UserEmail,
|
|
|
|
|
TeamID: p.TeamId,
|
|
|
|
|
TeamEmail: p.TeamEmail,
|
|
|
|
|
Team: p.Team,
|
|
|
|
|
Role: role,
|
|
|
|
|
Permission: permission,
|
|
|
|
|
PermissionName: permission.String(),
|
|
|
|
|
UID: folder.UID,
|
|
|
|
|
Title: folder.Title,
|
|
|
|
|
URL: folder.WithURL().URL,
|
|
|
|
|
IsFolder: true,
|
|
|
|
|
Inherited: false,
|
|
|
|
|
})
|
2024-01-24 05:39:11 -06:00
|
|
|
|
metrics.MFolderIDsAPICount.WithLabelValues(metrics.GetFolderPermissionList).Inc()
|
2018-02-20 08:25:16 -06:00
|
|
|
|
}
|
2023-08-24 08:37:54 -05:00
|
|
|
|
|
|
|
|
|
return acl, nil
|
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
|
2023-01-20 07:58:47 -06:00
|
|
|
|
Body []*dashboards.DashboardACLInfoDTO `json:"body"`
|
2022-07-27 08:54:37 -05:00
|
|
|
|
}
|