-
Notifications
You must be signed in to change notification settings - Fork 1
/
permission.go
115 lines (100 loc) · 2.32 KB
/
permission.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
package utility
import "github.com/mkawserm/flamed/pkg/pb"
// HasReadPermission checks availability of read permission
func HasReadPermission(ac *pb.AccessControl) bool {
if ac == nil {
return false
}
return hasBit(ac.Permission, 0)
}
// HasWritePermission checks availability of write permission
func HasWritePermission(ac *pb.AccessControl) bool {
if ac == nil {
return false
}
return hasBit(ac.Permission, 1)
}
// HasUpdatePermission checks availability of update permission
func HasUpdatePermission(ac *pb.AccessControl) bool {
if ac == nil {
return false
}
return hasBit(ac.Permission, 2)
}
// HasDeletePermission checks availability of delete permission
func HasDeletePermission(ac *pb.AccessControl) bool {
if ac == nil {
return false
}
return hasBit(ac.Permission, 3)
}
// HasGlobalSearchPermission checks availability of global search permission
func HasGlobalSearchPermission(ac *pb.AccessControl) bool {
if ac == nil {
return false
}
return hasBit(ac.Permission, 4)
}
// HasGlobalIteratePermission checks availability of global iterate permission
func HasGlobalIteratePermission(ac *pb.AccessControl) bool {
if ac == nil {
return false
}
return hasBit(ac.Permission, 5)
}
// HasGlobalRetrievePermission checks availability of global retrieve permission
func HasGlobalRetrievePermission(ac *pb.AccessControl) bool {
if ac == nil {
return false
}
return hasBit(ac.Permission, 6)
}
// HasGlobalCRUDPermission checks availability of global CRUD permission
func HasGlobalCRUDPermission(ac *pb.AccessControl) bool {
if ac == nil {
return false
}
return hasBit(ac.Permission, 7)
}
// NewPermission creates permission unsigned integer based on
// parameters boolean flag
func NewPermission(read bool, write bool, update bool, delete bool,
globalSearch bool,
globalIterate bool,
globalRetrieve bool,
globalCRUD bool) uint64 {
var p uint64 = 0
if read {
p = setBit(p, 0)
}
if write {
p = setBit(p, 1)
}
if update {
p = setBit(p, 2)
}
if delete {
p = setBit(p, 3)
}
if globalSearch {
p = setBit(p, 4)
}
if globalIterate {
p = setBit(p, 5)
}
if globalRetrieve {
p = setBit(p, 6)
}
if globalCRUD {
p = setBit(p, 7)
}
return p
}
func setBit(n uint64, pos uint16) uint64 {
n |= 1 << pos
return n
}
func hasBit(n uint64, pos uint16) bool {
val := n & (1 << pos)
return val > 0
}