Factoring the model user and correcting password

This commit is contained in:
Chris Mann 2023-07-22 11:33:47 +02:00
parent 5b9168fd1a
commit b8f125cb0f
3 changed files with 58 additions and 48 deletions

View file

@ -123,12 +123,13 @@ func handleInvitationCode(w http.ResponseWriter, r *http.Request) {
// Common functions for new account
type NewAccountData struct {
Username string
DisplayName string
GivenName string
Surname string
Mail string
SuggestPW string
Username string
DisplayName string
GivenName string
Surname string
Mail string
SuggestPW string
OtherMailbox string
ErrorUsernameTaken bool
ErrorInvalidUsername bool
@ -155,6 +156,7 @@ func handleNewAccount(w http.ResponseWriter, r *http.Request, l *ldap.Conn, invi
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.OtherMailbox = strings.TrimSpace(strings.Join(r.Form["otherMailbox"], ""))
newUser.Mail = strings.TrimSpace(strings.Join(r.Form["mail"], ""))
newUser.DN = "cn=" + newUser.CN + "," + config.InvitationBaseDN

View file

@ -15,18 +15,19 @@ import (
Represents a user
*/
type User struct {
DN string
CN string
GivenName string
DisplayName string
Mail string
SN string
UID string
Description string
Password string
CanAdmin bool
CanInvite bool
UserEntry *ldap.Entry
DN string
CN string
GivenName string
DisplayName string
Mail string
SN string
UID string
Description string
Password string
OtherMailbox string
CanAdmin bool
CanInvite bool
UserEntry *ldap.Entry
}
func get(user User, config *ConfigFile, ldapConn *ldap.Conn) (*User, error) {
@ -85,7 +86,7 @@ func add(user User, config *ConfigFile, ldapConn *ldap.Conn) error {
dn := user.DN
req := ldap.NewAddRequest(dn, nil)
req.Attribute("objectClass", []string{"top", "inetOrgPerson"})
req.Attribute("objectClass", []string{"top", "inetOrgPerson", "pilotPerson"})
if user.DisplayName != "" {
req.Attribute("displayName", []string{user.DisplayName})
}

View file

@ -22,17 +22,6 @@
</div>
{{else}}
<form method="POST" class="mt-4">
<div class="form-group">
<label for="displayname">Nom affiché :</label>
<input type="text" id="displayname" name="displayname" class="form-control" value="{{ .DisplayName }}" />
</div>
<div class="form-group">
<label for="mail">Email :</label>
<input type="text" id="mail" name="mail" class="form-control" value="{{ .Mail }}" />
<small class="form-text text-muted">
Le courriel de l'utilisateur.
</small>
</div>
<div class="form-group">
<label for="givenname">Prénom :</label>
<input type="text" id="givenname" name="givenname" class="form-control" value="{{ .GivenName }}" />
@ -41,6 +30,17 @@
<label for="surname">Nom de famille :</label>
<input type="text" id="surname" name="surname" class="form-control" value="{{ .Surname }}" />
</div>
<div class="form-group">
<label for="mail">Email de secours:</label>
<input type="text" id="othermailbox" name="othermailbox" class="form-control" value="{{ .OtherMailbox }}" />
<small class="form-text text-muted">
Le courriel de l'utilisateur.
</small>
</div>
<div class="form-group">
<label for="displayname">Nom affiché :</label>
<input type="text" id="displayname" name="displayname" class="form-control" value="{{ .DisplayName }}" />
</div>
<div class="form-group">
<label for="username">Identifiant souhaité :</label>
<input type="text" id="username" name="username" class="form-control" value="{{ .Username }}" />
@ -61,6 +61,13 @@
Ce nom d'utilisateur est déjà pris.
</div>
{{end}}
<div class="form-group">
<label for="mail">Email des GV:</label>
<input type="text" id="mail" name="mail" class="form-control" value="{{ .Mail }}" />
<small class="form-text text-muted">
Le courriel et login interne.
</small>
</div>
<p><a href="#" onClick="javascript:var suggestPW = Math.random().toString(36).slice(-10); document.getElementById('password').value='{{ .SuggestPW }}';document.getElementById('password2').value='{{ .SuggestPW }}';">Utiliser ce mot de passe :</a> {{ .SuggestPW }}</p>
<div class="form-group">
<label for="password">Mot de passe :</label>
@ -88,27 +95,27 @@
</form>
<script lang="javascript">
function changeGivenNameOrSurname () {
givenname = document.getElementById("givenname");
surname = document.getElementById("surname");
displayname = document.getElementById("displayname");
username = document.getElementById("username");
displayname.value = givenname.value + " " + surname.value
}
function changeDisplayname () {
displayname = document.getElementById("displayname");
username = document.getElementById("username");
givenname = document.getElementById("givenname");
surname = document.getElementById("surname");
if (!username.value) {
username.value = displayname.value;
changeUsername();
}
if (!givenname.value) {
givenname.value = displayname.value.split(" ")[0];
}
if (!surname.value) {
surname.value = displayname.value.split(" ")[1];
}
givenname = document.getElementById("givenname");
surname = document.getElementById("surname");
displayname = document.getElementById("displayname");
username = document.getElementById("username");
username.value = displayname.value.toLowerCase().replace(/[^A-z0-9.-]/g, '').replace(/^[.-]/, '').replace(/[.-]$/, '');
}
function changeUsername () {
username = document.getElementById("username");
calcCn = document.getElementById("calc-cn");
username.value = username.value.toLowerCase().replace(/[^A-z0-9.-]/g, '');
calcCn.innerText = "Login Name et Courriel seront : " + username.value.split("@")[0] + "@lesgv.com";
givenname = document.getElementById("givenname");
surname = document.getElementById("surname");
displayname = document.getElementById("displayname");
username = document.getElementById("username");
mail = document.getElementById("mail");
mail.value = username.value + "@lesgv.com";
}
document.getElementById("username").addEventListener("change",changeUsername);
document.getElementById("displayname").addEventListener("change",changeDisplayname);