gvguichet/profile.go

143 lines
3.8 KiB
Go
Raw Normal View History

package main
import (
"net/http"
"strings"
2023-07-17 10:19:01 +02:00
"log"
"fmt"
"github.com/go-ldap/ldap/v3"
)
type ProfileTplData struct {
Status *LoginStatus
ErrorMessage string
Success bool
Mail string
DisplayName string
GivenName string
Surname string
Description string
}
2023-07-17 10:19:01 +02:00
//ProfilePicture string
//Visibility string
func handleProfile(w http.ResponseWriter, r *http.Request) {
2022-12-01 23:05:59 +01:00
templateProfile := getTemplate("profile.html")
login := checkLogin(w, r)
if login == nil {
return
}
data := &ProfileTplData{
Status: login,
ErrorMessage: "",
Success: false,
}
data.Mail = login.UserEntry.GetAttributeValue("mail")
2023-07-17 10:19:01 +02:00
data.DisplayName = login.UserEntry.GetAttributeValue("displayName")
data.GivenName = login.UserEntry.GetAttributeValue("givenName")
data.Surname = login.UserEntry.GetAttributeValue("sn")
2023-07-17 10:19:01 +02:00
// data.Visibility = login.UserEntry.GetAttributeValue(FIELD_NAME_DIRECTORY_VISIBILITY)
data.Description = login.UserEntry.GetAttributeValue("description")
2023-07-17 10:19:01 +02:00
//data.ProfilePicture = login.UserEntry.GetAttributeValue(FIELD_NAME_PROFILE_PICTURE)
if r.Method == "POST" {
//5MB maximum size files
r.ParseMultipartForm(5 << 20)
2020-02-10 15:26:02 +01:00
data.DisplayName = strings.TrimSpace(strings.Join(r.Form["display_name"], ""))
data.GivenName = strings.TrimSpace(strings.Join(r.Form["given_name"], ""))
data.Surname = strings.TrimSpace(strings.Join(r.Form["surname"], ""))
data.Description = strings.Trim(strings.Join(r.Form["description"], ""), "")
2023-07-17 10:19:01 +02:00
/*
visible := strings.TrimSpace(strings.Join(r.Form["visibility"], ""))
if visible != "" {
visible = "on"
2023-07-17 10:19:01 +02:00
} else {
visible = "off"
}
data.Visibility = visible
2023-07-17 10:19:01 +02:00
*/
/*
profilePicture, err := uploadProfilePicture(w, r, login)
if err != nil {
data.ErrorMessage = err.Error()
}
2023-07-17 10:19:01 +02:00
if profilePicture != "" {
data.ProfilePicture = profilePicture
}
2023-07-17 10:19:01 +02:00
*/
modify_request := ldap.NewModifyRequest(login.Info.DN, nil)
modify_request.Replace("displayname", []string{data.DisplayName})
modify_request.Replace("givenname", []string{data.GivenName})
2023-07-17 10:19:01 +02:00
modify_request.Replace("surname", []string{data.Surname})
modify_request.Replace("description", []string{data.Description})
2023-07-17 10:19:01 +02:00
//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)
2023-07-17 17:23:03 +02:00
// 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.ErrorMessage = err.Error()
} else {
data.Success = true
}
}
templateProfile.Execute(w, data)
}
type PasswdTplData struct {
2020-02-09 19:56:01 +01:00
Status *LoginStatus
ErrorMessage string
TooShortError bool
NoMatchError bool
Success bool
}
func handlePasswd(w http.ResponseWriter, r *http.Request) {
2022-12-01 23:05:59 +01:00
templatePasswd := getTemplate("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"], "")
2020-02-09 19:56:01 +01:00
if len(password) < 8 {
data.TooShortError = true
} else if password2 != password {
data.NoMatchError = true
} else {
2023-07-17 10:19:01 +02:00
passwordModifyRequest := ldap.NewPasswordModifyRequest(login.Info.DN,"",password)
_, err := login.conn.PasswordModify(passwordModifyRequest)
if err != nil {
data.ErrorMessage = err.Error()
} else {
data.Success = true
}
}
}
templatePasswd.Execute(w, data)
}