aboutsummaryrefslogtreecommitdiff
path: root/profile.go
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-02-09 18:03:10 +0100
committerAlex Auvolat <alex@adnab.me>2020-02-09 18:03:10 +0100
commite9140c5a66ff9035dc973e93aad9c0ed909b6f13 (patch)
tree17b075011d941af2fb90c72e285923fca5f6759f /profile.go
parentedc75d18b792e19157107050abd35d832e46665a (diff)
downloadguichet-e9140c5a66ff9035dc973e93aad9c0ed909b6f13.tar.gz
guichet-e9140c5a66ff9035dc973e93aad9c0ed909b6f13.zip
Split out profile editing in profile.go
Diffstat (limited to 'profile.go')
-rw-r--r--profile.go107
1 files changed, 107 insertions, 0 deletions
diff --git a/profile.go b/profile.go
new file mode 100644
index 0000000..00e93dd
--- /dev/null
+++ b/profile.go
@@ -0,0 +1,107 @@
+package main
+
+import (
+ "html/template"
+ "net/http"
+ "strings"
+
+ "github.com/go-ldap/ldap/v3"
+)
+
+type ProfileTplData struct {
+ Status *LoginStatus
+ ErrorMessage string
+ Success bool
+ Mail string
+ DisplayName string
+ GivenName string
+ Surname string
+}
+
+func handleProfile(w http.ResponseWriter, r *http.Request) {
+ templateProfile := template.Must(template.ParseFiles("templates/layout.html", "templates/profile.html"))
+
+ login := checkLogin(w, r)
+ if login == nil {
+ return
+ }
+
+ data := &ProfileTplData{
+ Status: login,
+ ErrorMessage: "",
+ Success: false,
+ }
+
+ if r.Method == "POST" {
+ r.ParseForm()
+
+ data.Mail = strings.Join(r.Form["mail"], "")
+ data.DisplayName = strings.Join(r.Form["display_name"], "")
+ data.GivenName = strings.Join(r.Form["given_name"], "")
+ data.Surname = strings.Join(r.Form["surname"], "")
+
+ modify_request := ldap.NewModifyRequest(login.Info.DN, nil)
+ modify_request.Replace("mail", []string{data.Mail})
+ modify_request.Replace("displayname", []string{data.DisplayName})
+ modify_request.Replace("givenname", []string{data.GivenName})
+ modify_request.Replace("sn", []string{data.Surname})
+
+ err := login.conn.Modify(modify_request)
+ if err != nil {
+ data.ErrorMessage = err.Error()
+ } else {
+ data.Success = true
+ }
+ } else {
+ data.Mail = login.UserEntry.GetAttributeValue("mail")
+ data.DisplayName = login.UserEntry.GetAttributeValue("displayname")
+ data.GivenName = login.UserEntry.GetAttributeValue("givenname")
+ data.Surname = login.UserEntry.GetAttributeValue("sn")
+ }
+
+ templateProfile.Execute(w, data)
+}
+
+type PasswdTplData struct {
+ Status *LoginStatus
+ ErrorMessage string
+ NoMatchError bool
+ Success bool
+}
+
+func handlePasswd(w http.ResponseWriter, r *http.Request) {
+ templatePasswd := template.Must(template.ParseFiles("templates/layout.html", "templates/passwd.html"))
+
+ login := checkLogin(w, r)
+ if login == nil {
+ return
+ }
+
+ data := &PasswdTplData{
+ Status: login,
+ ErrorMessage: "",
+ Success: false,
+ }
+
+ if r.Method == "POST" {
+ r.ParseForm()
+
+ password := strings.Join(r.Form["password"], "")
+ password2 := strings.Join(r.Form["password2"], "")
+
+ if password2 != password {
+ data.NoMatchError = true
+ } else {
+ modify_request := ldap.NewModifyRequest(login.Info.DN, nil)
+ modify_request.Replace("userpassword", []string{SSHAEncode([]byte(password))})
+ err := login.conn.Modify(modify_request)
+ if err != nil {
+ data.ErrorMessage = err.Error()
+ } else {
+ data.Success = true
+ }
+ }
+ }
+
+ templatePasswd.Execute(w, data)
+}