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