diff --git a/ldap.go b/ldap.go index 1726be3..8c46a4e 100644 --- a/ldap.go +++ b/ldap.go @@ -168,6 +168,48 @@ func findLDAPAccountByEmail(email string) (string, error) { return entry.GetAttributeValue(Conf.Ldap.UserAttr), nil } +func findLDAPAccountForDisplay(uname string) (User, error) { + url := Conf.Ldap.Url + binddn := fmt.Sprintf("%v,%v", Conf.Ldap.AdminUser, Conf.Ldap.LdapDc) + basedn := fmt.Sprintf("%v,%v", Conf.Ldap.UserOu, Conf.Ldap.LdapDc) + l, err := ldap.DialURL(url) + if err != nil { + return User{}, err + } + defer l.Close() + err = l.Bind(binddn, Conf.Ldap.LdapPass) + if err != nil { + return User{}, err + } + result, err := l.Search(ldap.NewSearchRequest( + basedn, + ldap.ScopeWholeSubtree, + ldap.NeverDerefAliases, + 0, + 0, + false, + fmt.Sprintf("(&(objectClass=organizationalPerson)(%s=%s))", Conf.Ldap.UserAttr, uname), + []string{"cn", "sn", "givenName", "displayName", "mail", "employeeNumber"}, + nil, + )) + if err != nil { + return User{}, err + } + if len(result.Entries) != 1 { + err_text := fmt.Sprintf("Error finding user: Wanted 1 result, got %v\n", len(result.Entries)) + return User{}, errors.New(err_text) + } + entry := result.Entries[0] + 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"), + } + return u, nil +} func findLDAPMaxID() (int, error) { url := Conf.Ldap.Url binddn := fmt.Sprintf("%v,%v", Conf.Ldap.AdminUser, Conf.Ldap.LdapDc) diff --git a/main.go b/main.go index 8728b9c..b44fa29 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ func main() { router.HandleFunc("/login", login).Methods("POST") router.HandleFunc("/logout", logoutPage).Methods("GET") router.HandleFunc("/token", tokenPage).Methods("GET") + router.HandleFunc("/profile", profilePage).Methods("GET") router.HandleFunc("/passwordreset", resetPageFront).Methods("GET") router.HandleFunc("/passwordreset", resetLookup).Methods("POST") router.HandleFunc("/passwordresetform", resetPageBack).Methods("GET") diff --git a/templates/index.html b/templates/index.html index ace7ca6..a461339 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,6 +2,7 @@ {{ template "header" .}} {{if .LoggedIn }}

Get Token

+

Profile

{{else}}

Register

Reset Password

diff --git a/templates/profile.html b/templates/profile.html new file mode 100644 index 0000000..fb2d9e0 --- /dev/null +++ b/templates/profile.html @@ -0,0 +1,26 @@ +{{ define "profile" }} +{{ template "header" .}} +

Profile

+ + + + + + + + + + + + + + + + + + + +
Username: {{ .User.Username }}
Display Name: {{ .User.DisplayName }}
First Name: {{ .User.FirstName }}
Last Name: {{ .User.LastName }}
Email Address: {{ .User.Email }}
User ID: {{ .User.ID }}
+{{ template "footer" .}} +{{end}} + diff --git a/user.go b/user.go new file mode 100644 index 0000000..431f2c6 --- /dev/null +++ b/user.go @@ -0,0 +1,12 @@ +package main + +type User struct { + Username string + FirstName string + LastName string + DisplayName string + Email string + ID string +} + +//TODO Start using User as a proper model diff --git a/web.go b/web.go index 21d2606..e1ce3f1 100644 --- a/web.go +++ b/web.go @@ -5,6 +5,32 @@ import ( "net/http" ) +func profilePage(res http.ResponseWriter, req *http.Request) { + log.Println("GET /profile") + uname := getUserName(req) + if uname == "" { + http.Redirect(res, req, "/", 302) + } + user, err := findLDAPAccountForDisplay(uname) + if err != nil { + log.Printf("Error loading profile: %v\n", err) + http.Error(res, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + data := struct { + Title string + Username string + LoggedIn bool + User User + }{ + "Profile", + uname, + true, + user, + } + tpl.ExecuteTemplate(res, "profile", data) +} + func resetPageFront(res http.ResponseWriter, req *http.Request) { log.Println("GET /passwordreset") u := getUserName(req)