From 69c88c1a558af911ce4b747b75bd106235b82ec3 Mon Sep 17 00:00:00 2001 From: Bjarke Madsen <bjarke@nordu.net> Date: Thu, 24 Aug 2023 18:03:15 +0200 Subject: [PATCH] use given_name from the IDP --- compendium_v2/auth/session_management.py | 5 +++-- compendium_v2/email/__init__.py | 7 +++---- compendium_v2/routes/authentication.py | 2 +- test/test_send_mail.py | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/compendium_v2/auth/session_management.py b/compendium_v2/auth/session_management.py index 5c6b016b..a244f0a1 100644 --- a/compendium_v2/auth/session_management.py +++ b/compendium_v2/auth/session_management.py @@ -33,13 +33,14 @@ def admin_required(func): return wraps(func)(wrapper) -def create_user(email: str, fullname: str, oidc_sub: str): +def create_user(email: str, fullname: str, oidc_sub: str, given_name: str): """ Function used to create a new user in the database. :param email: The email of the user :param fullname: The full name of the user :param oidc_sub: The OIDC subject identifier (ID) of the user + :param given_name: The given name of the user :return: The user object """ @@ -47,7 +48,7 @@ def create_user(email: str, fullname: str, oidc_sub: str): user = User(email=email, fullname=fullname, oidc_sub=oidc_sub) session.add(user) send_admin_signup_notification(user) - send_user_signup_notification(user) + send_user_signup_notification(user, given_name) return user diff --git a/compendium_v2/email/__init__.py b/compendium_v2/email/__init__.py index 7edf05ca..be3349ef 100644 --- a/compendium_v2/email/__init__.py +++ b/compendium_v2/email/__init__.py @@ -75,10 +75,9 @@ def send_admin_signup_notification(user: User): send_mail(contents=contents, subject='New user signed up for Compendium') -def send_user_signup_notification(user: User): - fullname = user.fullname +def send_user_signup_notification(user: User, given_name: str): email = user.email + name = f' {given_name}' if given_name else '' - first_name = ' ' + fullname.split()[0] if fullname else '' - contents = USER_NOTIFICATION_TEMPLATE.format(name=first_name) + contents = USER_NOTIFICATION_TEMPLATE.format(name=name) send_mail(contents=contents, subject='You have signed up for the Compendium Survey', recipients=email) diff --git a/compendium_v2/routes/authentication.py b/compendium_v2/routes/authentication.py index aec7d330..f1636065 100644 --- a/compendium_v2/routes/authentication.py +++ b/compendium_v2/routes/authentication.py @@ -36,7 +36,7 @@ def authorize(): user = fetch_user(profile) if user is None: # create a new user - user = create_user(profile['email'], profile['name'], profile['sub']) + user = create_user(profile['email'], profile['name'], profile['sub'], profile['given_name']) login_user(user) # redirect to / diff --git a/test/test_send_mail.py b/test/test_send_mail.py index d68b067d..e3ae5a9e 100644 --- a/test/test_send_mail.py +++ b/test/test_send_mail.py @@ -33,4 +33,4 @@ def test_signup_email_user(app, mocker): mocker.patch('compendium_v2.email._send_mail', _send_mail) with test_user(app) as user: - send_user_signup_notification(user) + send_user_signup_notification(user, 'testname') -- GitLab