Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion submit-api/src/submit_api/services/user_service.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions submit-web/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading