-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacl.go
71 lines (61 loc) · 1.34 KB
/
acl.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
package acl
type Acl struct {
Groups map[string][]string
Actions []string
}
func (acl *Acl) build() {
if acl.Groups == nil {
acl.Groups = make(map[string][]string)
}
}
func (acl *Acl) addGroup(group string) {
acl.build()
if _, ok := acl.Groups[group]; !ok {
acl.Groups[group] = make([]string, 0)
}
}
func (acl *Acl) hasAction(action string) bool {
for _, a := range acl.Actions {
if a == action {
return true
}
}
return false
}
func (acl *Acl) addActions(actions ...string) {
for _, action := range actions {
if !acl.hasAction(action) {
acl.Actions = append(acl.Actions, action)
}
}
}
func (acl *Acl) Grant(group string, actions ...string) {
acl.addActions(actions...)
acl.addGroup(group)
acl.Groups[group] = append(acl.Groups[group], actions...)
}
func (acl *Acl) Revoke(group string, actions ...string) {
acl.addGroup(group)
for i, a := range acl.Groups[group] {
for _, action := range actions {
if a == action {
acl.Groups[group] = append(acl.Groups[group][:i], acl.Groups[group][i+1:]...)
}
}
}
}
func (acl *Acl) Can(group string, action string) bool {
acl.addGroup(group)
for _, a := range acl.Groups[group] {
if a == action {
return true
}
}
return false
}
func (acl *Acl) Not(group string, action string) bool {
return !acl.Can(group, action)
}
func (acl *Acl) Clear() {
acl.Groups = nil
}