forked from mikespook/gorbac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbac.go
212 lines (193 loc) · 5.01 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
208
209
210
211
212
/*
Package gorbac provides a lightweight role-based access
control implementation in Golang.
For the purposes of this package:
* an identity has one or more roles.
* a role requests access to a permission.
* a permission is given to a role.
Thus, RBAC has the following model:
* many to many relationship between identities and roles.
* many to many relationship between roles and permissions.
* roles can have parent roles.
*/
package gorbac
import (
"errors"
"sync"
)
var (
// ErrRoleNotExist occurred if a role cann't be found
ErrRoleNotExist = errors.New("Role does not exist")
// ErrRoleExist occurred if a role shouldn't be found
ErrRoleExist = errors.New("Role has already existed")
empty = struct{}{}
)
// AssertionFunc supplies more fine-grained permission controls.
type AssertionFunc func(*RBAC, string, Permission) bool
// RBAC object, in most cases it should be used as a singleton.
type RBAC struct {
mutex sync.RWMutex
roles Roles
permissions Permissions
parents map[string]map[string]struct{}
}
// New returns a RBAC structure.
// The default role structure will be used.
func New() *RBAC {
return &RBAC{
roles: make(Roles),
permissions: make(Permissions),
parents: make(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) Add(r Role) error {
rbac.mutex.Lock()
defer rbac.mutex.Unlock()
if _, ok := rbac.roles[r.ID()]; ok {
return ErrRoleExist
}
rbac.roles[r.ID()] = r
return nil
}
// Remove the role by `id`.
func (rbac *RBAC) Remove(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) Get(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
}
// IsGranted tests if the role `id` has Permission `p` with the condition `assert`.
func (rbac *RBAC) IsGranted(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.recursionCheck(id, p)
}
func (rbac *RBAC) recursionCheck(id string, p Permission) bool {
if role, ok := rbac.roles[id]; ok {
if role.Permit(p) {
return true
}
if parents, ok := rbac.parents[id]; ok {
for pID := range parents {
if _, ok := rbac.roles[pID]; ok {
if rbac.recursionCheck(pID, p) {
return true
}
}
}
}
}
return false
}