This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
util.go
111 lines (97 loc) · 3.56 KB
/
util.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
// Copyright Monax Industries Limited
// SPDX-License-Identifier: Apache-2.0
package permission
import (
"strings"
)
// ConvertMapStringIntToPermissions converts a map of string-bool pairs and a slice of
// strings for the roles to an AccountPermissions type. If the value in the
// permissions map is true for a particular permission string then the permission
// will be set in the AccountsPermissions. For all unmentioned permissions the
// ZeroBasePermissions is defaulted to.
func ConvertPermissionsMapAndRolesToAccountPermissions(permissions map[string]bool,
roles []string) (*AccountPermissions, error) {
var err error
accountPermissions := &AccountPermissions{}
accountPermissions.Base, err = convertPermissionsMapStringIntToBasePermissions(permissions)
if err != nil {
return nil, err
}
accountPermissions.Roles = roles
return accountPermissions, nil
}
// convertPermissionsMapStringIntToBasePermissions converts a map of string-bool
// pairs to BasePermissions.
func convertPermissionsMapStringIntToBasePermissions(permissions map[string]bool) (BasePermissions, error) {
// initialise basePermissions as ZeroBasePermissions
basePermissions := ZeroBasePermissions
for permissionName, value := range permissions {
permissionsFlag, err := PermStringToFlag(permissionName)
if err != nil {
return basePermissions, err
}
// sets the permissions bitflag and the setbit flag for the permission.
basePermissions.Set(permissionsFlag, value)
}
return basePermissions, nil
}
// Builds a composite BasePermission by creating a PermFlag from permissions strings and
// setting them all
func BasePermissionsFromStringList(permissions []string) (BasePermissions, error) {
permFlag, err := PermFlagFromStringList(permissions)
if err != nil {
return ZeroBasePermissions, err
}
return BasePermissions{
Perms: permFlag,
SetBit: permFlag,
}, nil
}
// Builds a composite PermFlag by mapping each permission string in permissions to its
// flag and composing them with binary or
func PermFlagFromStringList(permissions []string) (PermFlag, error) {
var permFlag PermFlag
for _, perm := range permissions {
s := strings.TrimSpace(perm)
if s == "" {
continue
}
flag, err := PermStringToFlag(s)
if err != nil {
return permFlag, err
}
permFlag |= flag
}
return permFlag, nil
}
// Builds a list of set permissions from a BasePermission by creating a list of permissions strings
// from the resultant permissions of basePermissions
func BasePermissionsToStringList(basePermissions BasePermissions) []string {
return PermFlagToStringList(basePermissions.ResultantPerms())
}
// Creates a list of individual permission flag strings from a possibly composite PermFlag
// by projecting out each bit and adding its permission string if it is set
func PermFlagToStringList(permFlag PermFlag) []string {
permStrings := make([]string, 0, NumPermissions)
for i := uint(0); i < NumPermissions; i++ {
permFlag := permFlag & (1 << i)
if permFlag > 0 {
permStrings = append(permStrings, permFlag.String())
}
}
return permStrings
}
// Generates a human readable string from the resultant permissions of basePermission
func BasePermissionsString(basePermissions BasePermissions) string {
return strings.Join(BasePermissionsToStringList(basePermissions), " | ")
}
func String(permFlag PermFlag) string {
return strings.Join(PermFlagToStringList(permFlag), " | ")
}
func (pf PermFlag) MarshalText() ([]byte, error) {
return []byte(String(pf)), nil
}
func (pf *PermFlag) UnmarshalText(s []byte) (err error) {
*pf, err = PermFlagFromStringList(strings.Split(string(s), "|"))
return
}