-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresult.go
86 lines (68 loc) · 1.59 KB
/
result.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
package acl
type ResultSet struct {
Matches map[string]*Role
}
// NewResultSet creates a new `ResultSet` instance
func NewResultSet(roles ...*Role) *ResultSet {
matches := map[string]*Role{}
for _, role := range roles {
matches[role.Id] = role
}
return &ResultSet{
Matches: matches,
}
}
// HasRole determines whether a role is present.
func (result *ResultSet) HasRole(id string) bool {
if _, exists := result.Matches[id]; exists {
return true
}
return false
}
// GetRole returns the role with the given identifier or nil.
func (result *ResultSet) GetRole(id string) *Role {
if result.HasRole(id) {
return result.Matches[id]
}
return nil
}
// Has checks that at least one role contains the given right.
func (result *ResultSet) Has(right string) bool {
for _, role := range result.Matches {
if role.Has(right) {
return true
}
}
return false
}
// HasOneOf checks that at least one role contains at least one of the given rights.
func (result *ResultSet) HasOneOf(rights ...string) bool {
for _, right := range rights {
for _, role := range result.Matches {
if role.Has(right) {
return true
}
}
}
return false
}
// HasAllOf verifies that all specified rights are present.
func (result *ResultSet) 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 {
for _, role := range result.Matches {
if role.Has(right) && resolve(right) {
return true
}
}
}
return false
}