Skip to content

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
feature/CH-221
Closed

CH-221 fix: Use kc_id instead of inconsistent kc_username or kc_email for user sync#820
afonsobspinto wants to merge 3 commits intodevelopfrom
feature/CH-221

Conversation

@afonsobspinto
Copy link
Copy Markdown
Member

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:

  • The pull request is explicitly linked to the relevant issue(s)
  • The issue is well described: clearly states the problem and the general proposed solution(s)
  • In this PR it is explicitly stated how to test the current change
  • The labels in the issue set the scope and the type of issue (bug, feature, etc.)
  • The relevant components are indicated in the issue (if any)
  • All the automated test checks are passing
  • All the linked issues are included in one Sprint
  • All the linked issues are in the Review state
  • All the linked issues are assigned

Breaking changes (select one):

  • The present changes do not change the preexisting api in any way
  • This PR and the issue are tagged as a breaking-change and the migration procedure is well described above

Possible deployment updates issues (select one):

  • There is no reason why deployments based on CloudHarness may break after the current update
  • This PR and the issue are tagged as alert:deployment

Test coverage (select one):

  • Tests for the relevant cases are included in this pr
  • The changes included in this pr are out of the current test coverage scope

Documentation (select one):

  • The documentation has been updated to match the current changes
  • The changes included in this PR are out of the current documentation scope

@afonsobspinto afonsobspinto added the bug Something isn't working label Sep 21, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"]) with get_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.

@afonsobspinto
Copy link
Copy Markdown
Member Author

Might have been superseded by #819

Copy link
Copy Markdown
Collaborator

@filippomc filippomc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging this issue fix to the CH-220 issue PR.

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"]])
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary, emails are unique

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From accounts settings I see we can activate duplicate emails:

image

Admittedly I never had the need to activate it but ideally I would prefer cloudharness_django to allow it if we eventually need

@filippomc
Copy link
Copy Markdown
Collaborator

Closing this as we are addressing this issue within #819

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants