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

Reviewed-on: https://scodoc.org/git/viennet/ScoDoc/pulls/177
This commit is contained in:
Emmanuel Viennet 2021-10-30 12:33:22 +02:00
commit e46c6a410f
4 changed files with 41 additions and 16 deletions

View File

@ -1,9 +1,13 @@
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% macro render_field(field) %}
{% macro render_field(field, auth_name=None) %}
<tr style="">
<td class="wtf-field">{{ field.label }}</td>
{% if auth_name %}
<td class="wtf-field"> {{ field.label }}<span style="font-weight:700;"> ({{ auth_name }}):</span></td>
{% else %}
<td class="wtf-field">{{ field.label }}</td>
{% endif %}
<td class="wtf-field">{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
@ -20,15 +24,19 @@
<h1>Modification du compte ScoDoc <tt>{{form.user_name.data}}</tt></h1>
<div class="help">
<p>Identifiez-vous avez votre mot de passe actuel</p>
<p>Vous pouvez changer le mot de passe et/ou l'adresse email.</p>
<p>Les champs vides ne seront pas changés.</p>
</div>
<form method=post>
{{ form.user_name }}
{{ form.csrf_token }}
<table class="tf"><tbody>
{{ render_field(form.old_password, size=14,
{{ render_field(form.old_password, size=14, auth_name=auth_username,
style="padding:1px; margin-left: 1em; margin-top: 4px;") }}
<tr>
<td colspan=""2">
<p>Vous pouvez changer le mot de passe et/ou l'adresse email.</p>
<p>Les champs laissés vides ne seront pas modifiés.</p>
</td>
</tr>
{{ render_field(form.new_password, size=14,
style="padding:1px; margin-left: 1em; margin-top: 12px;") }}
{{ render_field(form.bis_password, size=14,

View File

@ -16,3 +16,5 @@
{% endif %}
<p>A bientôt !</p>
<p>Ce message a été généré automatiquement par le serveur ScoDoc.</p>

View File

@ -8,4 +8,6 @@ Votre identifiant de connexion est: {{ user.user_name }}
{{ url_for('auth.reset_password', token=token, _external=True) }}
{% endif %}
<p>A bientôt !</p>
A bientôt !
Ce message a été généré automatiquement par le serveur ScoDoc.

View File

@ -35,6 +35,7 @@ Emmanuel Viennet, 2021
"""
import datetime
import re
from enum import auto, IntEnum
from xml.etree import ElementTree
import flask
@ -116,6 +117,12 @@ class ChangePasswordForm(FlaskForm):
raise ValidationError("Mot de passe actuel incorrect, ré-essayez")
class Mode(IntEnum):
WELCOME_AND_CHANGE_PASSWORD = auto()
WELCOME_ONLY = auto()
SILENT = auto()
@bp.route("/")
@bp.route("/index_html")
@scodoc
@ -145,6 +152,8 @@ def user_info(user_name, format="json"):
def create_user_form(user_name=None, edit=0, all_roles=1):
"form. création ou edition utilisateur"
auth_dept = current_user.dept
auth_username = current_user.user_name
from_mail = current_user.email
initvalues = {}
edit = int(edit)
all_roles = int(all_roles)
@ -529,19 +538,20 @@ def create_user_form(user_name=None, edit=0, all_roles=1):
)
return "\n".join(H) + msg + "\n" + tf[1] + F
# Traitement initial (mode) : 3 cas
# cf énumération Mode
# A: envoi de welcome + procedure de reset
# B: envoi de welcome seulement (mot de passe saisie dans le formulaire)
# C: Aucun envoi (mot de passe saisi dans le formulaire)
if vals["welcome:list"] == "1":
if vals["reset_password:list"] == "1":
mode = "A"
mode = Mode.WELCOME_AND_CHANGE_PASSWORD
else:
mode = "B"
mode = Mode.WELCOME_ONLY
else:
mode = "C"
mode = Mode.SILENT
# check passwords
if mode == "A":
if mode == Mode.WELCOME_AND_CHANGE_PASSWORD:
vals["password"] = generate_password()
else:
if vals["password"]:
@ -567,14 +577,14 @@ def create_user_form(user_name=None, edit=0, all_roles=1):
db.session.add(u)
db.session.commit()
# envoi éventuel d'un message
if mode == "A" or mode == "B":
if mode == "A":
if mode == Mode.WELCOME_AND_CHANGE_PASSWORD or mode == Mode.WELCOME_ONLY:
if mode == Mode.WELCOME_AND_CHANGE_PASSWORD:
token = u.get_reset_password_token()
else:
token = None
send_email(
"[ScoDoc] Création de votre compte",
sender=current_app.config["ADMINS"][0],
sender=from_mail, # current_app.config["ADMINS"][0],
recipients=[u.email],
text_body=render_template("email/welcome.txt", user=u, token=token),
html_body=render_template(
@ -787,7 +797,10 @@ def form_change_password(user_name=None):
return redirect(destination)
return render_template(
"auth/change_password.html", form=form, title="Modification compte ScoDoc"
"auth/change_password.html",
form=form,
title="Modification compte ScoDoc",
auth_username=current_user.user_name,
)