From 2529ef2211f2209e0c1ac34c9b098078bf1d5ce2 Mon Sep 17 00:00:00 2001 From: xinwen Date: Thu, 15 Oct 2020 17:56:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(orgs):=20=E6=9B=B4=E6=96=B0=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=97=B6`org=5Froles`=E5=8F=82=E6=95=B0=E4=B8=BA`None?= =?UTF-8?q?`=E6=97=B6=E4=B8=8D=E6=9B=B4=E6=96=B0=E7=BB=84=E7=BB=87?= =?UTF-8?q?=E8=A7=92=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/users/api/user.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/apps/users/api/user.py b/apps/users/api/user.py index 148378522..b37b2a6c4 100644 --- a/apps/users/api/user.py +++ b/apps/users/api/user.py @@ -52,6 +52,19 @@ class UserViewSet(CommonApiMixin, UserQuerysetMixin, BulkModelViewSet): for user in users: post_user_create.send(self.__class__, user=user) + @staticmethod + def set_users_to_org(users, org_roles, update=False): + # 只有真实存在的组织才真正关联用户 + if not current_org or not current_org.is_real(): + return + for user, roles in zip(users, org_roles): + if update and roles is None: + continue + if not roles: + # 当前组织创建的用户,至少是该组织的`User` + roles = [ORG_ROLE.USER] + OrganizationMember.objects.set_user_roles(current_org, user, roles) + def perform_create(self, serializer): validated_data = serializer.validated_data @@ -104,11 +117,7 @@ class UserViewSet(CommonApiMixin, UserQuerysetMixin, BulkModelViewSet): users = serializer.save() if isinstance(users, User): users = [users] - if current_org and current_org.is_real(): - for user, roles in zip(users, org_roles): - if roles is not None: - # roles 是 `Node` 表明不需要更新 - OrganizationMember.objects.set_user_roles(current_org, user, roles) + self.set_users_to_org(users, org_roles, update=True) def perform_bulk_update(self, serializer): # TODO: 需要测试