forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
57 lines (47 loc) · 1.16 KB
/
checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package accesscontrol
import (
"github.com/grafana/grafana/pkg/services/user"
)
func Checker(user *user.SignedInUser, action string) func(scopes ...string) bool {
if user.Permissions == nil || user.Permissions[user.OrgID] == nil {
return func(scopes ...string) bool { return false }
}
userScopes, ok := user.Permissions[user.OrgID][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)
}