guichet/view-user.go

175 lines
5 KiB
Go
Raw Normal View History

2023-07-26 06:39:41 +00:00
package main
import (
2023-07-26 07:00:42 +00:00
// b64 "encoding/base64"
2023-07-27 19:52:32 +00:00
"fmt"
2023-07-26 07:00:42 +00:00
// "log"
2023-07-27 19:52:32 +00:00
"log"
2023-07-26 06:39:41 +00:00
"net/http"
"strings"
2023-07-27 19:52:32 +00:00
"github.com/go-ldap/ldap/v3"
2023-07-26 07:00:42 +00:00
// "github.com/gorilla/mux"
2023-07-26 06:39:41 +00:00
)
2023-07-26 09:05:47 +00:00
func handleUserWait(w http.ResponseWriter, r *http.Request) {
2023-07-27 19:34:58 +00:00
templateUser := getTemplate("user/wait.html")
templateUser.Execute(w, HomePageData{
2023-07-26 09:16:52 +00:00
Common: NestedCommonTplData{
CanAdmin: false,
LoggedIn: false,
},
})
2023-07-26 09:05:47 +00:00
}
2023-07-27 19:52:32 +00:00
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")
2023-07-27 20:19:10 +00:00
action := r.FormValue("action")
2023-07-27 19:52:32 +00:00
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 {
2023-07-27 20:02:07 +00:00
http.Error(w, fmt.Sprintf("Error adding the email: %v", modifyRequest), http.StatusInternalServerError)
2023-07-27 19:52:32 +00:00
return
}
2023-07-27 20:10:04 +00:00
} else if action == "Delete" {
2023-07-27 19:52:32 +00:00
modifyRequest := ldap.NewModifyRequest(login.Info.DN, nil)
modifyRequest.Delete("mail", []string{email})
2023-07-27 20:13:11 +00:00
log.Printf("handleUserMail %v", modifyRequest)
2023-07-27 19:52:32 +00:00
err = login.conn.Modify(modifyRequest)
if err != nil {
2023-07-27 20:16:44 +00:00
log.Printf("handleUserMail DeleteMail %v", err)
2023-07-27 19:52:32 +00:00
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")
}
2023-07-26 07:00:42 +00:00
func handleUser(w http.ResponseWriter, r *http.Request) {
2023-07-27 19:34:58 +00:00
templateUser := getTemplate("user.html")
2023-07-26 06:39:41 +00:00
login := checkLogin(w, r)
if login == nil {
2023-07-26 07:11:51 +00:00
http.Redirect(w, r, "/", http.StatusFound)
2023-07-26 06:39:41 +00:00
return
}
data := &ProfileTplData{
Login: NestedLoginTplData{
Status: login,
Login: login,
},
Common: NestedCommonTplData{
CanAdmin: login.Common.CanAdmin,
LoggedIn: true,
ErrorMessage: "",
Success: false,
},
}
data.Mail = login.UserEntry.GetAttributeValue("mail")
data.DisplayName = login.UserEntry.GetAttributeValue("displayName")
data.GivenName = login.UserEntry.GetAttributeValue("givenName")
data.Surname = login.UserEntry.GetAttributeValue("sn")
2023-07-26 09:27:49 +00:00
data.OtherMailbox = login.UserEntry.GetAttributeValue("carLicense")
2023-07-27 19:34:58 +00:00
data.MailValues = login.UserEntry.GetAttributeValues("mail")
2023-07-26 06:39:41 +00:00
// data.Visibility = login.UserEntry.GetAttributeValue(FIELD_NAME_DIRECTORY_VISIBILITY)
data.Description = login.UserEntry.GetAttributeValue("description")
//data.ProfilePicture = login.UserEntry.GetAttributeValue(FIELD_NAME_PROFILE_PICTURE)
if r.Method == "POST" {
//5MB maximum size files
r.ParseMultipartForm(5 << 20)
user := User{
2023-07-26 09:27:49 +00:00
DN: login.Info.DN,
GivenName: strings.TrimSpace(strings.Join(r.Form["given_name"], "")),
DisplayName: strings.TrimSpace(strings.Join(r.Form["display_name"], "")),
Mail: strings.TrimSpace(strings.Join(r.Form["mail"], "")),
SN: strings.TrimSpace(strings.Join(r.Form["surname"], "")),
OtherMailbox: strings.TrimSpace(strings.Join(r.Form["othermailbox"], "")),
Description: strings.TrimSpace(strings.Join(r.Form["description"], "")),
2023-07-26 06:39:41 +00:00
// Password: ,
2023-07-26 09:27:49 +00:00
//UID: ,
// CN: ,
2023-07-26 06:39:41 +00:00
}
if user.DisplayName != "" {
err := modify(user, config, login.conn)
if err != nil {
2023-07-26 07:00:42 +00:00
data.Common.ErrorMessage = "handleUser : " + err.Error()
2023-07-26 06:39:41 +00:00
} else {
data.Common.Success = true
}
}
findUser, err := get(user, config, login.conn)
if err != nil {
2023-07-26 07:00:42 +00:00
data.Common.ErrorMessage = "handleUser : " + err.Error()
2023-07-26 06:39:41 +00:00
}
data.DisplayName = findUser.DisplayName
data.GivenName = findUser.GivenName
data.Surname = findUser.SN
data.Description = findUser.Description
data.Mail = findUser.Mail
data.Common.LoggedIn = false
/*
visible := strings.TrimSpace(strings.Join(r.Form["visibility"], ""))
if visible != "" {
visible = "on"
} else {
visible = "off"
}
data.Visibility = visible
*/
/*
profilePicture, err := uploadProfilePicture(w, r, login)
if err != nil {
data.Common.ErrorMessage = err.Error()
}
if profilePicture != "" {
data.ProfilePicture = profilePicture
}
*/
//modify_request.Replace(FIELD_NAME_DIRECTORY_VISIBILITY, []string{data.Visibility})
//modify_request.Replace(FIELD_NAME_DIRECTORY_VISIBILITY, []string{"on"})
//if data.ProfilePicture != "" {
// modify_request.Replace(FIELD_NAME_PROFILE_PICTURE, []string{data.ProfilePicture})
// }
// err := login.conn.Modify(modify_request)
// log.Printf(fmt.Sprintf("Profile:079: %v",modify_request))
// log.Printf(fmt.Sprintf("Profile:079: %v",err))
// log.Printf(fmt.Sprintf("Profile:079: %v",data))
// if err != nil {
// data.Common.ErrorMessage = err.Error()
// } else {
// data.Common.Success = true
// }
}
2023-07-27 19:52:32 +00:00
log.Printf("handleUser : %v", data)
2023-07-27 19:34:58 +00:00
// templateUser.Execute(w, data)
execTemplate(w, templateUser, data.Common, data.Login, *config, data)
2023-07-26 06:39:41 +00:00
}