Files
Karl Persson 2e38329026 RBAC: Add required component to perform access control checks for user api when running single tenant (#93104)
* Unexport store and create new constructor function

* Add ResourceAuthorizer and LegacyAccessClient

* Configure checks for user store

* List with checks if AccessClient is configured

* Allow system user service account to read all users

---------

Co-authored-by: Gabriel MABILLE <gamab@users.noreply.github.com>
2024-09-23 11:26:44 +02:00

55 lines
1.1 KiB
Go

package accesscontrol
import (
"github.com/grafana/grafana/pkg/apimachinery/identity"
)
func Checker(user identity.Requester, action string) func(scopes ...string) bool {
permissions := user.GetPermissions()
userScopes, ok := permissions[action]
if !ok {
return func(scopes ...string) bool { return false }
}
lookup := make(map[string]bool, len(userScopes))
for i := range userScopes {
lookup[userScopes[i]] = true
}
var checkedWildcards bool
var hasWildcard bool
return func(scopes ...string) bool {
if !checkedWildcards {
wildcards := wildcardsFromScopes(scopes...)
for _, w := range wildcards {
if _, ok := lookup[w]; ok {
hasWildcard = true
break
}
}
checkedWildcards = true
}
if hasWildcard {
return true
}
for _, s := range scopes {
if lookup[s] {
return true
}
}
return false
}
}
func wildcardsFromScopes(scopes ...string) Wildcards {
prefixes := make([]string, len(scopes))
for _, scope := range scopes {
prefixes = append(prefixes, ScopePrefix(scope))
}
return WildcardsFromPrefixes(prefixes)
}