Folder UID scope resolver (#46426)

This commit is contained in:
Yuriy Tseretyan
2022-03-15 15:37:16 +01:00
committed by GitHub
parent f46038ed3a
commit e62e9904ee
4 changed files with 78 additions and 1 deletions
+20
View File
@@ -43,3 +43,23 @@ func NewNameScopeResolver(db Store) (string, ac.AttributeScopeResolveFunc) {
}
return prefix, resolver
}
// 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
}