/
search.go
62 lines (59 loc) · 2.46 KB
/
search.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
package rbuser
import (
"strconv"
"strings"
"time"
ldap "gopkg.in/ldap.v2"
)
// Search ldap for a given filter and return first user that matches
func (rb *RbLdap) Search(filter string) (RbUser, error) {
sr, err := rb.Conn.Search(ldap.NewSearchRequest(
"ou=accounts,o=redbrick",
ldap.ScopeSingleLevel, ldap.NeverDerefAliases,
0, 0, false, filter,
[]string{"objectClass", "uid", "newbie", "cn", "altmail", "id", "course", "year",
"yearsPaid", "updatedBy", "updated", "createdBy", "created", "birthday", "uidNumber",
"gidNumber", "gecos", "loginShell", "homeDirectory", "userPassword", "host",
"shadowLastChange"}, nil,
))
if err != nil {
return RbUser{}, err
}
for _, entry := range sr.Entries {
noob, _ := strconv.ParseBool(entry.GetAttributeValue("newbie"))
dcuID, _ := strconv.Atoi(entry.GetAttributeValue("id"))
year, _ := strconv.Atoi(entry.GetAttributeValue("year"))
yearsPaid, _ := strconv.Atoi(entry.GetAttributeValue("yearsPaid"))
uidNum, _ := strconv.Atoi(entry.GetAttributeValue("uidNumber"))
gidNum, _ := strconv.Atoi(entry.GetAttributeValue("gidNumber"))
updated, _ := time.Parse("2006-01-02 15:04:00", entry.GetAttributeValue("updated"))
shadow, _ := time.Parse("2006-01-02 15:04:00", entry.GetAttributeValue("shadowLastChange"))
created, _ := time.Parse("2006-01-02 15:04:00", entry.GetAttributeValue("created"))
birthday, _ := time.Parse("2006-01-02 15:04:00", entry.GetAttributeValue("birthday"))
return RbUser{
UID: entry.GetAttributeValue("uid"),
ObjectClass: entry.GetAttributeValue("objectClass"),
Newbie: noob,
CN: entry.GetAttributeValue("cn"),
Altmail: entry.GetAttributeValue("altmail"),
ID: dcuID,
Course: entry.GetAttributeValue("course"),
Year: year,
YearsPaid: yearsPaid,
Updatedby: entry.GetAttributeValue("updatedBy"),
Updated: updated,
CreatedBy: entry.GetAttributeValue("createdBy"),
Created: created,
Birthday: birthday,
UIDNumber: uidNum,
GidNumber: gidNum,
Gecos: entry.GetAttributeValue("gecos"),
LoginShell: entry.GetAttributeValue("loginShell"),
HomeDirectory: entry.GetAttributeValue("homeDirectory"),
UserPassword: entry.GetAttributeValue("userPassword"),
Host: strings.Split(entry.GetAttributeValue("host"), ","),
ShadowLastChange: shadow,
}, nil
}
return RbUser{}, err
}