show groups in profile

This commit is contained in:
stryan 2021-03-31 16:12:35 -04:00
parent 8e8e67666c
commit 2624034fd7
3 changed files with 34 additions and 14 deletions

25
ldap.go
View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"strconv" "strconv"
"strings"
"github.com/go-ldap/ldap" "github.com/go-ldap/ldap"
) )
@ -251,7 +252,7 @@ func findLDAPAccountForDisplay(uname string) (User, error) {
0, 0,
false, false,
fmt.Sprintf("(&(objectClass=organizationalPerson)(%s=%s))", Conf.Ldap.UserAttr, uname), 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, nil,
)) ))
if err != nil { if err != nil {
@ -262,13 +263,23 @@ func findLDAPAccountForDisplay(uname string) (User, error) {
return User{}, errors.New(err_text) return User{}, errors.New(err_text)
} }
entry := result.Entries[0] 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{ u := User{
Username: entry.GetAttributeValue("cn"), Username: entry.GetAttributeValue("cn"),
FirstName: entry.GetAttributeValue("givenName"), FirstName: entry.GetAttributeValue("givenName"),
LastName: entry.GetAttributeValue("sn"), LastName: entry.GetAttributeValue("sn"),
DisplayName: entry.GetAttributeValue("displayName"), DisplayName: entry.GetAttributeValue("displayName"),
Email: entry.GetAttributeValue("mail"), Email: entry.GetAttributeValue("mail"),
ID: entry.GetAttributeValue("employeeNumber"), ID: entry.GetAttributeValue("employeeNumber"),
Groups: groups,
FriendlyGroups: fg,
} }
return u, nil return u, nil
} }

View File

@ -1,6 +1,6 @@
{{ define "profile" }} {{ define "profile" }}
{{ template "header" .}} {{ template "header" .}}
<h1> Profile </h1> <h1>User Profile </h1>
<table> <table>
<tr> <tr>
<th>Username: </th><td>{{ .User.Username }}</td> <th>Username: </th><td>{{ .User.Username }}</td>
@ -20,6 +20,13 @@
<tr> <tr>
<th>User ID: </th><td>{{ .User.ID }}</td> <th>User ID: </th><td>{{ .User.ID }}</td>
</tr> </tr>
</table>
<h1>User Groups </h1>
<table>
{{range .User.FriendlyGroups}}
<tr><th>Group: </th><td>{{.}}</td></tr>
{{end}}
</table> </table>
<p><a href="/profile/edit">Edit Profile</a></p> <p><a href="/profile/edit">Edit Profile</a></p>
{{ template "footer" .}} {{ template "footer" .}}

14
user.go
View File

@ -1,12 +1,14 @@
package main package main
type User struct { type User struct {
Username string Username string
FirstName string FirstName string
LastName string LastName string
DisplayName string DisplayName string
Email string Email string
ID string ID string
Groups []string
FriendlyGroups []string
} }
//TODO Start using User as a proper model //TODO Start using User as a proper model