Skip to content

Commit 7541f30

Browse files
committed
Fix user retrieve error when organizations are not enabled on keycloak
1 parent ed4ced7 commit 7541f30

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

  • libraries/cloudharness-common/cloudharness/auth

libraries/cloudharness-common/cloudharness/auth/keycloak.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,16 @@ def _add_related_to_user(self, user: User, with_details: bool, admin_client):
496496
user.user_groups = [UserGroup.from_dict(group) for group in admin_client.get_user_groups(user_id=user['id'], brief_representation=not with_details)]
497497
user.realm_roles = admin_client.get_realm_roles_of_user(user['id'])
498498
if hasattr(admin_client, 'get_user_organizations'):
499-
user.organizations = [Organization.from_dict(org) for org in admin_client.get_user_organizations(user['id'])]
499+
try:
500+
user.organizations = [Organization.from_dict(org) for org in admin_client.get_user_organizations(user['id'])]
501+
except KeycloakGetError as e:
502+
# Newer Keycloak/admin-client combinations may expose the organizations
503+
# endpoint even when organizations are disabled for the realm.
504+
# In that case, keep backwards-compatible behavior and return no orgs.
505+
if e.response_code == 404:
506+
user.organizations = []
507+
else:
508+
raise
500509
else:
501510
user.organizations = []
502511
return user

0 commit comments

Comments
 (0)