From f42ada29134fc8e73d80a963e51f8339d3d06788 Mon Sep 17 00:00:00 2001 From: dinesh Date: Mon, 6 Jul 2026 14:51:03 -0700 Subject: [PATCH] crate staff user if the user already exists --- .../src/submit_api/services/user_service.py | 24 ++++++++++++++++++- submit-web/src/router.tsx | 6 +++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/submit-api/src/submit_api/services/user_service.py b/submit-api/src/submit_api/services/user_service.py index d74724e62..b607101a9 100644 --- a/submit-api/src/submit_api/services/user_service.py +++ b/submit-api/src/submit_api/services/user_service.py @@ -1,7 +1,7 @@ """Service for account management.""" from flask import current_app from submit_api.exceptions import ResourceNotFoundError -from submit_api.models import User +from submit_api.models import User, db from submit_api.models.user import UserType from submit_api.models.staff_user import StaffUser from submit_api.utils.roles import EpicSubmitRole @@ -27,6 +27,25 @@ def get_or_provision_by_auth_guid(cls, _guid, token_info=None): # Check if user has staff roles in their token if cls._has_staff_roles(token_info): user = cls._auto_provision_staff_user(_guid, token_info) + elif user and user.type == UserType.STAFF and not user.staff_user and token_info: + # User exists as STAFF but staff_user record is missing - create it + if cls._has_staff_roles(token_info): + email = token_info.get('email') + given_name = token_info.get('given_name') + family_name = token_info.get('family_name') + + staff_user_data = { + 'first_name': given_name, + 'last_name': family_name, + 'work_email_address': email, + 'user_id': user.id + } + StaffUser.create_staff_user(staff_user_data) + + # Refresh the user object to load the staff_user relationship + db.session.refresh(user) + + current_app.logger.info(f"Created missing staff_user record for: {email} (guid: {_guid})") if not user: raise ResourceNotFoundError(f"User with auth guid {_guid} not found") @@ -67,6 +86,9 @@ def _auto_provision_staff_user(cls, _guid, token_info): } StaffUser.create_staff_user(staff_user_data) + # Refresh the user object to load the staff_user relationship + db.session.refresh(user) + current_app.logger.info(f"Auto-provisioned staff user: {email} (guid: {_guid})") return user diff --git a/submit-web/src/router.tsx b/submit-web/src/router.tsx index 93fa2c840..d9aa995ed 100644 --- a/submit-web/src/router.tsx +++ b/submit-web/src/router.tsx @@ -49,8 +49,10 @@ export default function RouterProviderWithAuthContext({ }, [authentication, router, setAccount]); useEffect(() => { - getAccountData(); - }, [authentication, getAccountData]); + if (authentication.isAuthenticated && authentication.user?.profile.preferred_username) { + getAccountData(); + } + }, [authentication.isAuthenticated, authentication.user?.profile.preferred_username, getAccountData]); useEffect(() => { // the `return` is important - addAccessTokenExpiring() returns a cleanup function