From 00e79ce95c98ddf6406a3a433a3272b273d62c60 Mon Sep 17 00:00:00 2001 From: butlerx Date: Tue, 27 Feb 2018 09:58:53 +0000 Subject: [PATCH] break rbldap.search in to its own file --- pkg/rbuser/rbldap.go | 61 ------------------------------------------- pkg/rbuser/search.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 61 deletions(-) create mode 100644 pkg/rbuser/search.go diff --git a/pkg/rbuser/rbldap.go b/pkg/rbuser/rbldap.go index b0db115..a8c4279 100644 --- a/pkg/rbuser/rbldap.go +++ b/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 @@ -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 -} diff --git a/pkg/rbuser/search.go b/pkg/rbuser/search.go new file mode 100644 index 0000000..53df940 --- /dev/null +++ b/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 +}