From c214d254d5b2c8a53114780349697278afe626bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Wed, 13 Dec 2023 13:43:26 +0100 Subject: [PATCH] fix: keep user logged in on error pages The `user` var was not passed to the error pages templates, so user would appear logged out on 404 or 500 error pages for instance. --- web/b3desk/__init__.py | 19 ++++++++++++++++++- web/b3desk/routes.py | 41 ++++------------------------------------- web/b3desk/session.py | 23 +++++++++++++++++++++++ 3 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 web/b3desk/session.py diff --git a/web/b3desk/__init__.py b/web/b3desk/__init__.py index 0d9ff363..69a5664f 100755 --- a/web/b3desk/__init__.py +++ b/web/b3desk/__init__.py @@ -16,7 +16,6 @@ from flask import Flask from flask import render_template from flask import request -from flask import session from flask_babel import Babel from flask_caching import Cache from flask_migrate import Migrate @@ -58,6 +57,8 @@ def setup_logging(app, gunicorn_logging=False): def setup_i18n(app): + from flask import session + babel.init_app(app) @babel.localeselector @@ -84,8 +85,23 @@ def setup_database(app): def setup_jinja(app): + from b3desk.session import has_user_session + from b3desk.session import get_current_user + @app.context_processor def global_processor(): + if has_user_session(): + user = get_current_user() + session_dict = { + "user": user, + "fullname": user.fullname, + } + else: + session_dict = { + "user": None, + "fullname": "", + } + return { "config": app.config, "beta": app.config["BETA"], @@ -94,6 +110,7 @@ def global_processor(): "version": "1.1.2", "LANGUAGES": LANGUAGES, **app.config["WORDINGS"], + **session_dict, } diff --git a/web/b3desk/routes.py b/web/b3desk/routes.py index d67f370e..9a8bb49c 100755 --- a/web/b3desk/routes.py +++ b/web/b3desk/routes.py @@ -47,24 +47,26 @@ from flask import request from flask import send_file from flask import send_from_directory -from flask import session from flask import url_for from flask_babel import lazy_gettext as _ from flask_pyoidc import OIDCAuthentication from flask_pyoidc.provider_configuration import ClientMetadata from flask_pyoidc.provider_configuration import ProviderConfiguration -from flask_pyoidc.user_session import UserSession from sqlalchemy import exc from webdav3.client import Client as webdavClient from webdav3.exceptions import WebDavException from werkzeug.utils import secure_filename from . import cache +from .session import get_authenticated_attendee_fullname +from .session import get_current_user +from .session import has_user_session from .templates.content import FAQ_CONTENT from .utils import is_accepted_email from .utils import is_valid_email from .utils import send_mail + bp = Blueprint("routes", __name__) @@ -102,41 +104,6 @@ ) -def get_current_user(): - user_session = UserSession(session) - info = user_session.userinfo - return get_or_create_user(info) - - -def has_user_session(): - user_session = UserSession(dict(session), "default") - return user_session.is_authenticated() - - -def get_authenticated_attendee_fullname(): - attendee_session = UserSession(session) - attendee_info = attendee_session.userinfo - given_name = attendee_info.get("given_name", "") - family_name = attendee_info.get("family_name", "") - fullname = f"{given_name} {family_name}".strip() - return fullname - - -@bp.context_processor -def global_processor(): - if has_user_session(): - user = get_current_user() - return { - "user": user, - "fullname": user.fullname, - } - else: - return { - "user": None, - "fullname": "", - } - - def add_mailto_links(meeting_data): d = meeting_data d["moderator_mailto_href"] = render_template( diff --git a/web/b3desk/session.py b/web/b3desk/session.py new file mode 100644 index 00000000..5bed444d --- /dev/null +++ b/web/b3desk/session.py @@ -0,0 +1,23 @@ +from b3desk.models.users import get_or_create_user +from flask import session +from flask_pyoidc.user_session import UserSession + + +def get_current_user(): + user_session = UserSession(session) + info = user_session.userinfo + return get_or_create_user(info) + + +def has_user_session(): + user_session = UserSession(dict(session), "default") + return user_session.is_authenticated() + + +def get_authenticated_attendee_fullname(): + attendee_session = UserSession(session) + attendee_info = attendee_session.userinfo + given_name = attendee_info.get("given_name", "") + family_name = attendee_info.get("family_name", "") + fullname = f"{given_name} {family_name}".strip() + return fullname