CH-221 fix: Use kc_id instead of inconsistent kc_username or kc_email for user sync#820
Closed
afonsobspinto wants to merge 3 commits intodevelopfrom
Closed
CH-221 fix: Use kc_id instead of inconsistent kc_username or kc_email for user sync#820afonsobspinto wants to merge 3 commits intodevelopfrom
afonsobspinto wants to merge 3 commits intodevelopfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a user synchronization bug where the code was incorrectly searching for users by email in the username field, causing User.DoesNotExist errors when Keycloak username differs from email. The fix replaces the inconsistent username/email lookup with reliable Keycloak ID-based user identification.
- Replace
User.objects.get(username=kc_user["email"])withget_user_by_kc_id(kc_user["id"]) - Add proper handling for new user creation with fallback username logic
- Update admin user comparison to use Keycloak ID instead of email
- Add missing
user.member.save()call for consistency
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
...mages/cloudharness-django/libraries/cloudharness-django/cloudharness_django/services/user.py
Show resolved
Hide resolved
Member
Author
|
Might have been superseded by #819 |
filippomc
reviewed
Sep 22, 2025
Collaborator
filippomc
left a comment
There was a problem hiding this comment.
Merging this issue fix to the CH-220 issue PR.
filippomc
reviewed
Sep 22, 2025
...mages/cloudharness-django/libraries/cloudharness-django/cloudharness_django/services/user.py
Outdated
Show resolved
Hide resolved
filippomc
reviewed
Sep 22, 2025
| for kc_user in self.auth_client.get_users(): | ||
| # check if user in all_admin_users | ||
| is_superuser = any([admin_user for admin_user in all_admin_users if admin_user["email"] == kc_user["email"]]) | ||
| is_superuser = any([admin_user for admin_user in all_admin_users if admin_user["id"] == kc_user["id"]]) |
Collaborator
There was a problem hiding this comment.
This is unnecessary, emails are unique
Member
Author
Collaborator
|
Closing this as we are addressing this issue within #819 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Closes https://metacell.atlassian.net/browse/CH-221
Implemented solution
User.objects.get(username=kc_user["email"]) was trying to find users by email but searching the username field, causing User.DoesNotExist errors when Keycloak username ≠ email.
This PR replaces it with get_user_by_kc_id(kc_user["id"]) which uses the reliable Keycloak ID for lookup.
How to test this PR
Sync keycloak users with Django users with at least 1 user where kc username differs from kc email.
Sanity checks:
Breaking changes (select one):
breaking-changeand the migration procedure is well described abovePossible deployment updates issues (select one):
alert:deploymentTest coverage (select one):
Documentation (select one):