From a415ea4a963b75f01b4fb127e7358885cfeb39c3 Mon Sep 17 00:00:00 2001 From: Chris Mann Date: Thu, 20 Jul 2023 13:46:32 +0200 Subject: [PATCH] Trying a utility file --- invite.go | 62 ++++-------- templates/home.html | 2 +- templates/invite_new_account.html | 154 ++++++++++++++++-------------- utils.go | 5 + 4 files changed, 108 insertions(+), 115 deletions(-) diff --git a/invite.go b/invite.go index 37b565d..e2fe929 100644 --- a/invite.go +++ b/invite.go @@ -52,10 +52,12 @@ func handleInvitationCode(w http.ResponseWriter, r *http.Request) { code := mux.Vars(r)["code"] code_id, code_pw := readCode(code) - l := ldapOpen(w) - if l == nil { - return - } + login := checkLogin(w, r) + + // l := ldapOpen(w) + // if l == nil { + // return + // } inviteDn := config.InvitationNameAttr + "=" + code_id + "," + config.InvitationBaseDN err := l.Bind(inviteDn, code_pw) @@ -117,10 +119,14 @@ func handleNewAccount(w http.ResponseWriter, r *http.Request, l *ldap.Conn, invi if r.Method == "POST" { r.ParseForm() - data.Username = strings.TrimSpace(strings.Join(r.Form["username"], "")) - data.DisplayName = strings.TrimSpace(strings.Join(r.Form["displayname"], "")) - data.GivenName = strings.TrimSpace(strings.Join(r.Form["givenname"], "")) - data.Surname = strings.TrimSpace(strings.Join(r.Form["surname"], "")) + newUser := NewUser{} + + newUser.CN = strings.TrimSpace(strings.Join(r.Form["username"], "@lesgv.com")) + newUser.DisplayName = strings.TrimSpace(strings.Join(r.Form["displayname"], "")) + newUser.GivenName = strings.TrimSpace(strings.Join(r.Form["givenname"], "")) + newUser.SN = strings.TrimSpace(strings.Join(r.Form["surname"], "")) + newUser.UID = strings.TrimSpace(strings.Join(r.Form["username"], "")) + newUser.Mail = strings.TrimSpace(strings.Join(r.Form["mail"], "")) password1 := strings.Join(r.Form["password"], "") password2 := strings.Join(r.Form["password2"], "") @@ -248,19 +254,12 @@ func handleInviteSendCode(w http.ResponseWriter, r *http.Request) { // carLicense - if r.Method == "POST" { r.ParseForm() data := &SendCodeData{ WebBaseAddress: config.WebAddress, } - - - - - - // modify_request := ldap.NewModifyRequest(login.UserEntry.DN, nil) // // choice := strings.Join(r.Form["choice"], "") // // sendto := strings.Join(r.Form["sendto"], "") @@ -277,7 +276,7 @@ func handleInviteSendCode(w http.ResponseWriter, r *http.Request) { // data.CodeDisplay = code // } log.Printf(fmt.Sprintf("279: %v %v %v", code, code_id, code_pw)) - addReq := ldap.NewAddRequest("documentIdentifier=" + code_id + "," + config.InvitationBaseDN,nil) + addReq := ldap.NewAddRequest("documentIdentifier="+code_id+","+config.InvitationBaseDN, nil) addReq.Attribute("objectClass", []string{"top", "document", "simpleSecurityObject"}) addReq.Attribute("cn", []string{code}) addReq.Attribute("userPassword", []string{code_pw}) @@ -293,22 +292,14 @@ func handleInviteSendCode(w http.ResponseWriter, r *http.Request) { data.CodeDisplay = code } - - templateInviteSendCode.Execute(w, data) - - - - - // if choice == "display" || choice == "send" { // log.Printf(fmt.Sprintf("260: %v %v %v %v", login, choice, sendto, data)) // trySendCode(login, choice, sendto, data) // } } - } func trySendCode(login *LoginStatus, choice string, sendto string, data *SendCodeData) { @@ -318,11 +309,6 @@ func trySendCode(login *LoginStatus, choice string, sendto string, data *SendCod log.Printf(fmt.Sprintf("272: %v %v %v", code, code_id, code_pw)) // Create invitation object in database - - - - - // len_base_dn := len(strings.Split(config.BaseDN, ",")) // dn_split := strings.Split(super_dn, ",") // for i := len_base_dn + 1; i <= len(dn_split); i++ { @@ -360,11 +346,11 @@ func trySendCode(login *LoginStatus, choice string, sendto string, data *SendCod // req := ldap.NewAddRequest(dn, nil) // req.Attribute("objectclass", object_class) // // req.Attribute("mail", []string{data.IdValue}) - // /* + // /* // if data.StructuralObjectClass != "" { // req.Attribute("structuralobjectclass", []string{data.StructuralObjectClass}) // } - // */ + // */ // if data.DisplayName != "" { // req.Attribute("displayname", []string{data.DisplayName}) // } @@ -384,9 +370,9 @@ func trySendCode(login *LoginStatus, choice string, sendto string, data *SendCod // req.Attribute("description", []string{data.Description}) // } // err := login.conn.Add(req) - // // log.Printf(fmt.Sprintf("899: %v",err)) - // // log.Printf(fmt.Sprintf("899: %v",req)) - // // log.Printf(fmt.Sprintf("899: %v",data)) + // // log.Printf(fmt.Sprintf("899: %v",err)) + // // log.Printf(fmt.Sprintf("899: %v",req)) + // // log.Printf(fmt.Sprintf("899: %v",data)) // if err != nil { // data.Error = err.Error() // } else { @@ -397,13 +383,6 @@ func trySendCode(login *LoginStatus, choice string, sendto string, data *SendCod // } // } - - - - - - - // inviteDn := config.InvitationNameAttr + "=" + code_id + "," + config.InvitationBaseDN // req := ldap.NewAddRequest(inviteDn, nil) // pw, err := SSHAEncode(code_pw) @@ -493,4 +472,3 @@ func readCode(code string) (code_id string, code_pw string) { code_pw = hex.EncodeToString(pw_hash[:16]) return code_id, code_pw } - diff --git a/templates/home.html b/templates/home.html index 74a48aa..6442e88 100644 --- a/templates/home.html +++ b/templates/home.html @@ -43,7 +43,7 @@ Inviter des gens sur {{.Org}}
- Envoyer un code d'invitation + Créer un nouveau compte directement
diff --git a/templates/invite_new_account.html b/templates/invite_new_account.html index 8727a62..48f39c1 100644 --- a/templates/invite_new_account.html +++ b/templates/invite_new_account.html @@ -5,76 +5,86 @@

Création d'un nouveau compte

Retour - - {{if .ErrorMessage}} -
Impossible de créer le compte. -
{{ .ErrorMessage }}
-
- {{end}} - {{if .WarningMessage}} -
Des erreurs se sont produites, le compte pourrait ne pas être totalement fonctionnel. -
{{ .WarningMessage }}
-
- {{end}} - {{if .Success}} -
- Le compe a été créé ! - Rendez-vous sur la page d'accueil pour vous connecter avec ce nouveau compte. -
- {{else}} -
-
Renseignements obligatoires
-
- - - - Votre identifiant doit être en minuscule. - -
- {{if .ErrorInvalidUsername}} -
- Nom d'utilisateur invalide. Ne peut contenir que les caractères suivants : chiffres, lettres minuscules, point, tiret bas (_) et tiret du milieu (-). -
- {{end}} - {{if .ErrorUsernameTaken}} -
- Ce nom d'utilisateur est déjà pris. -
- {{end}} -
- - - - La seule contrainte est que votre mot de passe doit faire au moins 8 caractères. Utilisez chiffres, majuscules, et caractères spéciaux sans modération ! - -
- {{if .ErrorPasswordTooShort}} -
- Le mot de passe choisi est trop court (minimum 8 caractères). -
- {{end}} -
- - -
- {{if .ErrorPasswordMismatch}} -
- Les deux mots de passe entrés ne correspondent pas. -
- {{end}} -
- - -
-
- - -
-
- - -
- -
- {{end}} +{{if .ErrorMessage}} +
Impossible de créer le compte. +
{{ .ErrorMessage }}
+
{{end}} +{{if .WarningMessage}} +
Des erreurs se sont produites, le compte pourrait ne pas être totalement + fonctionnel. +
{{ .WarningMessage }}
+
+{{end}} +{{if .Success}} +
+ Le compe a été créé ! + Rendez-vous sur la page d'accueil pour vous connecter avec ce nouveau compte. +
+{{else}} +
+
+ + + + Votre identifiant doit être en minuscule. + +
+
+
+ {{if .ErrorInvalidUsername}} +
+ Nom d'utilisateur invalide. Ne peut contenir que les caractères suivants : chiffres, lettres minuscules, point, + tiret bas (_) et tiret du milieu (-). +
+ {{end}} + {{if .ErrorUsernameTaken}} +
+ Ce nom d'utilisateur est déjà pris. +
+ {{end}} +
+ + + + Le courriel de l'utilisateur. + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + + La seule contrainte est que votre mot de passe doit faire au moins 8 caractères. Utilisez chiffres, majuscules, et + caractères spéciaux sans modération ! + +
+ {{if .ErrorPasswordTooShort}} +
+ Le mot de passe choisi est trop court (minimum 8 caractères). +
+ {{end}} +
+ + +
+ {{if .ErrorPasswordMismatch}} +
+ Les deux mots de passe entrés ne correspondent pas. +
+ {{end}} + +
+{{end}} +{{end}} \ No newline at end of file diff --git a/utils.go b/utils.go index 11d3864..08c8699 100644 --- a/utils.go +++ b/utils.go @@ -30,6 +30,7 @@ type NewUser struct { SN string UID string Description string + Password string } func openLdap(config ConfigFile) *ldap.Conn { @@ -67,6 +68,10 @@ func addNewUser(newUser NewUser, config *ConfigFile, login *LoginStatus) bool { if newUser.Description != "" { req.Attribute("description", []string{newUser.Description}) } + if newUser.Password != "" { + pw, _ := SSHAEncode(newUser.Password) + req.Attribute("userPassword", []string{pw}) + } err := login.conn.Add(req) log.Printf(fmt.Sprintf("71: %v", err)) log.Printf(fmt.Sprintf("72: %v", req))