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