mirror of
https://github.com/ansible-middleware/keycloak.git
synced 2026-11-22 15:10:38 +00:00
keycloak_user: fix check mode reporting changed=True for every existing user
In check mode, the desired group memberships were built as a set and compared with the stored memberships, a list, through is_struct_included(). It has no branch for sets and falls back to comparing their string forms, which never match, so check mode reported a change for every existing user, with or without groups. Check mode now decides like normal mode: a group given by name or by path counts as joined when the user is already a member. That rule moves from update_user_groups_membership() into filter_groups_to_add_to_and_remove_from_user(), which both modes use, and the groups reported in check mode no longer list a group given by path twice. Add check mode tests, including groups given by path. Fixes #442 Signed-off-by: Filipe Rosa Capela <Filipe.Capela-CIC.Netherlands1@ibm.com>
This commit is contained in:
@@ -3153,19 +3153,9 @@ class KeycloakAPI:
|
||||
return False
|
||||
|
||||
user_groups = self.get_user_group_details(user_id=userrep["id"], realm=realm)
|
||||
user_group_names = [user_group["name"] for user_group in user_groups if "name" in user_group]
|
||||
user_group_paths = [user_group["path"] for user_group in user_groups if "path" in user_group]
|
||||
|
||||
groups_to_add = [
|
||||
group_to_add
|
||||
for group_to_add in groups_to_add
|
||||
if group_to_add not in user_group_names and group_to_add not in user_group_paths
|
||||
]
|
||||
groups_to_remove = [
|
||||
group_to_remove
|
||||
for group_to_remove in groups_to_remove
|
||||
if group_to_remove in user_group_names or group_to_remove in user_group_paths
|
||||
]
|
||||
groups_to_add, groups_to_remove = self.filter_groups_to_add_to_and_remove_from_user(
|
||||
user_groups, groups_to_add, groups_to_remove
|
||||
)
|
||||
if not groups_to_add and not groups_to_remove:
|
||||
return False
|
||||
|
||||
@@ -3206,6 +3196,29 @@ class KeycloakAPI:
|
||||
groups_to_remove.append(group_name)
|
||||
return groups_to_add, groups_to_remove
|
||||
|
||||
def filter_groups_to_add_to_and_remove_from_user(self, user_groups, groups_to_add, groups_to_remove):
|
||||
"""
|
||||
Keep only the group membership changes that a user still needs.
|
||||
:param user_groups: The user's current groups, as returned by get_user_group_details.
|
||||
:param groups_to_add: Names or paths of the groups the user must be in.
|
||||
:param groups_to_remove: Names or paths of the groups the user must not be in.
|
||||
:return: The groups the user is not in yet, and the groups the user is in and must leave.
|
||||
"""
|
||||
user_group_names = [user_group["name"] for user_group in user_groups if "name" in user_group]
|
||||
user_group_paths = [user_group["path"] for user_group in user_groups if "path" in user_group]
|
||||
|
||||
groups_to_add = [
|
||||
group_to_add
|
||||
for group_to_add in groups_to_add
|
||||
if group_to_add not in user_group_names and group_to_add not in user_group_paths
|
||||
]
|
||||
groups_to_remove = [
|
||||
group_to_remove
|
||||
for group_to_remove in groups_to_remove
|
||||
if group_to_remove in user_group_names or group_to_remove in user_group_paths
|
||||
]
|
||||
return groups_to_add, groups_to_remove
|
||||
|
||||
def find_group_by_path(self, target, realm: str = "master"):
|
||||
"""
|
||||
Finds a realm group by path, e.g. '/my/group'.
|
||||
|
||||
Reference in New Issue
Block a user