Skip to content

Commit

Permalink
break rbldap.search in to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
butlerx committed Feb 27, 2018
1 parent 1466de3 commit 00e79ce
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 61 deletions.
61 changes: 0 additions & 61 deletions pkg/rbuser/rbldap.go
@@ -1,13 +1,5 @@
package rbuser

import (
"strconv"
"strings"
"time"

ldap "gopkg.in/ldap.v2"
)

// RbLdap Server object used for connecting to server
type RbLdap struct {
*ldapConf
Expand All @@ -20,56 +12,3 @@ func NewRbLdap(user, password, host string, port int) (*RbLdap, error) {
rb.connect()
return &rb, nil
}

// 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
}
62 changes: 62 additions & 0 deletions pkg/rbuser/search.go
@@ -0,0 +1,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
}

0 comments on commit 00e79ce

Please sign in to comment.