Skip to content

Commit

Permalink
generalized GetUsers() command
Browse files Browse the repository at this point in the history
  • Loading branch information
foodforarabbit committed May 31, 2017
1 parent a66face commit 96edd97
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Binary file modified cmd/json/json
Binary file not shown.
13 changes: 12 additions & 1 deletion cmd/json/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"encoding/json"
"fmt"
"sort"

"github.com/foodforarabbit/go-ldap-client"
)
Expand Down Expand Up @@ -77,7 +78,17 @@ func main() {
log.Fatalf("Error getting groups for user %s: %+v", username, err)
}
for _, user2 := range users {
log.Printf("allUser: %+v", user2)

log.Println("------")
si := make([]string, 0, len(user2))
for i := range user2 {
si = append(si, i)
}
sort.Strings(si)
for _, i := range si {
log.Printf("%s: %+v\n", i, user2[i])
}
// log.Printf("allUser: %+v", user2)
}
log.Printf("Groups: %+v", groups)
}
11 changes: 6 additions & 5 deletions ldap-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type LDAPClient struct {
Host string
ServerName string
UserFilter string // e.g. "(uid=%s)"
UserObjectFilter string // e.g. "(&(objectCategory=person)(objectClass=user))"
Conn *ldap.Conn
Port int
InsecureSkipVerify bool
Expand Down Expand Up @@ -134,8 +135,8 @@ func (lc *LDAPClient) Authenticate(username, password string) (bool, map[string]
return true, user, nil
}
// Authenticate authenticates the user against the ldap backend.
func (lc *LDAPClient) GetUsers() ([]map[string]string, error) {
users := make([]map[string]string, 1)
func (lc *LDAPClient) GetUsers() ([]map[string][]string, error) {
users := make([]map[string][]string, 1)
err := lc.Connect()
if err != nil {
return users, err
Expand All @@ -150,7 +151,7 @@ func (lc *LDAPClient) GetUsers() ([]map[string]string, error) {
}

attributes := append(lc.Attributes, "dn")
// Search for the given username

searchRequest := ldap.NewSearchRequest(
lc.Base,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
Expand All @@ -169,9 +170,9 @@ func (lc *LDAPClient) GetUsers() ([]map[string]string, error) {
}

for _, entry := range sr.Entries {
user := map[string]string{}
user := map[string][]string{}
for _, attr := range lc.Attributes {
user[attr] = entry.GetAttributeValue(attr)
user[attr] = entry.GetAttributeValues(attr)
}
users = append(users, user)
}
Expand Down

0 comments on commit 96edd97

Please sign in to comment.