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 (
2022-03-30 08:14:26 -05:00
ScopeFoldersRoot = "folders"
ScopeFoldersPrefix = "folders:uid:"
2022-03-09 10:57:50 -06:00
ActionFoldersCreate = "folders:create"
ActionFoldersRead = "folders:read"
ActionFoldersWrite = "folders:write"
ActionFoldersDelete = "folders:delete"
ActionFoldersPermissionsRead = "folders.permissions:read"
ActionFoldersPermissionsWrite = "folders.permissions:write"
2022-03-30 08:14:26 -05:00
ScopeDashboardsRoot = "dashboards"
ScopeDashboardsPrefix = "dashboards:uid:"
2022-03-09 10:57:50 -06:00
)
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
2022-05-02 02:29:30 -05:00
// NewFolderNameScopeResolver provides an ScopeAttributeResolver that is able to convert a scope prefixed with "folders:name:" into an uid based scope.
func NewFolderNameScopeResolver ( db Store ) ( string , ac . ScopeAttributeResolver ) {
2022-03-10 11:19:50 -06:00
prefix := ScopeFoldersProvider . GetResourceScopeName ( "" )
2022-05-02 02:29:30 -05:00
return prefix , ac . ScopeAttributeResolverFunc ( func ( ctx context . Context , orgID int64 , scope string ) ( [ ] string , error ) {
2022-03-10 11:19:50 -06:00
if ! strings . HasPrefix ( scope , prefix ) {
2022-05-02 02:29:30 -05:00
return nil , ac . ErrInvalidScope
2022-03-10 11:19:50 -06:00
}
nsName := scope [ len ( prefix ) : ]
if len ( nsName ) == 0 {
2022-05-02 02:29:30 -05:00
return nil , ac . ErrInvalidScope
2022-03-10 11:19:50 -06:00
}
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 {
2022-05-02 02:29:30 -05:00
return nil , err
2022-03-10 11:19:50 -06:00
}
2022-05-02 02:29:30 -05:00
return [ ] string { ScopeFoldersProvider . GetResourceScopeUID ( folder . Uid ) } , nil
} )
2022-03-10 11:19:50 -06:00
}
2022-03-15 09:37:16 -05:00
2022-05-02 02:29:30 -05:00
// NewFolderIDScopeResolver provides an ScopeAttributeResolver that is able to convert a scope prefixed with "folders:id:" into an uid based scope.
func NewFolderIDScopeResolver ( db Store ) ( string , ac . ScopeAttributeResolver ) {
2022-03-30 08:14:26 -05:00
prefix := ScopeFoldersProvider . GetResourceScope ( "" )
2022-05-02 02:29:30 -05:00
return prefix , ac . ScopeAttributeResolverFunc ( func ( ctx context . Context , orgID int64 , scope string ) ( [ ] string , error ) {
2022-03-15 09:37:16 -05:00
if ! strings . HasPrefix ( scope , prefix ) {
2022-05-02 02:29:30 -05:00
return nil , ac . ErrInvalidScope
2022-03-15 09:37:16 -05:00
}
2022-03-30 08:14:26 -05:00
id , err := strconv . ParseInt ( scope [ len ( prefix ) : ] , 10 , 64 )
if err != nil {
2022-05-02 02:29:30 -05:00
return nil , ac . ErrInvalidScope
2022-03-15 09:37:16 -05:00
}
2022-03-30 08:14:26 -05:00
if id == 0 {
2022-05-02 02:29:30 -05:00
return [ ] string { ScopeFoldersProvider . GetResourceScopeUID ( ac . GeneralFolderUID ) } , nil
2022-03-30 08:14:26 -05:00
}
folder , err := db . GetFolderByID ( ctx , orgID , id )
2022-03-15 09:37:16 -05:00
if err != nil {
2022-05-02 02:29:30 -05:00
return nil , err
2022-03-15 09:37:16 -05:00
}
2022-03-30 08:14:26 -05:00
2022-05-02 02:29:30 -05:00
return [ ] string { ScopeFoldersProvider . GetResourceScopeUID ( folder . Uid ) } , nil
} )
2022-03-15 09:37:16 -05:00
}