From 2624034fd71870832cf75a6ab46ad73bf41e35d4 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 31 Mar 2021 16:12:35 -0400 Subject: [PATCH] show groups in profile --- ldap.go | 25 ++++++++++++++++++------- templates/profile.html | 9 ++++++++- user.go | 14 ++++++++------ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/ldap.go b/ldap.go index 1f9ad74..e4d4edf 100644 --- a/ldap.go +++ b/ldap.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "strconv" + "strings" "github.com/go-ldap/ldap" ) @@ -251,7 +252,7 @@ func findLDAPAccountForDisplay(uname string) (User, error) { 0, false, fmt.Sprintf("(&(objectClass=organizationalPerson)(%s=%s))", Conf.Ldap.UserAttr, uname), - []string{"cn", "sn", "givenName", "displayName", "mail", "employeeNumber"}, + []string{"cn", "sn", "givenName", "displayName", "mail", "employeeNumber", "memberOf"}, nil, )) if err != nil { @@ -262,13 +263,23 @@ func findLDAPAccountForDisplay(uname string) (User, error) { return User{}, errors.New(err_text) } entry := result.Entries[0] + groups := entry.GetAttributeValues("memberOf") + fg := make([]string, 0) + for _, group := range groups { + group_s := strings.Split(group, ",") + group_cn := group_s[0] + fg = append(fg, strings.Trim(group_cn, "cn=")) + } + u := User{ - Username: entry.GetAttributeValue("cn"), - FirstName: entry.GetAttributeValue("givenName"), - LastName: entry.GetAttributeValue("sn"), - DisplayName: entry.GetAttributeValue("displayName"), - Email: entry.GetAttributeValue("mail"), - ID: entry.GetAttributeValue("employeeNumber"), + Username: entry.GetAttributeValue("cn"), + FirstName: entry.GetAttributeValue("givenName"), + LastName: entry.GetAttributeValue("sn"), + DisplayName: entry.GetAttributeValue("displayName"), + Email: entry.GetAttributeValue("mail"), + ID: entry.GetAttributeValue("employeeNumber"), + Groups: groups, + FriendlyGroups: fg, } return u, nil } diff --git a/templates/profile.html b/templates/profile.html index 28c19a9..bb61df5 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -1,6 +1,6 @@ {{ define "profile" }} {{ template "header" .}} -

Profile

+

User Profile

@@ -20,6 +20,13 @@ + +
Username: {{ .User.Username }}
User ID: {{ .User.ID }}
+

User Groups

+ + {{range .User.FriendlyGroups}} + + {{end}}
Group: {{.}}

Edit Profile

{{ template "footer" .}} diff --git a/user.go b/user.go index 431f2c6..095e84e 100644 --- a/user.go +++ b/user.go @@ -1,12 +1,14 @@ package main type User struct { - Username string - FirstName string - LastName string - DisplayName string - Email string - ID string + Username string + FirstName string + LastName string + DisplayName string + Email string + ID string + Groups []string + FriendlyGroups []string } //TODO Start using User as a proper model