forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_permission_store.go
47 lines (37 loc) · 1.21 KB
/
list_permission_store.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
package rbac
import "github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
type ListPermissionStore struct {
users *permissionIndex
groups *permissionIndex
}
func NewListPermissionStore(client v1.Interface) *ListPermissionStore {
users, groups := newIndexes(client)
return &ListPermissionStore{
users: users,
groups: groups,
}
}
type ListPermissionSet map[ListPermission]bool
func (l ListPermissionSet) HasAccess(namespace, name string) bool {
return l[ListPermission{
Namespace: namespace,
Name: name,
}]
}
type ListPermission struct {
Namespace string
Name string
}
func (l *ListPermissionStore) UserPermissions(subjectName, apiGroup, resource, verb string) ListPermissionSet {
return getFromIndex(subjectName, apiGroup, resource, verb, l.users)
}
func (l *ListPermissionStore) GroupPermissions(subjectName, apiGroup, resource, verb string) ListPermissionSet {
return getFromIndex(subjectName, apiGroup, resource, verb, l.groups)
}
func getFromIndex(subjectName, apiGroup, resource, verb string, index *permissionIndex) ListPermissionSet {
result := ListPermissionSet{}
for _, value := range index.get(subjectName, apiGroup, resource, verb) {
result[value] = true
}
return result
}