-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
86 lines (76 loc) · 1.94 KB
/
helper.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
var accessRegister = AccessRegister{
actionAccess: make(map[string]map[string]ActionAccess),
resourceAccess: make(map[int]ResourceAccess),
}
func AddActionAccess(controllerName, actionName string, actionAccess ActionAccess) {
accessRegister.AddActionAccess(controllerName, actionName, actionAccess)
}
func AddResourceAccess(resourceType int, resourceAccess ResourceAccess) {
accessRegister.AddResourceAccess(resourceType, resourceAccess)
}
func HasActionAccess(controllerName, actionName string, rights []int) bool {
if len(rights) == 0 {
return false
}
if isRA(rights) {
return true
}
actionList := accessRegister.GetActionAccesses()
if _, ok := actionList[controllerName]; !ok {
return false
}
if _, ok := actionList[controllerName][actionName]; !ok {
return false
}
for resourceType, accessType := range actionList[controllerName][actionName] {
if HasResourceAccess(resourceType, accessType, rights) {
return true
}
}
return false
}
func HasResourceAccess(resourceType, accessType int, rights []int) bool {
resourceTypes := accessRegister.GetResourceAccesses()
if _, ok := resourceTypes[resourceType]; !ok {
return false
}
if _, ok := resourceTypes[resourceType][accessType]; !ok {
return false
}
return HasRight(resourceTypes[resourceType][accessType], rights)
}
func HasRight(checkRights, rights []int) bool {
if isRA(rights) {
return true
}
rightsLeft := make(map[int]int)
rightsRight := make(map[int]int)
for n, right := range rights {
rightsLeft[n] = padding(right)
rightsRight[n] = padding(right+1) - 1
}
for _, accessRight := range checkRights {
ar := padding(accessRight)
for n := range rights {
if ar >= rightsLeft[n] && ar <= rightsRight[n] {
return true
}
}
}
return false
}
func isRA(rights []int) bool {
for i := range rights {
if rights[i] == RSA {
return true
}
}
return false
}
func padding(c int) int {
for c != 0 && c <= 100000000 {
c *= 10
}
return c
}