From ec6c77aa017c869b5050582008e2eb59451a35f3 Mon Sep 17 00:00:00 2001 From: Chris Mann Date: Thu, 27 Jul 2023 21:52:32 +0200 Subject: [PATCH] Handle multiple mail values --- controller.go | 1 + view-user.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/controller.go b/controller.go index 606f2a9..ef286f1 100644 --- a/controller.go +++ b/controller.go @@ -70,6 +70,7 @@ func makeGVRouter() (*mux.Router, error) { r.HandleFunc("/user", handleUser) r.HandleFunc("/user/new", handleInviteNewAccount) r.HandleFunc("/user/wait", handleUserWait) + r.HandleFunc("/user/mail", handleUserMail) r.HandleFunc("/picture/{name}", handleDownloadPicture) diff --git a/view-user.go b/view-user.go index d30c718..1141625 100644 --- a/view-user.go +++ b/view-user.go @@ -2,10 +2,13 @@ package main import ( // b64 "encoding/base64" - // "fmt" + "fmt" // "log" + "log" "net/http" "strings" + + "github.com/go-ldap/ldap/v3" // "github.com/gorilla/mux" ) @@ -19,6 +22,56 @@ func handleUserWait(w http.ResponseWriter, r *http.Request) { }) } +func handleUserMail(w http.ResponseWriter, r *http.Request) { + login := checkLogin(w, r) + if login == nil { + http.Redirect(w, r, "/", http.StatusFound) + return + } + email := r.FormValue("email") + action := r.FormValue("add") + index := r.FormValue("index") + var err error + if action == "Add" { + // Add the new mail value to the entry + modifyRequest := ldap.NewModifyRequest(login.Info.DN, nil) + modifyRequest.Add("mail", []string{email}) + + err = login.conn.Modify(modifyRequest) + if err != nil { + http.Error(w, fmt.Sprintf("Error adding the email: %s", err), http.StatusInternalServerError) + return + } + } else if action == "Delete" && index != "" { + // Delete the specified mail value from the entry + i := strings.Index(index, ":") + if i > 0 { + index = index[:i] + } + i = strings.Index(index, "/") + if i > 0 { + index = index[:i] + } + + modifyRequest := ldap.NewModifyRequest(login.Info.DN, nil) + modifyRequest.Delete("mail", []string{email}) + + err = login.conn.Modify(modifyRequest) + if err != nil { + http.Error(w, fmt.Sprintf("Error deleting the email: %s", err), http.StatusInternalServerError) + return + } + } + + message := fmt.Sprintf("Mail value updated successfully to: %s", email) + http.Redirect(w, r, "/user?message="+message, http.StatusSeeOther) + +} + +func toInteger(index string) { + panic("unimplemented") +} + func handleUser(w http.ResponseWriter, r *http.Request) { templateUser := getTemplate("user.html") @@ -123,6 +176,8 @@ func handleUser(w http.ResponseWriter, r *http.Request) { } + log.Printf("handleUser : %v", data) + // templateUser.Execute(w, data) execTemplate(w, templateUser, data.Common, data.Login, *config, data) }