mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AccessControl: tidy scope resolution (#40677)
* AccessControl: tidy scope resolution
This commit is contained in:
parent
3718494b35
commit
6a3ce8bd38
@ -3,6 +3,8 @@ package accesscontrol
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Scope builds scope from parts
|
// Scope builds scope from parts
|
||||||
@ -29,3 +31,39 @@ func Parameter(key string) string {
|
|||||||
func Field(key string) string {
|
func Field(key string) string {
|
||||||
return fmt.Sprintf(`{{ .%s }}`, key)
|
return fmt.Sprintf(`{{ .%s }}`, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type KeywordScopeResolveFunc func(*models.SignedInUser) (string, error)
|
||||||
|
|
||||||
|
// ScopeResolver contains a map of functions to resolve scope keywords such as `self` or `current` into `id` based scopes
|
||||||
|
type ScopeResolver struct {
|
||||||
|
keywordResolvers map[string]KeywordScopeResolveFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewScopeResolver() ScopeResolver {
|
||||||
|
return ScopeResolver{
|
||||||
|
keywordResolvers: map[string]KeywordScopeResolveFunc{
|
||||||
|
"orgs:current": resolveCurrentOrg,
|
||||||
|
"users:self": resolveUserSelf,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveCurrentOrg(u *models.SignedInUser) (string, error) {
|
||||||
|
return Scope("orgs", "id", fmt.Sprintf("%v", u.OrgId)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveUserSelf(u *models.SignedInUser) (string, error) {
|
||||||
|
return Scope("users", "id", fmt.Sprintf("%v", u.UserId)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveKeyword resolves scope with keywords such as `self` or `current` into `id` based scopes
|
||||||
|
func (s *ScopeResolver) ResolveKeyword(user *models.SignedInUser, permission Permission) (*Permission, error) {
|
||||||
|
if fn, ok := s.keywordResolvers[permission.Scope]; ok {
|
||||||
|
resolvedScope, err := fn(user)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not resolve %v: %v", permission.Scope, err)
|
||||||
|
}
|
||||||
|
permission.Scope = resolvedScope
|
||||||
|
}
|
||||||
|
return &permission, nil
|
||||||
|
}
|
||||||
|
@ -1,43 +0,0 @@
|
|||||||
package accesscontrol
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
type KeywordScopeResolveFunc func(*models.SignedInUser) (string, error)
|
|
||||||
|
|
||||||
// ScopeResolver contains a map of functions to resolve scope keywords such as `self` or `current` into `id` based scopes
|
|
||||||
type ScopeResolver struct {
|
|
||||||
keywordResolvers map[string]KeywordScopeResolveFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewScopeResolver() ScopeResolver {
|
|
||||||
return ScopeResolver{
|
|
||||||
keywordResolvers: map[string]KeywordScopeResolveFunc{
|
|
||||||
"orgs:current": resolveCurrentOrg,
|
|
||||||
"users:self": resolveUserSelf,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveCurrentOrg(u *models.SignedInUser) (string, error) {
|
|
||||||
return Scope("orgs", "id", fmt.Sprintf("%v", u.OrgId)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveUserSelf(u *models.SignedInUser) (string, error) {
|
|
||||||
return Scope("users", "id", fmt.Sprintf("%v", u.UserId)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveKeyword resolves scope with keywords such as `self` or `current` into `id` based scopes
|
|
||||||
func (s *ScopeResolver) ResolveKeyword(user *models.SignedInUser, permission Permission) (*Permission, error) {
|
|
||||||
if fn, ok := s.keywordResolvers[permission.Scope]; ok {
|
|
||||||
resolvedScope, err := fn(user)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("could not resolve %v: %v", permission.Scope, err)
|
|
||||||
}
|
|
||||||
permission.Scope = resolvedScope
|
|
||||||
}
|
|
||||||
return &permission, nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user