-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap.go
301 lines (267 loc) · 5.16 KB
/
ldap.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package contacts
import (
"fmt"
"log"
"reflect"
"time"
ldap "github.com/go-ldap/ldap/v3"
)
func connect(config Config) (*ldap.Conn, error) {
conn, err := ldap.Dial("tcp", fmt.Sprintf("%s:%s", config.Host, config.Port))
if err != nil {
return nil, err
}
err = conn.Bind(config.Username, config.Password)
if err != nil {
return nil, err
}
return conn, nil
}
func del(config Config, request *ldap.DelRequest) error {
conn, err := connect(config)
if err != nil {
return err
}
defer conn.Close()
return conn.Del(request)
}
func save(config Config, request *ldap.ModifyRequest) error {
log.Printf("save: %+v", request)
conn, err := connect(config)
if err != nil {
return err
}
defer conn.Close()
return conn.Modify(request)
}
func create(config Config, request *ldap.AddRequest) error {
log.Printf("create: %+v", request)
conn, err := connect(config)
if err != nil {
return err
}
defer conn.Close()
return conn.Add(request)
}
func getEntries(config Config, request *ldap.SearchRequest, handle func(*ldap.Entry)) error {
conn, err := connect(config)
if err != nil {
return err
}
defer conn.Close()
s, err := conn.Search(request)
if err != nil {
return err
}
for _, e := range s.Entries {
handle(e)
}
return nil
}
func setAttributes(c interface{}, entry *ldap.Entry) {
if c == nil {
return
}
handleLDAPAttributes(c, func(n string, v reflect.Value) {
attrs := entry.GetAttributeValues(n)
if !v.CanSet() {
return
}
if !v.CanInterface() {
return
}
switch v.Interface().(type) {
case string:
sval := ""
if len(attrs) > 0 {
sval = attrs[0]
}
v.SetString(sval)
case []string:
v.Set(reflect.ValueOf(attrs))
case time.Time:
sval := ""
if len(attrs) > 0 {
sval = attrs[0]
}
v.Set(reflect.ValueOf(parseDate(sval)))
}
})
}
func attributeNames(c interface{}) []string {
var names []string
if c == nil {
return names
}
handleLDAPAttributes(c, func(n string, v reflect.Value) {
names = append(names, n)
})
return names
}
func attributeValues(c interface{}) map[string][]string {
vals := map[string][]string{}
if c == nil {
return vals
}
handleLDAPAttributes(c, func(n string, fval reflect.Value) {
if !fval.CanInterface() {
return
}
switch v := fval.Interface().(type) {
case string:
if v != "" {
vals[n] = []string{v}
}
case []string:
if len(v) > 0 {
vals[n] = v
}
case time.Time:
if !v.IsZero() {
vals[n] = []string{LDAPDate(v).FullDate()}
}
}
})
return vals
}
func handleLDAPAttributes(c interface{}, fn func(key string, val reflect.Value)) {
if c == nil {
return
}
cval := reflect.ValueOf(c).Elem()
ctype := cval.Type()
for i := 0; i < ctype.NumField(); i++ {
field := ctype.Field(i)
if n, ok := field.Tag.Lookup("ldap"); ok {
fval := cval.Field(i)
fn(n, fval)
}
}
}
func changes(cur, updates map[string][]string) map[string]map[string][]string {
diff := map[string]map[string][]string{
"add": {},
"delete": {},
"modify": {},
"replace": {},
}
if reflect.DeepEqual(cur, updates) {
return nil
}
for k, v := range updates {
if k == "cn" {
continue
}
if pre, ok := cur[k]; ok {
if reflect.DeepEqual(v, pre) {
continue
}
if len(pre) == len(v) {
if len(pre) > 1 {
diff["replace"][k] = v
continue
}
diff["modify"][k] = v
continue
} else {
if len(v) == 0 {
diff["delete"][k] = pre
continue
}
if len(pre) == 0 {
diff["add"][k] = v
continue
}
diff["replace"][k] = v
continue
}
} else {
diff["add"][k] = v
}
}
for k, pre := range cur {
if k == "cn" {
continue
}
if _, ok := updates[k]; !ok {
diff["delete"][k] = pre
}
}
return diff
}
type LDAPDate time.Time
func (l LDAPDate) FullDate() string {
if time.Time(l).IsZero() {
return ""
}
if time.Time(l).Year() > 0 {
return time.Time(l).Format("Monday, January 2, 2006")
}
return time.Time(l).Format("January 2")
}
func (l LDAPDate) Date() string {
if time.Time(l).IsZero() {
return ""
}
if time.Time(l).Year() > 0 {
return time.Time(l).Format("Jan _2, 2006")
}
return time.Time(l).Format("Jan _2")
}
func (l LDAPDate) DayOfWeek() string {
if time.Time(l).IsZero() {
return ""
}
return time.Time(l).Format("Monday")
}
func (l LDAPDate) DayOfMonth() int {
if time.Time(l).IsZero() {
return -1
}
return time.Time(l).Day()
}
func (l LDAPDate) Month() string {
if time.Time(l).IsZero() {
return ""
}
return time.Time(l).Format("January")
}
func (l LDAPDate) Year() int {
if time.Time(l).IsZero() {
return -1
}
return time.Time(l).Year()
}
var (
dateFormats = []string{
"Monday, January 02, 2006",
"Monday, January _2, 2006",
"Monday, January 2, 2006",
"January 02, 2006",
"January _2, 2006",
"January 2, 2006",
"January 02",
"January _2",
"January 2",
"Jan 02, 2006",
"Jan _2, 2006",
"Jan 2, 2006",
"Jan 02",
"Jan _2",
"Jan 2",
"01/02/2006",
"1/2/2006",
"01/02/06",
"1/2/06",
"01/02",
"1/2",
}
)
func parseDate(given string) time.Time {
for _, dateFormat := range dateFormats {
if date, err := time.ParseInLocation(dateFormat, given, time.Local); err == nil {
return date
}
}
return time.Time{}
}