-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrbac.go
207 lines (189 loc) · 4.91 KB
/
rbac.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package gorbac
import "sync"
var empty = struct{}{}
type _RBAC struct {
mutex sync.RWMutex
roles Roles
permissions Permissions
parents map[string]map[string]struct{}
}
// SetParents bind `parents` to the role `id`.
// If the role or any of parents is not existing,
// an error will be returned.
func (rbac *_RBAC) SetParents(id string, parents []string) error {
rbac.mutex.Lock()
defer rbac.mutex.Unlock()
if _, ok := rbac.roles[id]; !ok {
return ErrRoleNotExist
}
for _, parent := range parents {
if _, ok := rbac.roles[parent]; !ok {
return ErrRoleNotExist
}
}
if _, ok := rbac.parents[id]; !ok {
rbac.parents[id] = make(map[string]struct{})
}
for _, parent := range parents {
rbac.parents[id][parent] = empty
}
return nil
}
// GetParents return `parents` of the role `id`.
// If the role is not existing, an error will be returned.
// Or the role doesn't have any parents,
// a nil slice will be returned.
func (rbac *_RBAC) GetParents(id string) ([]string, error) {
rbac.mutex.Lock()
defer rbac.mutex.Unlock()
if _, ok := rbac.roles[id]; !ok {
return nil, ErrRoleNotExist
}
ids, ok := rbac.parents[id]
if !ok {
return nil, nil
}
var parents []string
for parent := range ids {
parents = append(parents, parent)
}
return parents, nil
}
// SetParent bind the `parent` to the role `id`.
// If the role or the parent is not existing,
// an error will be returned.
func (rbac *_RBAC) SetParent(id string, parent string) error {
rbac.mutex.Lock()
defer rbac.mutex.Unlock()
if _, ok := rbac.roles[id]; !ok {
return ErrRoleNotExist
}
if _, ok := rbac.roles[parent]; !ok {
return ErrRoleNotExist
}
if _, ok := rbac.parents[id]; !ok {
rbac.parents[id] = make(map[string]struct{})
}
var empty struct{}
rbac.parents[id][parent] = empty
return nil
}
// RemoveParent unbind the `parent` with the role `id`.
// If the role or the parent is not existing,
// an error will be returned.
func (rbac *_RBAC) RemoveParent(id string, parent string) error {
rbac.mutex.Lock()
defer rbac.mutex.Unlock()
if _, ok := rbac.roles[id]; !ok {
return ErrRoleNotExist
}
if _, ok := rbac.roles[parent]; !ok {
return ErrRoleNotExist
}
delete(rbac.parents[id], parent)
return nil
}
// Add a role `r`.
func (rbac *_RBAC) AddRole(r Role) error {
rbac.mutex.Lock()
defer rbac.mutex.Unlock()
if r != nil {
rbac.roles[r.ID()] = r
}
return nil
}
// Remove the role by `id`.
func (rbac *_RBAC) RemoveRole(id string) error {
rbac.mutex.Lock()
defer rbac.mutex.Unlock()
if _, ok := rbac.roles[id]; !ok {
return ErrRoleNotExist
}
delete(rbac.roles, id)
for rid, parents := range rbac.parents {
if rid == id {
delete(rbac.parents, rid)
continue
}
for parent := range parents {
if parent == id {
delete(rbac.parents[rid], id)
break
}
}
}
return nil
}
// Get the role by `id` and a slice of its parents id.
func (rbac *_RBAC) GetRole(id string) (Role, []string, error) {
rbac.mutex.RLock()
defer rbac.mutex.RUnlock()
r, ok := rbac.roles[id]
if !ok {
return nil, nil, ErrRoleNotExist
}
var parents []string
for parent := range rbac.parents[id] {
parents = append(parents, parent)
}
return r, parents, nil
}
// Get the role by `id`.
func (rbac *_RBAC) GetRoleOnly(id string) (Role, error) {
rbac.mutex.RLock()
defer rbac.mutex.RUnlock()
r, ok := rbac.roles[id]
if !ok {
return nil, ErrRoleNotExist
}
return r, nil
}
// IsGranted tests if the role `id` has Permission `p`.
func (rbac *_RBAC) IsGranted(id string, p Permission) bool {
return rbac.IsAssertGranted(id, p, nil)
}
// IsAssertGranted tests if the role `id` has Permission `p` with the condition `assert`.
func (rbac *_RBAC) IsAssertGranted(id string, p Permission, assert AssertionFunc) bool {
rbac.mutex.RLock()
defer rbac.mutex.RUnlock()
return rbac.isGranted(id, p, assert)
}
func (rbac *_RBAC) isGranted(id string, p Permission, assert AssertionFunc) bool {
if assert != nil && !assert(rbac, id, p) {
return false
}
return rbac.recursionCheckID(id, p.ID())
}
// IsGranted tests if the role `id` has Permission id `pid`.
func (rbac *_RBAC) IsGrantedID(id, pid string) bool {
return rbac.IsAssertGrantedID(id, pid, nil)
}
// IsAssertGranted tests if the role `id` has Permission id `pid` with the condition `assert`.
func (rbac *_RBAC) IsAssertGrantedID(id, pid string, assert AssertionIDFunc) bool {
rbac.mutex.RLock()
defer rbac.mutex.RUnlock()
return rbac.isGrantedID(id, pid, assert)
}
func (rbac *_RBAC) isGrantedID(id, pid string, assert AssertionIDFunc) bool {
if assert != nil && !assert(rbac, id, pid) {
return false
}
return rbac.recursionCheckID(id, pid)
}
func (rbac *_RBAC) recursionCheckID(id, pid string) bool {
if role, ok := rbac.roles[id]; ok {
if role.PermitID(pid) {
return true
}
if parents, ok := rbac.parents[id]; ok {
for pID := range parents {
if _, ok := rbac.roles[pID]; ok {
if rbac.recursionCheckID(pID, pid) {
return true
}
}
}
}
}
return false
}