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 // Common functions for new account
type NewAccountData struct { type NewAccountData struct {
Username string Username string
DisplayName string DisplayName string
GivenName string GivenName string
Surname string Surname string
Mail string Mail string
SuggestPW string SuggestPW string
OtherMailbox string
ErrorUsernameTaken bool ErrorUsernameTaken bool
ErrorInvalidUsername 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.GivenName = strings.TrimSpace(strings.Join(r.Form["givenname"], ""))
newUser.SN = strings.TrimSpace(strings.Join(r.Form["surname"], "")) newUser.SN = strings.TrimSpace(strings.Join(r.Form["surname"], ""))
newUser.UID = strings.TrimSpace(strings.Join(r.Form["username"], "")) 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.Mail = strings.TrimSpace(strings.Join(r.Form["mail"], ""))
newUser.DN = "cn=" + newUser.CN + "," + config.InvitationBaseDN newUser.DN = "cn=" + newUser.CN + "," + config.InvitationBaseDN

View file

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

View file

@ -22,17 +22,6 @@
</div> </div>
{{else}} {{else}}
<form method="POST" class="mt-4"> <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"> <div class="form-group">
<label for="givenname">Prénom :</label> <label for="givenname">Prénom :</label>
<input type="text" id="givenname" name="givenname" class="form-control" value="{{ .GivenName }}" /> <input type="text" id="givenname" name="givenname" class="form-control" value="{{ .GivenName }}" />
@ -41,6 +30,17 @@
<label for="surname">Nom de famille :</label> <label for="surname">Nom de famille :</label>
<input type="text" id="surname" name="surname" class="form-control" value="{{ .Surname }}" /> <input type="text" id="surname" name="surname" class="form-control" value="{{ .Surname }}" />
</div> </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"> <div class="form-group">
<label for="username">Identifiant souhaité :</label> <label for="username">Identifiant souhaité :</label>
<input type="text" id="username" name="username" class="form-control" value="{{ .Username }}" /> <input type="text" id="username" name="username" class="form-control" value="{{ .Username }}" />
@ -61,6 +61,13 @@
Ce nom d'utilisateur est déjà pris. Ce nom d'utilisateur est déjà pris.
</div> </div>
{{end}} {{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> <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"> <div class="form-group">
<label for="password">Mot de passe :</label> <label for="password">Mot de passe :</label>
@ -88,27 +95,27 @@
</form> </form>
<script lang="javascript"> <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 () { function changeDisplayname () {
displayname = document.getElementById("displayname"); givenname = document.getElementById("givenname");
username = document.getElementById("username"); surname = document.getElementById("surname");
givenname = document.getElementById("givenname"); displayname = document.getElementById("displayname");
surname = document.getElementById("surname"); username = document.getElementById("username");
if (!username.value) { username.value = displayname.value.toLowerCase().replace(/[^A-z0-9.-]/g, '').replace(/^[.-]/, '').replace(/[.-]$/, '');
username.value = displayname.value;
changeUsername();
}
if (!givenname.value) {
givenname.value = displayname.value.split(" ")[0];
}
if (!surname.value) {
surname.value = displayname.value.split(" ")[1];
}
} }
function changeUsername () { function changeUsername () {
username = document.getElementById("username"); givenname = document.getElementById("givenname");
calcCn = document.getElementById("calc-cn"); surname = document.getElementById("surname");
username.value = username.value.toLowerCase().replace(/[^A-z0-9.-]/g, ''); displayname = document.getElementById("displayname");
calcCn.innerText = "Login Name et Courriel seront : " + username.value.split("@")[0] + "@lesgv.com"; username = document.getElementById("username");
mail = document.getElementById("mail");
mail.value = username.value + "@lesgv.com";
} }
document.getElementById("username").addEventListener("change",changeUsername); document.getElementById("username").addEventListener("change",changeUsername);
document.getElementById("displayname").addEventListener("change",changeDisplayname); document.getElementById("displayname").addEventListener("change",changeDisplayname);