guichet/profile.go

141 lines
3.5 KiB
Go
Raw Permalink Normal View History

package main
import (
"net/http"
"strings"
"github.com/go-ldap/ldap/v3"
)
type ProfileTplData struct {
2023-09-26 06:40:41 +00:00
User *LoggedUser
ErrorMessage string
Success bool
Mail string
DisplayName string
GivenName string
Surname string
Visibility string
Description string
ProfilePicture string
}
func handleProfile(w http.ResponseWriter, r *http.Request) {
2022-12-01 22:05:59 +00:00
templateProfile := getTemplate("profile.html")
2023-09-25 13:35:54 +00:00
user := RequireUserHtml(w, r)
if user == nil {
return
}
data := &ProfileTplData{
2023-09-26 06:40:41 +00:00
User: user,
ErrorMessage: "",
Success: false,
}
2023-09-25 13:35:54 +00:00
data.Mail = user.Entry.GetAttributeValue("mail")
data.DisplayName = user.Entry.GetAttributeValue("displayname")
data.GivenName = user.Entry.GetAttributeValue("givenname")
data.Surname = user.Entry.GetAttributeValue("sn")
data.Visibility = user.Entry.GetAttributeValue(FIELD_NAME_DIRECTORY_VISIBILITY)
data.Description = user.Entry.GetAttributeValue("description")
data.ProfilePicture = user.Entry.GetAttributeValue(FIELD_NAME_PROFILE_PICTURE)
if r.Method == "POST" {
//5MB maximum size files
r.ParseMultipartForm(5 << 20)
2020-02-10 14:26:02 +00: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"], ""), "")
visible := strings.TrimSpace(strings.Join(r.Form["visibility"], ""))
if visible != "" {
visible = "on"
}
data.Visibility = visible
2023-09-25 13:35:54 +00:00
profilePicture, err := uploadProfilePicture(w, r, user)
if err != nil {
data.ErrorMessage = err.Error()
}
if profilePicture != "" {
data.ProfilePicture = profilePicture
}
2023-09-25 13:35:54 +00:00
modify_request := ldap.NewModifyRequest(user.Login.Info.DN(), nil)
modify_request.Replace("displayname", []string{data.DisplayName})
modify_request.Replace("givenname", []string{data.GivenName})
modify_request.Replace("sn", []string{data.Surname})
modify_request.Replace("description", []string{data.Description})
modify_request.Replace(FIELD_NAME_DIRECTORY_VISIBILITY, []string{data.Visibility})
if data.ProfilePicture != "" {
modify_request.Replace(FIELD_NAME_PROFILE_PICTURE, []string{data.ProfilePicture})
}
2023-09-25 13:35:54 +00:00
err = user.Login.conn.Modify(modify_request)
if err != nil {
data.ErrorMessage = err.Error()
} else {
data.Success = true
}
}
templateProfile.Execute(w, data)
}
type PasswdTplData struct {
2023-09-25 13:35:54 +00:00
User *LoggedUser
2020-02-09 18:56:01 +00:00
ErrorMessage string
TooShortError bool
NoMatchError bool
Success bool
}
func handlePasswd(w http.ResponseWriter, r *http.Request) {
2022-12-01 22:05:59 +00:00
templatePasswd := getTemplate("passwd.html")
2023-09-25 13:35:54 +00:00
user := RequireUserHtml(w, r)
if user == nil {
return
}
data := &PasswdTplData{
2023-09-25 13:35:54 +00:00
User: user,
ErrorMessage: "",
Success: false,
}
if r.Method == "POST" {
r.ParseForm()
password := strings.Join(r.Form["password"], "")
password2 := strings.Join(r.Form["password2"], "")
2020-02-09 18:56:01 +00:00
if len(password) < 8 {
data.TooShortError = true
} else if password2 != password {
data.NoMatchError = true
} else {
2023-09-25 13:35:54 +00:00
modify_request := ldap.NewModifyRequest(user.Login.Info.DN(), nil)
pw, err := SSHAEncode(password)
2022-07-19 08:35:14 +00:00
if err == nil {
modify_request.Replace("userpassword", []string{pw})
2023-09-25 13:35:54 +00:00
err := user.Login.conn.Modify(modify_request)
2022-07-19 08:35:14 +00:00
if err != nil {
data.ErrorMessage = err.Error()
} else {
data.Success = true
}
} else {
2022-07-19 08:35:14 +00:00
data.ErrorMessage = err.Error()
}
}
}
templatePasswd.Execute(w, data)
}