From fe8e1160abaec768e280b8df2c1986f35ad02e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Wed, 6 Nov 2024 09:10:43 +0100 Subject: [PATCH] refactor: move session related methods in a dedicated file --- canaille/__init__.py | 2 +- canaille/app/flask.py | 49 +---------------------------- canaille/app/i18n.py | 2 +- canaille/app/session.py | 50 ++++++++++++++++++++++++++++++ canaille/core/endpoints/account.py | 6 ++-- canaille/core/endpoints/auth.py | 6 ++-- canaille/oidc/endpoints/oauth.py | 4 +-- 7 files changed, 61 insertions(+), 58 deletions(-) create mode 100644 canaille/app/session.py diff --git a/canaille/__init__.py b/canaille/__init__.py index 27da9ec0..06bd248f 100644 --- a/canaille/__init__.py +++ b/canaille/__init__.py @@ -57,7 +57,7 @@ def make_session_permanent(): @app.context_processor def global_processor(): - from canaille.app.flask import current_user + from canaille.app.session import current_user return { "debug": app.debug or app.config.get("TESTING", False), diff --git a/canaille/app/flask.py b/canaille/app/flask.py index e1ed09fb..808db140 100644 --- a/canaille/app/flask.py +++ b/canaille/app/flask.py @@ -5,61 +5,14 @@ from flask import abort from flask import current_app -from flask import g from flask import request -from flask import session from werkzeug.routing import BaseConverter -from canaille.app import models from canaille.app.i18n import gettext as _ +from canaille.app.session import current_user from canaille.app.themes import render_template -def current_user(): - if "user" in g: - return g.user - - for user_id in session.get("user_id", [])[::-1]: - user = current_app.backend.instance.get(models.User, user_id) - if user and ( - not current_app.backend.has_account_lockability() or not user.locked - ): - g.user = user - return g.user - - session["user_id"].remove(user_id) - - if "user_id" in session and not session["user_id"]: - del session["user_id"] - - return None - - -def login_user(user): - """Open a session for the user.""" - g.user = user - try: - previous = ( - session["user_id"] - if isinstance(session["user_id"], list) - else [session["user_id"]] - ) - session["user_id"] = previous + [user.id] - except KeyError: - session["user_id"] = [user.id] - - -def logout_user(): - """Close the user session.""" - try: - session["user_id"].pop() - del g.user - if not session["user_id"]: - del session["user_id"] - except (IndexError, KeyError): - pass - - def user_needed(): def wrapper(view_function): @wraps(view_function) diff --git a/canaille/app/i18n.py b/canaille/app/i18n.py index 9905e31b..cbbd15c6 100644 --- a/canaille/app/i18n.py +++ b/canaille/app/i18n.py @@ -46,7 +46,7 @@ def global_processor(): def locale_selector(): - from .flask import current_user + from .session import current_user user = current_user() available_language_codes = getattr(g, "available_language_codes", []) diff --git a/canaille/app/session.py b/canaille/app/session.py new file mode 100644 index 00000000..377015bf --- /dev/null +++ b/canaille/app/session.py @@ -0,0 +1,50 @@ +from flask import current_app +from flask import g +from flask import session + +from canaille.app import models + + +def current_user(): + if "user" in g: + return g.user + + for user_id in session.get("user_id", [])[::-1]: + user = current_app.backend.instance.get(models.User, user_id) + if user and ( + not current_app.backend.has_account_lockability() or not user.locked + ): + g.user = user + return g.user + + session["user_id"].remove(user_id) + + if "user_id" in session and not session["user_id"]: + del session["user_id"] + + return None + + +def login_user(user): + """Open a session for the user.""" + g.user = user + try: + previous = ( + session["user_id"] + if isinstance(session["user_id"], list) + else [session["user_id"]] + ) + session["user_id"] = previous + [user.id] + except KeyError: + session["user_id"] = [user.id] + + +def logout_user(): + """Close the user session.""" + try: + session["user_id"].pop() + del g.user + if not session["user_id"]: + del session["user_id"] + except (IndexError, KeyError): + pass diff --git a/canaille/core/endpoints/account.py b/canaille/core/endpoints/account.py index 1c8ee5ec..2667ab22 100644 --- a/canaille/core/endpoints/account.py +++ b/canaille/core/endpoints/account.py @@ -24,9 +24,6 @@ from canaille.app import default_fields from canaille.app import models from canaille.app import obj_to_b64 -from canaille.app.flask import current_user -from canaille.app.flask import login_user -from canaille.app.flask import logout_user from canaille.app.flask import permissions_needed from canaille.app.flask import render_htmx_template from canaille.app.flask import request_is_htmx @@ -41,6 +38,9 @@ from canaille.app.forms import set_writable from canaille.app.i18n import gettext as _ from canaille.app.i18n import reload_translations +from canaille.app.session import current_user +from canaille.app.session import login_user +from canaille.app.session import logout_user from canaille.app.themes import render_template from canaille.backends import Backend diff --git a/canaille/core/endpoints/auth.py b/canaille/core/endpoints/auth.py index 9891755a..d649605a 100644 --- a/canaille/core/endpoints/auth.py +++ b/canaille/core/endpoints/auth.py @@ -8,11 +8,11 @@ from flask import url_for from canaille.app import build_hash -from canaille.app.flask import current_user -from canaille.app.flask import login_user -from canaille.app.flask import logout_user from canaille.app.flask import smtp_needed from canaille.app.i18n import gettext as _ +from canaille.app.session import current_user +from canaille.app.session import login_user +from canaille.app.session import logout_user from canaille.app.themes import render_template from canaille.backends import Backend diff --git a/canaille/oidc/endpoints/oauth.py b/canaille/oidc/endpoints/oauth.py index a4d63f5e..2c0fb2a4 100644 --- a/canaille/oidc/endpoints/oauth.py +++ b/canaille/oidc/endpoints/oauth.py @@ -18,10 +18,10 @@ from canaille import csrf from canaille.app import models -from canaille.app.flask import current_user -from canaille.app.flask import logout_user from canaille.app.flask import set_parameter_in_url_query from canaille.app.i18n import gettext as _ +from canaille.app.session import current_user +from canaille.app.session import logout_user from canaille.app.themes import render_template from canaille.backends import Backend