diff --git a/ldap.go b/ldap.go index 8c46a4e..cf28bf6 100644 --- a/ldap.go +++ b/ldap.go @@ -210,6 +210,50 @@ func findLDAPAccountForDisplay(uname string) (User, error) { } return u, nil } +func updateLDAPAccountByUser(user User) error { + url := Conf.Ldap.Url + userdn := fmt.Sprintf("%v=%v,%v,%v", Conf.Ldap.UserAttr, user.Username, Conf.Ldap.UserOu, Conf.Ldap.LdapDc) + 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 err + } + defer l.Close() + err = l.Bind(binddn, Conf.Ldap.LdapPass) + if err != nil { + return err + } + result, err := l.Search(ldap.NewSearchRequest( + basedn, + ldap.ScopeWholeSubtree, + ldap.NeverDerefAliases, + 0, + 0, + false, + fmt.Sprintf("(&(objectClass=organizationalPerson)(%s=%s))", Conf.Ldap.UserAttr, user.Username), + []string{"dn"}, + nil, + )) + if err != nil { + return err + } + if len(result.Entries) != 1 { + err_text := fmt.Sprintf("Error finding login user: Wanted 1 result, got %v\n", len(result.Entries)) + return errors.New(err_text) + } + modify := ldap.NewModifyRequest(userdn, nil) + modify.Replace("mail", []string{user.Email}) + modify.Replace("givenName", []string{user.FirstName}) + modify.Replace("sn", []string{user.LastName}) + modify.Replace("displayName", []string{user.DisplayName}) + err = l.Modify(modify) + + if err != nil { + return err + } + return 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 b44fa29..68cb8df 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,9 @@ 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("/profile/view", profilePage).Methods("GET") + router.HandleFunc("/profile/edit", profileEditPage).Methods("GET") + router.HandleFunc("/profile/edit", profileEdit).Methods("POST") router.HandleFunc("/passwordreset", resetPageFront).Methods("GET") router.HandleFunc("/passwordreset", resetLookup).Methods("POST") router.HandleFunc("/passwordresetform", resetPageBack).Methods("GET") diff --git a/profile.go b/profile.go new file mode 100644 index 0000000..3c5f41d --- /dev/null +++ b/profile.go @@ -0,0 +1,39 @@ +package main + +import ( + "log" + "net/http" +) + +func profileEdit(res http.ResponseWriter, req *http.Request) { + 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 + } + dispname := req.FormValue("displayname") + firstname := req.FormValue("firstname") + lastname := req.FormValue("lastname") + email := req.FormValue("email") + if dispname != user.DisplayName || firstname != user.FirstName || lastname != user.LastName || email != user.Email { + log.Printf("updating user %v\n", user.Username) + user.DisplayName = dispname + user.FirstName = firstname + user.LastName = lastname + user.Email = email + err = updateLDAPAccountByUser(user) + if err != nil { + log.Printf("Error updating user account: %v\n", err) + http.Error(res, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } else { + http.Redirect(res, req, "/profile/view", 303) + } + } + return +} diff --git a/templates/index.html b/templates/index.html index a461339..5d07428 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,7 +2,7 @@ {{ template "header" .}} {{if .LoggedIn }}
- + {{else}} diff --git a/templates/profile.html b/templates/profile.html index fb2d9e0..28c19a9 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -21,6 +21,7 @@