add profile page

This commit is contained in:
stryan 2020-09-24 16:39:52 -04:00
parent f7e55b9ab1
commit c7d583c622
6 changed files with 108 additions and 0 deletions

42
ldap.go
View File

@ -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)

View File

@ -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")

View File

@ -2,6 +2,7 @@
{{ template "header" .}}
{{if .LoggedIn }}
<p><a href="/token">Get Token</a></p>
<p><a href="/profile">Profile</a></p>
{{else}}
<p><a href="/register">Register</a></p>
<p><a href="/passwordreset">Reset Password</a></p>

26
templates/profile.html Normal file
View File

@ -0,0 +1,26 @@
{{ define "profile" }}
{{ template "header" .}}
<h1> Profile </h1>
<table>
<tr>
<th>Username: </th><td>{{ .User.Username }}</td>
</tr>
<tr>
<th>Display Name: </th><td>{{ .User.DisplayName }}</td>
</tr>
<tr>
<th>First Name: </th><td>{{ .User.FirstName }}</td>
</tr>
<tr>
<th>Last Name: </th><td>{{ .User.LastName }}</td>
</tr>
<tr>
<th>Email Address: </th><td>{{ .User.Email }}</td>
</tr>
<tr>
<th>User ID: </th><td>{{ .User.ID }}</td>
</tr>
</table>
{{ template "footer" .}}
{{end}}

12
user.go Normal file
View File

@ -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

26
web.go
View File

@ -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)