-
Notifications
You must be signed in to change notification settings - Fork 2
/
role.go
174 lines (148 loc) · 4.1 KB
/
role.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
// Copyright 2018 Marc Binder. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package acl
import (
"sort"
)
// ExaminerFunc a function to determine whether a role can be added to a `ResultSet`.
type ExaminerFunc = func(payload interface{}) bool
// NewRole returns a new role instance.
func NewRole(id string) *Role {
return &Role{
Id: id,
}
}
// Role contains all necessary information about one set of granted rights.
//
// Each role requires an identifier. It is possible to define multiple roles with the
// same identifier as long as each manager contains an unique set of identifiers.
type Role struct {
Id string
granted []string
examiner ExaminerFunc
}
// Grant adds the given right(s) to the role.
//
// func main() {
// r := NewRole("a")
// r.Grant("right.a", "right.b")
// }
//
// Note, that duplications will be ignored.
func (role *Role) Grant(rights ...string) *Role {
var granted []string
for _, right := range rights {
if determineIndex(role.granted, right) == -1 {
granted = append(granted, right)
}
}
if len(granted) > 0 {
role.granted = append(role.granted, granted...)
// to use sort.SearchStrings, the slice must be sorted in ascending order
sort.Strings(role.granted)
}
return role
}
// Revoke removes the given right(s) from the role.
//
// func main() {
// r := NewRole("a")
// r.Grant("right.a", "right.b")
// r.Revoke("right.a")
// }
func (role *Role) Revoke(rights ...string) *Role {
for _, right := range rights {
if index := determineIndex(role.granted, right); index >= 0 {
role.granted = append(role.granted[:index], role.granted[index+1:]...)
}
}
return role
}
// AcquireFrom grabs the rights from the given roles to add them.
//
// func main() {
// r1 := NewRole("r1").Grant("right.a")
// r2 := NewRole("r2").AcquireFrom(r1).Grant("right.b")
// }
func (role *Role) AcquireFrom(roles ...*Role) *Role {
for _, ar := range roles {
role.Grant(ar.granted...)
}
return role
}
// Has checks that the given right has been granted.
//
// To resolve whether a right is available or not, the function uses a binary search to
// determine the actual index of the given right(s). Therefore, the array of granted
// rights is always sorted alphabetically.
func (role *Role) Has(right string) bool {
return determineIndex(role.granted, right) >= 0
}
// HasOneOf checks that at least one of the given rights has been granted.
//
// func main() {
// r := NewRole("r")
// r.Grant("right.a")
// r.HasOneOf("right.a", "right.b")
// }
func (role *Role) HasOneOf(rights ...string) bool {
for _, right := range rights {
if determineIndex(role.granted, right) >= 0 {
return true
}
}
return false
}
// HasAllOf verifies that all specified rights are present.
//
// func main() {
// r := NewRole("r").Grant("a", "b", "c")
// r.HasAllOf("a", b", "c")
// }
func (role *Role) HasAllOf(rights ...string) bool {
registry := map[string]bool{}
total := len(rights)
resolve := func(right string) bool {
if !registry[right] {
total--
}
registry[right] = true
return total == 0
}
for _, right := range rights {
if role.Has(right) && resolve(right) {
return true
}
}
return false
}
// SetExaminer sets / overwrites the examiner.
//
// The examiner is used to determine whether a role can be added to a `ResultSet`.
//
// type User struct {
// isAdmin bool
// }
//
// func main() {
// r := NewRole("admin").Grant("godmode").SetExaminer(func (payload interface{}) bool {
// user := payload.(User)
// return user.isAdmin
// })
//
// rs := NewManager().Register(r).Examine(User{isAdmin: true})
// }
func (role *Role) SetExaminer(examiner ExaminerFunc) *Role {
role.examiner = examiner
return role
}
// examine calls the examiner function to determine if a role can be added to a `ResultSet`.
//
// Without examiner, the function will always return false.
func (role *Role) examine(payload interface{}) bool {
if role.examiner == nil {
return false
}
return role.examiner(payload)
}