58 lines
1 KiB
Go
58 lines
1 KiB
Go
package ldap
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
ldap "gopkg.in/ldap.v2"
|
|
)
|
|
|
|
func (p *Pool) Search(base, filter string, attr string) (bool, string, []string, error) {
|
|
p.lock.Lock()
|
|
defer p.lock.Unlock()
|
|
|
|
err := p.auth()
|
|
if err != nil {
|
|
return false, "", nil, err
|
|
}
|
|
|
|
var list []string = nil
|
|
if attr != "" {
|
|
list = []string{attr}
|
|
}
|
|
|
|
res, err := p.conn.Search(ldap.NewSearchRequest(
|
|
base,
|
|
ldap.ScopeWholeSubtree,
|
|
ldap.NeverDerefAliases,
|
|
0,
|
|
0,
|
|
false,
|
|
filter,
|
|
list,
|
|
nil,
|
|
))
|
|
if err != nil {
|
|
return false, "", nil, err
|
|
}
|
|
|
|
if len(res.Entries) == 0 {
|
|
return true, "", nil, fmt.Errorf("No results for %s filter %s", base, filter)
|
|
}
|
|
|
|
if attr == "" && len(res.Entries) > 1 {
|
|
return true, "", nil, fmt.Errorf("Too many results for %s filter %s", base, filter)
|
|
}
|
|
|
|
var result []string = nil
|
|
if attr != "" {
|
|
result = []string{}
|
|
for _, entry := range res.Entries {
|
|
result = append(result, entry.GetAttributeValue(attr))
|
|
}
|
|
sort.Strings(result)
|
|
}
|
|
|
|
return true, res.Entries[0].DN, result, nil
|
|
}
|