-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmember.go
217 lines (184 loc) · 5.42 KB
/
member.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package organization
import (
"errors"
"fmt"
ldap "gopkg.in/ldap.v2"
)
// MemberSignleAttrs default signle attributes
var MemberSignleAttrs = [...]string{`id`, `name`, `cn`, `telephoneNumber`, `labeledURI`, `gender`, `title`, `priority`}
// MemberSignleACLAttrs default signle acl attributes
var MemberSignleACLAttrs = [...]string{`rbacType`}
// MemberMultipleAttrs default multiple attributes
var MemberMultipleAttrs = [...]string{`email`, `unitID`}
// MemberMultipleACLAttrs default multiple acl attributes
var MemberMultipleACLAttrs = [...]string{`rbacRole`}
// AddMember to ldap server
func (org *Organization) AddMember(info map[string][]string) (string, error) {
id := generateNewID()
aq := ldap.NewAddRequest(org.dn(id, member))
aq.Attribute(`objectClass`, []string{`member`, `top`})
aq.Attribute(`id`, []string{id})
for k, v := range info {
aq.Attribute(k, v)
}
err := org.Add(aq)
if err != nil {
return ``, err
}
nm, err := org.MemberByID(id, true, false)
if err != nil {
return ``, err
}
return id, org.logAddMember(nm)
}
// ModifyMember ...
func (org *Organization) ModifyMember(id string, info map[string][]string) error {
oMember, err := org.MemberByID(id, true, false)
if err != nil {
return err
}
dn := org.dn(id, member)
mq := ldap.NewModifyRequest(dn)
for k, v := range info {
mq.Replace(k, v)
}
err = org.Modify(mq)
if err == nil {
go func() {
nMember, _ := org.MemberByID(id, true, false)
org.logModifyMember(oMember, nMember)
}()
}
return err
}
// DelMember by id
func (org *Organization) DelMember(id string) error {
m, err := org.MemberByID(id, true, false)
if err != nil {
return err
}
mids, err := org.relatedMIDs(m[`rbacType`].(string))
if err != nil {
return err
}
dq := ldap.NewDelRequest(fmt.Sprintf(`id=%s,%s`, id, org.parentDN(member)), nil)
err = org.Del(dq)
if err != nil {
return err
}
return org.logDelMember(id, mids)
}
// AuthMember ...
func (org *Organization) AuthMember(telephoneNumber, pwd string) (string, error) {
filter := fmt.Sprintf(`(telephoneNumber=%s)`, telephoneNumber)
sq := ldap.NewSearchRequest(org.parentDN(member), ldap.ScopeSingleLevel, ldap.DerefAlways, 0, 0, false, filter, []string{`id`}, nil)
r, err := org.Search(sq)
if err != nil {
return ``, err
}
if len(r.Entries) != 1 {
return ``, fmt.Errorf(`can't find this member by tel: [%s]`, telephoneNumber)
}
success, err := org.Compare(r.Entries[0].DN, `userPassword`, pwd)
if err != nil {
return ``, err
}
if !success {
return ``, errors.New(`password incorrect`)
}
return r.Entries[0].GetAttributeValue(`id`), nil
}
// ModifyPassword ...
func (org *Organization) ModifyPassword(id, originalPassword, newPassword string) error {
dn := org.dn(id, member)
success, err := org.Compare(dn, `userPassword`, originalPassword)
if err != nil {
return err
}
if !success {
return errors.New(`password incorrect`)
}
mq := ldap.NewModifyRequest(dn)
mq.Replace(`userPassword`, []string{newPassword})
return org.Modify(mq) // 这样写可以避免产生审计日志
}
// Members return all members
func (org *Organization) Members(pageSize uint32, cookie []byte) (*SearchResult, error) {
sq := &searchRequest{
org.parentDN(member), `(objectClass=member)`,
append(MemberSignleAttrs[:], MemberSignleACLAttrs[:]...),
append(MemberMultipleAttrs[:], MemberMultipleACLAttrs[:]...), pageSize, cookie}
return org.search(sq)
}
// MemberIDsByTypeIDs ...
func (org *Organization) MemberIDsByTypeIDs(tids []string) ([]string, error) {
filter, err := sqConvertArraysToFilter(`rbacType`, tids)
if err != nil {
return nil, err
}
return org.memberIDsByFilter(filter)
}
// MemberIDsByRoleIDs ...
func (org *Organization) MemberIDsByRoleIDs(rids []string) ([]string, error) {
filter, err := sqConvertArraysToFilter(`rbacRole`, rids)
if err != nil {
return nil, err
}
return org.memberIDsByFilter(filter)
}
// MemberIDsByDepartmentIDs ...
func (org *Organization) MemberIDsByDepartmentIDs(ids []string) ([]string, error) {
filter, err := sqConvertArraysToFilter(`unitID`, ids)
if err != nil {
return nil, err
}
return org.memberIDsByFilter(filter)
}
func (org *Organization) memberIDsByFilter(filter string) ([]string, error) {
dn := org.parentDN(member)
sq := ldap.NewSearchRequest(dn, ldap.ScopeSingleLevel, ldap.DerefAlways,
0, 0, false, filter, []string{`id`}, nil)
sr, e := org.Search(sq)
if e != nil {
return nil, e
}
var ids []string
for _, entry := range sr.Entries {
ids = append(ids, entry.GetAttributeValue(`id`))
}
return ids, nil
}
// MemberByIDs ...
func (org *Organization) MemberByIDs(ids []string, containACL bool, containPwd bool) ([]map[string]interface{}, error) {
sa := MemberSignleAttrs[:]
ma := MemberMultipleAttrs[:]
if containACL {
sa = append(sa, MemberSignleACLAttrs[:]...)
ma = append(ma, MemberMultipleACLAttrs[:]...)
}
if containPwd {
sa = append(sa, `thirdPassword`)
}
dn := org.parentDN(member)
filter, err := sqConvertIDsToFilter(ids)
if err != nil {
return nil, err
}
sq := &searchRequest{dn, filter, sa, ma, 0, nil}
r, e := org.search(sq)
if e != nil {
return nil, e
}
return r.Data, nil
}
// MemberByID search member by id
func (org *Organization) MemberByID(id string, containACL bool, containPwd bool) (map[string]interface{}, error) {
members, err := org.MemberByIDs([]string{id}, containACL, containPwd)
if err != nil {
return nil, err
}
if len(members) != 1 {
return nil, errors.New(`not found`)
}
return members[0], nil
}