-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.go
157 lines (131 loc) · 3.38 KB
/
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package role
import (
"encoding/json"
"fmt"
"sync"
"github.com/corioders/gokit/errors"
)
type roleInternal struct {
name string
managerName string
rwmu sync.RWMutex
permissions map[string]*Permission
}
type Role struct {
ri *roleInternal
}
var (
ErrNilPermission = errors.New("Nil permission pointer was passeed")
)
type roleJson struct {
Name string `json:"n"`
ManagerName string `json:"mn"`
}
// MarshalJSON implements Marshaler interface.
func (r *Role) MarshalJSON() ([]byte, error) {
rrj := roleJson{r.ri.name, r.ri.managerName}
return json.Marshal(rrj)
}
// UnmarshalJSON implements Unmarshaler interface.
func (r *Role) UnmarshalJSON(data []byte) error {
rrj := roleJson{}
err := json.Unmarshal(data, &rrj)
if err != nil {
return errors.WithStack(err)
}
manager, err := managerFromName(rrj.ManagerName)
if err != nil {
return errors.WithStack(err)
}
role, ok := manager.getRole(rrj.Name)
if !ok {
return errors.WithMessage(ErrRoleNotExists, fmt.Sprintf(`role name: "%v"`, rrj.Name))
}
r.ri = role.ri
return nil
}
func newRole(name, managerName string, permissions ...*Permission) (*roleInternal, error) {
ri := &roleInternal{
name: name,
managerName: managerName,
rwmu: sync.RWMutex{},
permissions: make(map[string]*Permission),
}
for _, permission := range permissions {
if permission == nil {
return nil, errors.WithStack(ErrNilPermission)
}
ri.permissions[permission.name] = permission
}
return ri, nil
}
// NewRole creates new role, name must be unique across one RoleManager.
// NewRole return error if name is non unique.
func (rm *RoleManager) NewRole(name string, permissions ...*Permission) (*Role, error) {
ri, err := newRole(name, rm.name, permissions...)
if err != nil {
return nil, errors.WithStack(err)
}
role := &Role{
ri: ri,
}
err = rm.insertRole(role)
if err != nil {
return nil, errors.WithStack(err)
}
return role, nil
}
// MustNewRole is the same as NewRole, accept it panics is name is non unique.
func (rm *RoleManager) MustNewRole(name string, permissions ...*Permission) *Role {
role, err := rm.NewRole(name, permissions...)
if err != nil {
panic(err)
}
return role
}
// Allow a permission to the role.
// Allow panics if any of permissions is nil.
func (r *Role) Allow(permissions ...*Permission) {
r.ri.rwmu.Lock()
for _, permission := range permissions {
if permission == nil {
r.ri.rwmu.Unlock()
panic(errors.WithStack(ErrNilPermission))
}
r.ri.permissions[permission.name] = permission
}
r.ri.rwmu.Unlock()
}
// Disallow the specific permission.
// Disallow panics if any of permissions is nil.
func (r *Role) Disallow(permissions ...*Permission) {
r.ri.rwmu.Lock()
for _, permission := range permissions {
if permission == nil {
r.ri.rwmu.Unlock()
panic(errors.WithStack(ErrNilPermission))
}
delete(r.ri.permissions, permission.name)
}
r.ri.rwmu.Unlock()
}
// IsAllowedTo returns true if the role has specific permission.
// IsAllowedTo panics if p is nil.
func (r *Role) IsAllowedTo(p *Permission) bool {
if p == nil {
panic(errors.WithStack(ErrNilPermission))
}
found := false
r.ri.rwmu.RLock()
for _, rp := range r.ri.permissions {
if rp.matches(p) {
found = true
break
}
}
r.ri.rwmu.RUnlock()
return found
}
// func (r *Role) Matches(a *Role) bool {
// return r.ri.name == a.ri.name && r.ri.managerName == a.ri.managerName
// }