forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_permissions.go
70 lines (63 loc) · 1.57 KB
/
account_permissions.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
package permission
import "github.com/hyperledger/burrow/binary"
func NewAccountPermissions(pss ...PermFlag) AccountPermissions {
var perms PermFlag
for _, ps := range pss {
perms |= ps
}
return AccountPermissions{
Base: BasePermissions{
Perms: perms,
SetBit: perms,
},
}
}
// Returns true if the role is found
func (ap AccountPermissions) HasRole(role string) bool {
role = string(binary.RightPadBytes([]byte(role), 32))
for _, r := range ap.Roles {
if r == role {
return true
}
}
return false
}
// Returns true if the role is added, and false if it already exists
func (ap *AccountPermissions) AddRole(role string) bool {
role = string(binary.RightPadBytes([]byte(role), 32))
for _, r := range ap.Roles {
if r == role {
return false
}
}
ap.Roles = append(ap.Roles, role)
return true
}
// Returns true if the role is removed, and false if it is not found
func (ap *AccountPermissions) RmRole(role string) bool {
role = string(binary.RightPadBytes([]byte(role), 32))
for i, r := range ap.Roles {
if r == role {
post := []string{}
if len(ap.Roles) > i+1 {
post = ap.Roles[i+1:]
}
ap.Roles = append(ap.Roles[:i], post...)
return true
}
}
return false
}
// Clone clones the account permissions
func (ap *AccountPermissions) Clone() AccountPermissions {
// clone base permissions
basePermissionsClone := ap.Base
// clone roles []string
rolesClone := make([]string, len(ap.Roles))
// strings are immutable so copy suffices
copy(rolesClone, ap.Roles)
return AccountPermissions{
Base: basePermissionsClone,
Roles: rolesClone,
}
}