Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify code such that when deleting the user and adding the workspace_group_id filter #356

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/spaceone/identity/manager/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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()

Expand Down
47 changes: 26 additions & 21 deletions src/spaceone/identity/service/user_profile_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,21 @@
from spaceone.identity.manager.mfa_manager.base import MFAManager
from spaceone.identity.manager.role_binding_manager import RoleBindingManager
from spaceone.identity.manager.role_manager import RoleManager
from spaceone.identity.manager.token_manager.local_token_manager import (
LocalTokenManager,
)
from spaceone.identity.manager.token_manager.local_token_manager import \
LocalTokenManager
from spaceone.identity.manager.user_manager import UserManager
from spaceone.identity.manager.workspace_group_manager import WorkspaceGroupManager
from spaceone.identity.manager.workspace_group_manager import \
WorkspaceGroupManager
from spaceone.identity.manager.workspace_manager import WorkspaceManager
from spaceone.identity.model.user.database import User
from spaceone.identity.model.user.response import *
from spaceone.identity.model.user_profile.request import *
from spaceone.identity.model.user_profile.request import (
UserProfileGetWorkspaceGroupsRequest,
)
from spaceone.identity.model.user_profile.request import \
UserProfileGetWorkspaceGroupsRequest
from spaceone.identity.model.user_profile.response import (
MyWorkspaceGroupsResponse,
MyWorkspacesResponse,
)
from spaceone.identity.service.workspace_group_service import WorkspaceGroupService
MyWorkspaceGroupsResponse, MyWorkspacesResponse)
from spaceone.identity.service.workspace_group_service import \
WorkspaceGroupService

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -350,41 +348,48 @@ def get_workspaces(
MyWorkspaceResponse:
"""

workspace_group_id = params.workspace_group_id
user_id = params.user_id
domain_id = params.domain_id

role_mgr = RoleManager()
rb_mgr = RoleBindingManager()
workspace_mgr = WorkspaceManager()
allow_all = False

user_vo = self.user_mgr.get_user(params.user_id, params.domain_id)
user_vo = self.user_mgr.get_user(user_id, domain_id)

if user_vo.role_type == "DOMAIN_ADMIN":
allow_all = True

conditions = {
"user_id": params.user_id,
"domain_id": params.domain_id,
"user_id": user_id,
"domain_id": domain_id,
"role_type": ["WORKSPACE_OWNER", "WORKSPACE_MEMBER"],
}

if params.workspace_group_id:
conditions["workspace_group_id"] = params.workspace_group_id
if workspace_group_id:
conditions["workspace_group_id"] = workspace_group_id

rb_vos = rb_mgr.filter_role_bindings(**conditions)

workspace_filter_conditions = {"domain_id": domain_id, "state": "ENABLED"}
if allow_all:
if workspace_group_id:
workspace_filter_conditions["workspace_group_id"] = workspace_group_id

workspace_vos = workspace_mgr.filter_workspaces(
domain_id=params.domain_id, state="ENABLED"
**workspace_filter_conditions
)
else:
workspace_ids = list(set([rb.workspace_id for rb in rb_vos]))
workspace_filter_conditions["workspace_id"] = workspace_ids
workspace_vos = workspace_mgr.filter_workspaces(
workspace_id=workspace_ids,
domain_id=params.domain_id,
state="ENABLED",
**workspace_filter_conditions
)

role_vos = role_mgr.filter_roles(
domain_id=params.domain_id,
domain_id=domain_id,
role_type=["WORKSPACE_OWNER", "WORKSPACE_MEMBER"],
)

Expand Down
Loading