Skip to content

Commit

Permalink
Merge pull request #409 from whdalsrnt/master
Browse files Browse the repository at this point in the history
refactor: Implement user deletion of user group when workspace role b…
  • Loading branch information
whdalsrnt authored Nov 22, 2024
2 parents 3b79e85 + 2a7c1ec commit 4b4bf37
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
15 changes: 14 additions & 1 deletion src/spaceone/identity/manager/role_binding_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from spaceone.core.manager import BaseManager

from spaceone.identity.model.role_binding.database import RoleBinding
from spaceone.identity.manager.user_group_manager import UserGroupManager

_LOGGER = logging.getLogger(__name__)


class RoleBindingManager(BaseManager):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.role_binding_model = RoleBinding
Expand Down Expand Up @@ -46,6 +46,19 @@ def delete_role_binding_by_vo(role_binding_vo: RoleBinding) -> None:
)
role_binding_vo.delete()

if role_binding_vo.workspace_id:
# Delete user from user groups
user_group_mgr = UserGroupManager()
user_group_vos = user_group_mgr.filter_user_groups(
users=role_binding_vo.user_id, domain_id=role_binding_vo.domain_id
)
for user_group_vo in user_group_vos:
users = user_group_vo.users
users.remove(role_binding_vo.user_id)
user_group_mgr.update_user_group_by_vo(
{"users": users}, user_group_vo=user_group_vo
)

def get_role_binding(
self, role_binding_id: str, domain_id: str, workspace_id: str = None
) -> RoleBinding:
Expand Down
27 changes: 5 additions & 22 deletions src/spaceone/identity/service/role_binding_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from spaceone.identity.manager.role_manager import RoleManager
from spaceone.identity.manager.user_manager import UserManager
from spaceone.identity.manager.workspace_manager import WorkspaceManager
from spaceone.identity.manager.user_group_manager import UserGroupManager
from spaceone.identity.model import RoleBinding
from spaceone.identity.model.role_binding.request import *
from spaceone.identity.model.role_binding.response import *

Expand Down Expand Up @@ -263,7 +261,11 @@ def delete(self, params: RoleBindingDeleteRequest) -> None:
user_vo = self.user_mgr.get_user(rb_vo.user_id, rb_vo.domain_id)

self.user_mgr.update_user_by_vo(user_role_info, user_vo)
self.delete_role_binding_by_vo(rb_vo)

if rb_vo.workspace_id:
self.update_workspace_user_count(rb_vo.domain_id, rb_vo.workspace_id)

self.role_binding_manager.delete_role_binding_by_vo(rb_vo)

@transaction(
permission="identity:RoleBinding.read",
Expand Down Expand Up @@ -461,25 +463,6 @@ def update_workspace_user_count(self, domain_id: str, workspace_id: str) -> None
{"user_count": user_rb_total_count}, workspace_vo
)

def delete_role_binding_by_vo(self, rb_vo: RoleBinding) -> None:
self.role_binding_manager.delete_role_binding_by_vo(rb_vo)

if rb_vo.workspace_id:
# Delete user from user groups
user_group_mgr = UserGroupManager()
user_group_vos = user_group_mgr.filter_user_groups(
users=rb_vo.user_id, domain_id=rb_vo.domain_id
)
for user_group_vo in user_group_vos:
users = user_group_vo.users
users.remove(rb_vo.user_id)
user_group_mgr.update_user_group_by_vo(
{"users": users}, user_group_vo=user_group_vo
)

# Update workspace user count
self.update_workspace_user_count(rb_vo.domain_id, rb_vo.workspace_id)

def _get_workspace_user_count(self, domain_id: str, workspace_id: str) -> int:
user_rb_ids = self.role_binding_manager.stat_role_bindings(
query={
Expand Down

0 comments on commit 4b4bf37

Please sign in to comment.