Merge pull request 'create_user_plus' (#164) from jmplace/ScoDoc-Lille:create_user_plus into master

Reviewed-on: https://scodoc.org/git/viennet/ScoDoc/pulls/164
This commit is contained in:
Emmanuel Viennet 2021-10-15 17:44:33 +02:00
commit c2de33f7f5
4 changed files with 135 additions and 15 deletions

View File

@ -0,0 +1,30 @@
function refresh() {
if ($("input[name='welcome:list']").is(":checked")) {
$("input[name='reset_password:list']").closest("tr").css("display", "table-row")
if ($("input[name='reset_password:list']").is(":checked")) {
$("#tf_password").closest('tr').css("display", "none");
$("#tf_password2").closest('tr').css("display", "none");
} else {
// Le mot de passe doit être saisi
$("#tf_password").closest('tr').css("display", "table-row");
$("#tf_password2").closest('tr').css("display", "table-row");
}
} else {
// Le mot de passe doit être saisi
$("input[name='reset_password:list']").closest("tr").css("display", "none")
$("#tf_password").closest('tr').css("display", "table-row");
$("#tf_password2").closest('tr').css("display", "table-row");
}
}
$(function() {
$("input[name='welcome:list']").click(function() {
refresh();
})
$("input[name='reset_password:list']").click(function() {
refresh();
})
refresh();
})

View File

@ -0,0 +1,16 @@
<p>Bienvenue {{ user.prenom }} {{ user.nom }},</p>
<p>
Votre accès à ScoDoc vient d'être validé.
votre identifiant de connexion est: {{ user.user_name }}
{% if token %}
Pour initialiser votre mot de passe ScoDoc,
<a href="{{ url_for('auth.reset_password', token=token, _external=True) }}">
cliquez sur ce lien
</a>.
</p>
<p>Vous pouvez aussi copier ce lien dans votre navigateur Web:</p>
<p>{{ url_for('auth.reset_password', token=token, _external=True) }}</p>
{% endif %}
<p>A bientôt !</p>

View File

@ -0,0 +1,11 @@
Bienvenue {{ user.prenom }} {{ user.nom }},
Votre accès à ScoDoc vient d'être validé.
Votre identifiant de connexion est: {{ user.user_name }}
{% if token %}
Pour initialiser votre mot de passe ScoDoc, suivre le lien:
{{ url_for('auth.reset_password', token=token, _external=True) }}
{% endif %}
<p>A bientôt !</p>

View File

@ -38,7 +38,7 @@ import re
from xml.etree import ElementTree from xml.etree import ElementTree
import flask import flask
from flask import g, url_for, request from flask import g, url_for, request, current_app
from flask import redirect, render_template from flask import redirect, render_template
from flask_login import current_user from flask_login import current_user
@ -49,6 +49,7 @@ from app.auth.models import Permission
from app.auth.models import User from app.auth.models import User
from app.auth.models import Role from app.auth.models import Role
from app.auth.models import UserRole from app.auth.models import UserRole
from app.email import send_email
from app.models import Departement from app.models import Departement
from app.decorators import ( from app.decorators import (
@ -63,6 +64,7 @@ from app.scodoc import sco_utils as scu
from app.scodoc import sco_xml from app.scodoc import sco_xml
from app import log from app import log
from app.scodoc.sco_exceptions import AccessDenied, ScoValueError from app.scodoc.sco_exceptions import AccessDenied, ScoValueError
from app.scodoc.sco_import_users import generate_password
from app.scodoc.sco_permissions_check import can_handle_passwd from app.scodoc.sco_permissions_check import can_handle_passwd
from app.scodoc.TrivialFormulator import TrivialFormulator, tf_error_message from app.scodoc.TrivialFormulator import TrivialFormulator, tf_error_message
from app.views import users_bp as bp from app.views import users_bp as bp
@ -100,7 +102,12 @@ def create_user_form(user_name=None, edit=0, all_roles=1):
initvalues = {} initvalues = {}
edit = int(edit) edit = int(edit)
all_roles = int(all_roles) all_roles = int(all_roles)
H = [html_sco_header.sco_header(bodyOnLoad="init_tf_form('')")] H = [
html_sco_header.sco_header(
bodyOnLoad="init_tf_form('')",
javascripts=["js/user_form.js"],
)
]
F = html_sco_header.sco_footer() F = html_sco_header.sco_footer()
if edit: if edit:
if not user_name: if not user_name:
@ -214,7 +221,32 @@ def create_user_form(user_name=None, edit=0, all_roles=1):
"title": "Pseudo (login)", "title": "Pseudo (login)",
"size": 20, "size": 20,
"allow_null": False, "allow_null": False,
"explanation": "nom utilisé pour la connexion. Doit être unique parmi tous les utilisateurs.", "explanation": "nom utilisé pour la connexion. Doit être unique parmi tous les utilisateurs. "
"Lettres ou chiffres uniquement.",
},
),
("formsemestre_id", {"input_type": "hidden"}),
(
"welcome",
{
"title": "Message d'accueil",
"input_type": "checkbox",
"explanation": "Envoie un mail d'accueil à l'utilisateur.",
"labels": ("",),
"allowed_values": ("1",),
"default": "1",
},
),
(
"reset_password",
{
"title": "réinit. mot de passe.",
"input_type": "checkbox",
"explanation": "ajoute la procédure de changement de mot de passe au mail d'accueil",
"labels": ("",),
"allowed_values": ("1",),
"default": "1",
"attributes": ["style='margin-left:20pt'"],
}, },
), ),
( (
@ -387,7 +419,7 @@ def create_user_form(user_name=None, edit=0, all_roles=1):
nom=vals["nom"], nom=vals["nom"],
prenom=vals["prenom"], prenom=vals["prenom"],
email=vals["email"], email=vals["email"],
dept=vals["dept"], dept=vals.get("dept", auth_dept),
roles=vals["roles"], roles=vals["roles"],
) )
if not ok: if not ok:
@ -444,7 +476,22 @@ def create_user_form(user_name=None, edit=0, all_roles=1):
"identifiant invalide (pas d'accents ni de caractères spéciaux)" "identifiant invalide (pas d'accents ni de caractères spéciaux)"
) )
return "\n".join(H) + msg + "\n" + tf[1] + F return "\n".join(H) + msg + "\n" + tf[1] + F
# Traitement initial (mode) : 3 cas
# A: envoi de welcome + procedure de reset
# B: envoi de welcome seulement (mot de passe saisie dans le formulaire)
# C: Aucun envoi (mot de pase saisi dans le formulaire)
if vals["welcome:list"] == "1":
if vals["reset_password:list"] == "1":
mode = "A"
else:
mode = "B"
else:
mode = "C"
# check passwords # check passwords
if mode == "A":
vals["password"] = generate_password()
else:
if vals["password"]: if vals["password"]:
if vals["password"] != vals["password2"]: if vals["password"] != vals["password2"]:
msg = tf_error_message( msg = tf_error_message(
@ -467,6 +514,22 @@ def create_user_form(user_name=None, edit=0, all_roles=1):
u.from_dict(vals, new_user=True) u.from_dict(vals, new_user=True)
db.session.add(u) db.session.add(u)
db.session.commit() db.session.commit()
# envoi éventuel d'un message
if mode == "A" or mode == "B":
if mode == "A":
token = u.get_reset_password_token()
else:
token = None
send_email(
"[ScoDoc] Réinitialisation de votre mot de passe",
sender=current_app.config["ADMINS"][0],
recipients=[u.email],
text_body=render_template("email/welcome.txt", user=u, token=token),
html_body=render_template(
"email/welcome.html", user=u, token=token
),
)
return flask.redirect( return flask.redirect(
url_for( url_for(
"users.user_info_page", "users.user_info_page",