This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrights.go
326 lines (286 loc) · 7.45 KB
/
rights.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package aci
/*
rights.go contains methods pertaining to the use of discrete privilege identifiers.
*/
/*
Right constants are discrete left-shifted privilege aggregates that can be used in an additive (or subtractive) manner to form a complete [Permission].
*/
const (
ReadAccess Right = 1 << iota // 1
WriteAccess // 2
AddAccess // 4
DeleteAccess // 8
SearchAccess // 16
CompareAccess // 32
SelfWriteAccess // 64
ProxyAccess // 128
ImportAccess // 256
ExportAccess // 512
NoAccess Right = 0
AllAccess Right = 895 // DOES NOT INCLUDE "proxy"
)
const badPerm = `<invalid_permission>`
var badPermission Permission
var rightsMap map[Right]string
var rightsNames map[string]Right
/*
Right contains the specific bit value of a single user privilege. Constants of this type are intended for submission to the [Permission.Shift], [Permission.Unshift] and [Permission.Positive] methods.
*/
type Right uint16
/*
Permission defines a level of access bestowed (or withheld) by a [PermissionBindRule].
*/
type Permission struct {
*permission
}
type permission struct {
*bool
*rights
}
/*
Allow returns a granting [Permission] instance bearing the provided instances of [Right].
*/
func Allow(x ...any) Permission {
return Permission{newPermission(true, x...)}
}
/*
Deny returns a withholding [Permission] instance bearing the provided instances of [Right].
*/
func Deny(x ...any) Permission {
return Permission{newPermission(false, x...)}
}
/*
newPermission returns a newly initialized instance of *permission bearing the provided disposition and Right instance(s).
*/
func newPermission(disp bool, x ...any) (p *permission) {
p = new(permission)
p.bool = &disp
p.rights = newRights()
p.shift(x...)
return p
}
func (r *permission) shift(x ...any) {
if !r.isZero() {
// iterate through the sequence of "anys"
// and assert to a Right (or the abstraction
// of a Right).
for i := 0; i < len(x); i++ {
switch tv := x[i].(type) {
case int, Right:
r.rights.cast().Shift(tv)
case string:
if priv, found := rightsNames[lc(tv)]; found {
r.rights.cast().Shift(priv)
}
}
}
}
}
func (r *permission) unshift(x ...any) {
if !r.isZero() {
// iterate through the sequence of "anys"
// and assert to a Right (or the abstraction
// of a Right).
for i := 0; i < len(x); i++ {
switch tv := x[i].(type) {
case int, Right:
r.rights.cast().Unshift(tv)
case string:
if priv, found := rightsNames[lc(tv)]; found {
r.rights.cast().Unshift(priv)
}
}
}
}
}
func (r *permission) positive(x any) (posi bool) {
if !r.isZero() {
switch tv := x.(type) {
case int:
if posi = tv == 0 && r.rights.cast().Int() == tv; posi {
break
}
posi = r.rights.cast().Positive(tv)
case string:
if priv, found := rightsNames[lc(tv)]; found {
posi = r.positive(priv)
}
case Right:
posi = r.positive(int(tv))
}
}
return
}
/*
String is a stringer method that returns a single string name value for receiver instance.
*/
func (r Right) String() (p string) {
switch r {
case NoAccess:
return rightsMap[0]
case AllAccess:
return rightsMap[895]
}
if kw, found := rightsMap[r]; found {
p = kw
}
return
}
/*
Compare returns a Boolean value indicative of a SHA-1 comparison between the receiver (r) and input value x.
*/
func (r Right) Compare(x any) bool {
return compareHashInstance(r, x)
}
/*
Len returns the abstract integer length of the receiver, quantifying the number of [Right] instances currently being expressed. For example, if the receiver instance has its [ReadAccess] and [DeleteAccess] [Right] bits enabled, this would represent an abstract length of two (2).
*/
func (r Permission) Len() (l int) {
if !r.IsZero() {
l = r.permission.len()
}
return
}
func (r permission) len() int {
var D int
for i := 0; i < r.rights.cast().Size(); i++ {
if d := Right(1 << i); r.rights.cast().Positive(d) {
D++
}
}
return D
}
/*
String is a stringer method that returns the string representation of the receiver instance.
*/
func (r Permission) String() string {
if r.IsZero() {
return badPerm
}
pint := r.permission.rights.cast().Int()
var rights []string
if Right(pint) == AllAccess {
rights = append(rights, AllAccess.String())
return r.sprintf(rights)
} else if pint == 1023 {
rights = append(rights, AllAccess.String())
rights = append(rights, ProxyAccess.String())
return r.sprintf(rights)
} else if Right(pint) == NoAccess {
rights = append(rights, NoAccess.String())
return r.sprintf(rights)
}
size := r.permission.rights.cast().Size()
for i := 0; i < size; i++ {
if right := Right(1 << i); r.Positive(right) {
rights = append(rights, right.String())
}
}
return r.sprintf(rights)
}
/*
Compare returns a Boolean value indicative of a SHA-1 comparison between the receiver (r) and input value x.
*/
func (r Permission) Compare(x any) bool {
return compareHashInstance(r, x)
}
func (r Permission) sprintf(rights []string) string {
return sprintf("%s(%s)", r.Disposition(), join(rights, `,`))
}
/*
Disposition returns the string disposition `allow` or 'deny', depending on the state of the receiver.
*/
func (r Permission) Disposition() string {
if r.permission == nil {
return `<unknown_disposition>`
}
return r.permission.disposition()
}
func (r permission) disposition() (disp string) {
disp = `<unknown_disposition>`
if *r.bool {
disp = `allow`
} else if !*r.bool {
disp = `deny`
}
return
}
/*
Positive returns a Boolean value indicative of whether a particular bit is positive (is set). Negation implies negative, or unset.
*/
func (r Permission) Positive(x any) (posi bool) {
if err := r.Valid(); err == nil {
posi = r.permission.positive(x)
}
return
}
/*
Shift left-shifts the receiver instance to include [Right] x, if not already present.
*/
func (r Permission) Shift(x ...any) Permission {
if err := r.Valid(); err == nil {
for i := 0; i < len(x); i++ {
r.permission.shift(x[i]) //rights.cast().Shift(x[i])
}
}
return r
}
/*
Unshift right-shifts the receiver instance to remove [Right] x, if present.
*/
func (r Permission) Unshift(x ...any) Permission {
if err := r.Valid(); err == nil {
for i := 0; i < len(x); i++ {
r.permission.unshift(x[i]) //rights.cast().Unshift(x[i])
}
}
return r
}
/*
IsZero returns a Boolean value indicative of whether the receiver is nil, or unset.
*/
func (r Permission) IsZero() bool {
return r.permission.isZero()
}
func (r *permission) isZero() bool {
if r == nil {
return true
}
return r.bool == nil && r.rights == nil
}
/*
Valid returns a non-error instance if the receiver fails to pass basic validity checks.
*/
func (r Permission) Valid() (err error) {
err = nilInstanceErr(r)
if !r.IsZero() {
err = noPermissionDispErr()
if r.permission.bool != nil {
err = nil
}
}
return
}
func init() {
rightsMap = map[Right]string{
NoAccess: `none`,
ReadAccess: `read`,
WriteAccess: `write`,
AddAccess: `add`,
DeleteAccess: `delete`,
SearchAccess: `search`,
CompareAccess: `compare`,
SelfWriteAccess: `selfwrite`,
AllAccess: `all`,
ProxyAccess: `proxy`,
ImportAccess: `import`,
ExportAccess: `export`,
}
// we want to resolve the name
// of a Right into an actual
// Right instance.
rightsNames = make(map[string]Right, 0)
for k, v := range rightsMap {
rightsNames[v] = k
}
}