/
role.go
54 lines (46 loc) · 971 Bytes
/
role.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
package acl
import (
"strings"
"github.com/pkg/errors"
)
// roles are essentially permission groups
type Role string
const (
All = "all"
)
var (
// A list of all the roles. Currently, the roles
// are defined statically within the program
Roles = []Role{}
)
func getRoles() {
ACLs.Range(func(key, value interface{}) bool {
name, ok := key.(Role)
if !ok {
return true
}
Roles = append(Roles, Role(strings.ToLower(string(name))))
return true
})
}
func (r Role) ToACL() (ACL, error) {
s := strings.ToLower(string(r))
val, ok := ACLs.Load(s)
if !ok {
return ACL{}, errors.Errorf("the role %v is not valid. valid roles are %v", r, Roles)
}
acl, ok := val.(ACL)
if !ok {
return ACL{}, errors.Errorf("the role %v is not valid type. valid roles are %v", r, Roles)
}
return acl, nil
}
func (r0 Role) Validate() bool {
r := strings.ToLower(string(r0))
for _, e := range Roles {
if string(e) == r {
return true
}
}
return false
}