From 081869a494c57ca09c4bb046ad0b532290d5913c Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Tue, 10 Sep 2024 15:05:19 +0900 Subject: [PATCH] feat: delete user's workspace groups when delete the user Signed-off-by: Youngjin Jo --- src/spaceone/identity/manager/user_manager.py | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/spaceone/identity/manager/user_manager.py b/src/spaceone/identity/manager/user_manager.py index affcc21..645ff67 100644 --- a/src/spaceone/identity/manager/user_manager.py +++ b/src/spaceone/identity/manager/user_manager.py @@ -11,7 +11,8 @@ from spaceone.identity.manager.project_manager import ProjectManager from spaceone.identity.manager.role_binding_manager import RoleBindingManager from spaceone.identity.manager.user_group_manager import UserGroupManager -from spaceone.identity.manager.workspace_group_manager import WorkspaceGroupManager +from spaceone.identity.manager.workspace_group_manager import \ + WorkspaceGroupManager from spaceone.identity.model.user.database import User _LOGGER = logging.getLogger(__name__) @@ -127,16 +128,24 @@ def delete_user_by_vo(user_vo: User) -> None: users.remove(user_vo.user_id) project_mgr.update_project_by_vo({"users": users}, project_vo=project_vo) - # TODO: Delete Workspace Group + # Delete workspace groups workspace_group_vos = workspace_group_mgr.filter_workspace_groups( - users=user_vo.user_id, domain_id=user_vo.domain_id + users__user_id=user_vo.user_id, domain_id=user_vo.domain_id ) + for workspace_group_vo in workspace_group_vos: - users = workspace_group_vo.users - users.remove(user_vo.user_id) - workspace_group_mgr.update_workspace_group_by_vo( - {"users": users}, workspace_group_vo=workspace_group_vo - ) + workspace_group_dict = workspace_group_vo.to_mongo().to_dict() + users = workspace_group_dict.get("users", []) + + if users: + updated_users = [ + user for user in users if user.get("user_id") != user_vo.user_id + ] + + if len(updated_users) != len(users): + workspace_group_mgr.update_workspace_group_by_vo( + {"users": updated_users}, workspace_group_vo=workspace_group_vo + ) user_vo.delete()