2022-03-09 10:57:50 -06:00
|
|
|
package dashboards
|
|
|
|
|
2022-03-10 11:19:50 -06:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
|
|
|
)
|
2022-03-09 10:57:50 -06:00
|
|
|
|
|
|
|
const (
|
|
|
|
ActionFoldersCreate = "folders:create"
|
|
|
|
ActionFoldersRead = "folders:read"
|
|
|
|
ActionFoldersWrite = "folders:write"
|
|
|
|
ActionFoldersDelete = "folders:delete"
|
|
|
|
ActionFoldersPermissionsRead = "folders.permissions:read"
|
|
|
|
ActionFoldersPermissionsWrite = "folders.permissions:write"
|
|
|
|
|
|
|
|
ScopeFoldersRoot = "folders"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-03-10 11:19:50 -06:00
|
|
|
ScopeFoldersAll = ac.GetResourceAllScope(ScopeFoldersRoot)
|
|
|
|
ScopeFoldersProvider = ac.NewScopeProvider(ScopeFoldersRoot)
|
2022-03-09 10:57:50 -06:00
|
|
|
)
|
2022-03-10 11:19:50 -06:00
|
|
|
|
|
|
|
// NewNameScopeResolver provides an AttributeScopeResolver that is able to convert a scope prefixed with "folders:name:" into an id based scope.
|
|
|
|
func NewNameScopeResolver(db Store) (string, ac.AttributeScopeResolveFunc) {
|
|
|
|
prefix := ScopeFoldersProvider.GetResourceScopeName("")
|
|
|
|
resolver := func(ctx context.Context, orgID int64, scope string) (string, error) {
|
|
|
|
if !strings.HasPrefix(scope, prefix) {
|
|
|
|
return "", ac.ErrInvalidScope
|
|
|
|
}
|
|
|
|
nsName := scope[len(prefix):]
|
|
|
|
if len(nsName) == 0 {
|
|
|
|
return "", ac.ErrInvalidScope
|
|
|
|
}
|
2022-03-14 10:21:42 -05:00
|
|
|
folder, err := db.GetFolderByTitle(ctx, orgID, nsName)
|
2022-03-10 11:19:50 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return ScopeFoldersProvider.GetResourceScope(strconv.FormatInt(folder.Id, 10)), nil
|
|
|
|
}
|
|
|
|
return prefix, resolver
|
|
|
|
}
|
2022-03-15 09:37:16 -05:00
|
|
|
|
|
|
|
// NewUidScopeResolver provides an AttributeScopeResolver that is able to convert a scope prefixed with "folders:uid:" into an id based scope.
|
|
|
|
func NewUidScopeResolver(db Store) (string, ac.AttributeScopeResolveFunc) {
|
|
|
|
prefix := ScopeFoldersProvider.GetResourceScopeUID("")
|
|
|
|
resolver := func(ctx context.Context, orgID int64, scope string) (string, error) {
|
|
|
|
if !strings.HasPrefix(scope, prefix) {
|
|
|
|
return "", ac.ErrInvalidScope
|
|
|
|
}
|
|
|
|
uid := scope[len(prefix):]
|
|
|
|
if len(uid) == 0 {
|
|
|
|
return "", ac.ErrInvalidScope
|
|
|
|
}
|
|
|
|
folder, err := db.GetFolderByUID(ctx, orgID, uid)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return ScopeFoldersProvider.GetResourceScope(strconv.FormatInt(folder.Id, 10)), nil
|
|
|
|
}
|
|
|
|
return prefix, resolver
|
|
|
|
}
|