-
Notifications
You must be signed in to change notification settings - Fork 3
/
sq.go
190 lines (161 loc) · 4.27 KB
/
sq.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
package organization
import (
"fmt"
"strings"
ldap "gopkg.in/ldap.v2"
)
type searchRequest struct {
dn string
filter string
sAttrs []string
mAttrs []string
size uint32
cookie []byte
}
// SearchResult ...
type SearchResult struct {
Size uint32
Cookie []byte
Data []map[string]interface{}
}
func (org *Organization) search(sq *searchRequest) (*SearchResult, error) {
var controls []ldap.Control
if sq.size > 0 {
control := ldap.NewControlPaging(sq.size)
control.SetCookie(sq.cookie)
controls = append(controls, control)
}
lsq := ldap.NewSearchRequest(sq.dn,
ldap.ScopeWholeSubtree,
ldap.DerefAlways, 0, 0, false,
sq.filter,
append(sq.sAttrs, sq.mAttrs...),
controls)
var lsr *ldap.SearchResult
var err error
if sq.size == 0 && sq.cookie == nil {
lsr, err = org.SearchWithPaging(lsq, 200) // 会循环拿完所有对象
} else {
lsr, err = org.Search(lsq)
}
if err != nil {
return nil, err
}
data := make([]map[string]interface{}, 0)
// 所有的人不返回dn属性
shouldAppendDN := !strings.Contains(sq.dn, `ou=member`)
for _, e := range lsr.Entries {
v := make(map[string]interface{}, 0)
for _, s := range sq.sAttrs {
v[s] = e.GetAttributeValue(s)
}
for _, m := range sq.mAttrs {
v[m] = e.GetAttributeValues(m)
}
if shouldAppendDN {
v[`dn`] = e.DN
}
data = append(data, v)
}
var cookie []byte
var size uint32
pagingResult := ldap.FindControl(lsr.Controls, ldap.ControlTypePaging)
if pagingResult != nil {
cookie = pagingResult.(*ldap.ControlPaging).Cookie
size = pagingResult.(*ldap.ControlPaging).PagingSize
}
return &SearchResult{size, cookie, data}, nil
}
// func (org *Organization) searchType(filter, dn string, pageSize uint32, cookie []byte) (*SearchResult, error) {
// if len(filter) == 0 {
// filter = `(objectClass=doloresType)`
// } else {
// filter = fmt.Sprintf(`(&(objectClass=doloresType)%s)`, filter)
// }
// return org.search(&searchRequest{(dn),
// filter,
// []string{`id`, `cn`, `description`, `modifyTimestamp`}, nil,
// pageSize,
// cookie,
// })
// }
func (org *Organization) searchPermission(filter, dn string, pageSize uint32, cookie []byte) (*SearchResult, error) {
if len(filter) == 0 {
filter = `(objectClass=permission)`
} else {
filter = fmt.Sprintf(`(&(objectClass=permission)%s)`, filter)
}
return org.search(&searchRequest{dn,
filter,
[]string{`id`, `cn`, `description`},
[]string{`rbacType`},
pageSize,
cookie,
})
}
func (org *Organization) searchRole(filter string, pageSize uint32, cookie []byte) (*SearchResult, error) {
if len(filter) == 0 {
filter = `(objectClass=role)`
} else {
filter = fmt.Sprintf(`(&(objectClass=role)%s)`, filter)
}
return org.search(&searchRequest{org.parentDN(role),
filter,
[]string{`id`, `cn`, `description`},
[]string{`upid`, `ppid`},
pageSize,
cookie,
})
}
func (org *Organization) searchUnit(filter string, containACL bool) ([]map[string]interface{}, error) {
attrs := UnitAttributes[0:]
if containACL {
attrs = append(attrs, `rbacType`)
}
if len(filter) == 0 {
filter = `(objectClass=organizationalUnit)`
}
sq := ldap.NewSearchRequest(org.parentDN(unit),
ldap.ScopeWholeSubtree, ldap.DerefAlways, 0, 0, false,
filter, attrs, nil)
sr, e := org.Search(sq)
if e != nil {
return nil, e
}
units := make([]map[string]interface{}, 0)
for _, e := range sr.Entries {
unit := make(map[string]interface{}, 0)
for _, attr := range attrs {
unit[attr] = e.GetAttributeValue(attr)
}
// unit[`id`] = e.GetAttributeValue(`id`)
// unit[`cn`] = e.GetAttributeValue(`ou`)
// unit[`description`] = e.GetAttributeValue(`description`)
if containACL {
unit[`dn`] = e.DN
}
dn, _ := ldap.ParseDN(e.DN)
if len(dn.RDNs) > 5 {
unit[`parentID`] = dn.RDNs[1].Attributes[0].Value
}
units = append(units, unit)
}
return units, nil
}
func sqConvertIDsToFilter(ids []string) (string, error) {
return sqConvertArraysToFilter(`id`, ids)
}
func sqConvertArraysToFilter(label string, datas []string) (string, error) {
if len(datas) == 0 {
return ``, fmt.Errorf(`At least one %s`, label)
}
if len(datas) == 1 {
return fmt.Sprintf(`(%s=%s)`, label, datas[0]), nil
}
filter := `(|`
for _, id := range datas {
filter += fmt.Sprintf(`(%s=%s)`, label, id)
}
filter += `)`
return filter, nil
}