-
Notifications
You must be signed in to change notification settings - Fork 3
/
role.go
184 lines (147 loc) · 3.91 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package organization
import (
"errors"
"fmt"
ldap "gopkg.in/ldap.v2"
)
// AddRole to ldap server, this method will automatically update org's rbacx
func (org *Organization) AddRole(name, description string, ups, pps []string) (string, error) {
id := generateNewID()
dn := org.dn(id, role)
aq := ldap.NewAddRequest(dn)
aq.Attribute(`objectClass`, []string{`role`, `top`})
aq.Attribute(`cn`, []string{name})
aq.Attribute(`description`, []string{description})
aq.Attribute(`upid`, ups)
aq.Attribute(`ppid`, pps)
err := org.Add(aq) // 先写数据库
if err != nil {
return ``, err
}
r, err := org.RoleByID(id)
if err != nil {
return ``, nil
}
org.insertNewPolicyByRole(r)
return id, nil
}
// DelRole from ldap server, automatically update org's rbacx
func (org *Organization) DelRole(id string) error {
// 判断有没有人引用这个Role
mIDs, err := org.MemberIDsByRoleIDs([]string{id})
if err != nil {
return err
}
if len(mIDs) > 0 {
return fmt.Errorf(`尚有人引用此角色 count: %d`, len(mIDs))
}
dn := org.dn(id, role)
dq := ldap.NewDelRequest(dn, nil)
err = org.Del(dq)
if err != nil {
return err
}
org.removePolicyByRoleID(id)
return nil
}
// ModifyRole in ldap server, automatically update org's rbacx
func (org *Organization) ModifyRole(id, name, description string, ups, pps []string) error {
if len(ups) > 0 || len(pps) > 0 { // permission id 有效性判断
p, err := org.PermissionByIDs(append(ups, pps...))
if err != nil {
return err
}
if len(p.Data) != len(ups)+len(pps) {
return errors.New(`permission ids is invalid`)
}
}
r, err := org.RoleByID(id)
if err != nil {
return err
}
mq := ldap.NewModifyRequest(r[`dn`].(string))
if len(name) > 0 {
mq.Replace(`cn`, []string{name})
}
if len(description) > 0 {
mq.Replace(`description`, []string{description})
}
if len(ups) > 0 {
mq.Replace(`upid`, ups)
}
if len(pps) > 0 {
mq.Replace(`ppid`, pps)
}
err = org.Modify(mq)
if err != nil {
return err
}
oTypes := append(ups, r[`upid`].([]string)...)
nTypes := append(pps, r[`ppid`].([]string)...)
org.refreshRBACIfNeeded(oTypes, nTypes)
return nil
}
// AllRoles ...
func (org *Organization) AllRoles() ([]map[string]interface{}, error) {
r, e := org.Roles(0, nil)
if e != nil {
return nil, e
}
return r.Data, nil
}
// Roles in ldap
func (org *Organization) Roles(pageSize uint32, cookie []byte) (*SearchResult, error) {
return org.searchRole(``, pageSize, cookie)
}
// RoleByID ...
func (org *Organization) RoleByID(id string) (map[string]interface{}, error) {
sr, e := org.RoleByIDs([]string{id})
if e != nil {
return nil, e
}
if len(sr.Data) != 1 {
return nil, errors.New(`found many roles`)
}
return sr.Data[0], nil
}
// RoleByIDs in ldap
func (org *Organization) RoleByIDs(ids []string) (*SearchResult, error) {
filter, err := sqConvertIDsToFilter(ids)
if err != nil {
return nil, err
}
return org.searchRole(filter, 0, nil)
}
// RoleIDsByMemberID ...
func (org *Organization) RoleIDsByMemberID(id string) ([]string, error) {
if len(id) == 0 {
return nil, errors.New(`id must not be empty`)
}
filter := fmt.Sprintf(`(id=%s)`, id)
sq := ldap.NewSearchRequest(org.parentDN(member),
ldap.ScopeSingleLevel,
ldap.DerefAlways, 0, 0, false, filter, []string{`rbacRole`}, nil)
sr, err := org.Search(sq)
if err != nil {
return nil, err
}
if len(sr.Entries) != 1 {
return nil, fmt.Errorf(`[%s] member doesn't exit`, id)
}
return sr.Entries[0].GetAttributeValues(`rbacRole`), nil
}
// RoleIDsByPermissionID which role contain this permission
func (org *Organization) RoleIDsByPermissionID(id string) ([]string, error) {
filter := fmt.Sprintf(`(|(upid=%s)(ppid=%s))`, id, id)
dn := org.parentDN(role)
sq := &searchRequest{dn, filter, []string{`id`}, nil, 0, nil}
r, e := org.search(sq)
if e != nil {
return nil, e
}
var ids []string
for _, v := range r.Data {
ids = append(ids, v[`id`].(string))
}
return ids, nil
}