Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
render_template,
g,
)

from werkzeug.middleware.proxy_fix import ProxyFix
from sqlalchemy.exc import ProgrammingError
from flask_migrate import Migrate
Expand Down Expand Up @@ -88,6 +89,24 @@ def constants_js():
"""Route des constantes javascript"""
return render_template("constants.js")


@app.after_request
def after_login_method(response):
"""
Fonction s'exécutant après chaque requete
permet de gérer l'authentification
"""
if not request.cookies.get("token"):
session["current_user"] = None

if (
request.endpoint == "auth.login" and response.status_code == 200
): # noqa
current_user = json.loads(response.get_data().decode("utf-8"))
session["current_user"] = current_user["user"]
return response


@app.context_processor
def inject_user():
return dict(user=getattr(g, "user", None))
Expand Down Expand Up @@ -139,6 +158,8 @@ def inject_user():
route_register.route, url_prefix="/api_register"
) # noqa


app.login_manager.unauthorized_handler(handle_unauthenticated_request)


return app
26 changes: 26 additions & 0 deletions app/t_roles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Définition du formulaire : création/modification d'un role
"""

import ast
import json

from flask_wtf import FlaskForm
from wtforms import (
StringField,
Expand All @@ -19,6 +22,28 @@
from wtforms.validators import DataRequired, Email


class JSONField(StringField):
def _value(self):
return self.data if self.data else {}

def process_formdata(self, valuelist):
if valuelist:
try:
self.data = ast.literal_eval(valuelist[0])
except SyntaxError:
raise ValueError("This field contains invalid JSON")
else:
self.data = None

def pre_validate(self, form):
super().pre_validate(form)
if self.data:
try:
json.dumps(self.data)
except TypeError:
raise ValueError("This field contains invalid JSON")


class MultiCheckboxField(SelectMultipleField):
widget = widgets.ListWidget(prefix_label=False)
option_widget = widgets.CheckboxInput()
Expand All @@ -44,6 +69,7 @@ class Utilisateur(FlaskForm):
groupe = HiddenField("groupe", default=None)
remarques = TextAreaField("Commentaire")
id_role = HiddenField("id")
champs_addi = JSONField("Autres")
submit = SubmitField("Enregistrer")


Expand Down
20 changes: 17 additions & 3 deletions app/t_roles/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from app.env import db



URL_REDIRECT = current_app.config["URL_REDIRECT"]
URL_APPLICATION = current_app.config["URL_APPLICATION"]

route = Blueprint("user", __name__)
Expand Down Expand Up @@ -55,6 +57,7 @@ def users():
"Actif",
"pass_plus",
"pass_md5",
"Autres",
] # noqa
columns = [
"id_role",
Expand All @@ -67,6 +70,7 @@ def users():
"active",
"pass_plus",
"pass_md5",
"champs_addi",
] # noqa
filters = [{"col": "groupe", "filter": "False"}]
contents = TRoles.get_all(columns, filters, order_by="identifiant", order="asc")
Expand Down Expand Up @@ -211,6 +215,7 @@ def updatepass(id_role=None):
role_fullname = buildUserFullName(myuser)
title = f"Changer le mot de passe de l'utilisateur '{role_fullname}'"


if request.method == "POST":
if form.validate_on_submit() and form.validate():
form_user = pops(form.data, False)
Expand All @@ -232,7 +237,11 @@ def updatepass(id_role=None):
return render_template(
"user_pass.html",
form=form,
title=title,
title="Changer le mot de passe de l'utilisateur '"
+ myuser["nom_role"]
+ " "
+ myuser["prenom_role"]
+ "'",
id_role=id_role,
)
form_user["id_role"] = id_role
Expand All @@ -244,7 +253,11 @@ def updatepass(id_role=None):
return render_template(
"user_pass.html",
form=form,
title=title,
title="Changer le mot de passe de l'utilisateur '"
+ myuser["nom_role"]
+ " "
+ myuser["prenom_role"]
+ "'",
id_role=id_role,
)

Expand Down Expand Up @@ -283,6 +296,7 @@ def info(id_role):
)



def buildUserFullName(user):
fullname = []
if user["nom_role"]:
Expand All @@ -291,7 +305,6 @@ def buildUserFullName(user):
fullname.append(user["prenom_role"].title())
return " ".join(fullname)


def pops(form, with_group=True):
"""
Methode qui supprime les éléments indésirables du formulaires
Expand All @@ -318,6 +331,7 @@ def process(form, user, groups):
form.email.process_data(user["email"])
form.remarques.process_data(user["remarques"])
form.identifiant.process_data(user["identifiant"])
form.champs_addi.process_data(user["champs_addi"])
form.a_groupe.process_data(groups)
return form

Expand Down
1 change: 1 addition & 0 deletions app/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h3 class="margin-neg">{{ title }}</h3>
{{ wtf.form_field(form.a_groupe) }}
{{ wtf.form_field(form.email) }}
{{ wtf.form_field(form.remarques) }}
{{ wtf.form_field(form.champs_addi) }}
{{ wtf.form_field(form.submit, class="btn btn-success") }}
</div>
</form>
Expand Down