Skip to content

Commit

Permalink
fix: keep user logged in on error pages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
azmeuk committed Dec 13, 2023
1 parent 6342bfb commit c214d25
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
19 changes: 18 additions & 1 deletion web/b3desk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"],
Expand All @@ -94,6 +110,7 @@ def global_processor():
"version": "1.1.2",
"LANGUAGES": LANGUAGES,
**app.config["WORDINGS"],
**session_dict,
}


Expand Down
41 changes: 4 additions & 37 deletions web/b3desk/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand Down Expand Up @@ -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(
Expand Down
23 changes: 23 additions & 0 deletions web/b3desk/session.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c214d25

Please sign in to comment.