-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.go
79 lines (64 loc) · 1.44 KB
/
groups.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
package ldap
import (
"errors"
"github.com/go-ldap/ldap/v3"
)
var ErrGroupNotFound = errors.New("group not found")
type Group struct {
Object
// Members is a list of DNs
Members []string
}
func (l *LDAP) FindGroupByDN(dn string) (group *Group, err error) {
c, err := l.GetConnection()
if err != nil {
return nil, err
}
defer c.Close()
r, err := c.Search(&ldap.SearchRequest{
BaseDN: dn,
Scope: ldap.ScopeBaseObject,
DerefAliases: ldap.NeverDerefAliases,
Filter: "(objectClass=group)",
Attributes: []string{"cn", "member"},
})
if err != nil {
return nil, err
}
if len(r.Entries) == 0 {
return nil, ErrGroupNotFound
}
if len(r.Entries) > 1 {
return nil, ErrDNDuplicated
}
group = &Group{
Object: objectFromEntry(r.Entries[0]),
Members: r.Entries[0].GetAttributeValues("member"),
}
return
}
func (l *LDAP) FindGroups() (groups []Group, err error) {
c, err := l.GetConnection()
if err != nil {
return nil, err
}
defer c.Close()
r, err := c.Search(&ldap.SearchRequest{
BaseDN: l.config.BaseDN,
Scope: ldap.ScopeWholeSubtree,
DerefAliases: ldap.NeverDerefAliases,
Filter: "(objectClass=group)",
Attributes: []string{"cn", "member"},
})
if err != nil {
return nil, err
}
for _, entry := range r.Entries {
group := Group{
Object: objectFromEntry(entry),
Members: entry.GetAttributeValues("member"),
}
groups = append(groups, group)
}
return
}