-
Notifications
You must be signed in to change notification settings - Fork 21
/
clientGroupAcl.go
87 lines (78 loc) · 1.65 KB
/
clientGroupAcl.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
package clientGroupAcl
import ()
// Acl struct
type ClientGroupAcl struct {
Owner string `bson:"owner" json:"owner"`
Read []string `bson:"read" json:"read"`
Write []string `bson:"write" json:"write"`
Delete []string `bson:"delete" json:"delete"`
Execute []string `bson:"execute" json:"execute"`
}
type Rights map[string]bool
func (a *ClientGroupAcl) SetOwner(str string) {
a.Owner = str
return
}
func (a *ClientGroupAcl) UnSet(str string, r Rights) {
if r["read"] {
a.Read = del(a.Read, str)
}
if r["write"] {
a.Write = del(a.Write, str)
}
if r["delete"] {
a.Delete = del(a.Delete, str)
}
if r["execute"] {
a.Execute = del(a.Execute, str)
}
return
}
func (a *ClientGroupAcl) Set(str string, r Rights) {
if r["read"] {
a.Read = insert(a.Read, str)
}
if r["write"] {
a.Write = insert(a.Write, str)
}
if r["delete"] {
a.Delete = insert(a.Delete, str)
}
if r["execute"] {
a.Execute = insert(a.Execute, str)
}
return
}
func (a *ClientGroupAcl) Check(str string) (r Rights) {
r = Rights{"read": false, "write": false, "delete": false, "execute": false}
acls := map[string][]string{"read": a.Read, "write": a.Write, "delete": a.Delete, "execute": a.Execute}
for k, v := range acls {
for _, id := range v {
if str == id {
r[k] = true
break
}
}
}
return
}
func del(arr []string, s string) (narr []string) {
narr = []string{}
for i, item := range arr {
if item != s {
narr = append(narr, item)
} else {
narr = append(narr, arr[i+1:]...)
break
}
}
return
}
func insert(arr []string, s string) []string {
for _, item := range arr {
if item == s {
return arr
}
}
return append(arr, s)
}