From 62eb131f59475a83b2fd8419c462c40fd2bfbcc2 Mon Sep 17 00:00:00 2001 From: Bai Date: Mon, 21 Jun 2021 18:36:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=97=B6=E5=A6=82=E6=9E=9C=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=9C=A8=E4=BB=BB=E4=BD=95=E7=BB=84=E7=BB=87=E5=86=85=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E6=B7=BB=E5=8A=A0=E5=88=B0default=E7=BB=84=E7=BB=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/orgs/signals_handler/common.py | 11 +++++++++++ apps/settings/utils/ldap.py | 7 +------ utils/migrate_unorg_users_to_default_org.sh | 9 +++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 utils/migrate_unorg_users_to_default_org.sh diff --git a/apps/orgs/signals_handler/common.py b/apps/orgs/signals_handler/common.py index fb8b2a2d1..942c82cbb 100644 --- a/apps/orgs/signals_handler/common.py +++ b/apps/orgs/signals_handler/common.py @@ -15,6 +15,7 @@ from orgs.hands import set_current_org, Node, get_current_org from perms.models import (AssetPermission, ApplicationPermission) from users.models import UserGroup, User from common.const.signals import PRE_REMOVE, POST_REMOVE +from common.decorator import on_transaction_commit from common.signals import django_ready from common.utils import get_logger from common.utils.connection import RedisPubSub @@ -167,3 +168,13 @@ def on_org_user_changed(action, instance, reverse, pk_set, **kwargs): leaved_users = set(pk_set) - set(org.members.filter(id__in=user_pk_set).values_list('id', flat=True)) _clear_users_from_org(org, leaved_users) + + +@receiver(post_save, sender=User) +@on_transaction_commit +def on_user_created_set_default_org(sender, instance, created, **kwargs): + if not created: + return + if instance.orgs.count() > 0: + return + Organization.default().members.add(instance) diff --git a/apps/settings/utils/ldap.py b/apps/settings/utils/ldap.py index 6eeab1daa..0cac92a4c 100644 --- a/apps/settings/utils/ldap.py +++ b/apps/settings/utils/ldap.py @@ -365,17 +365,12 @@ class LDAPImportUtil(object): def perform_import(self, users): logger.info('Start perform import ldap users, count: {}'.format(len(users))) errors = [] - instances = [] for user in users: try: - obj, created = self.update_or_create(user) - if created: - instances.append(obj) + self.update_or_create(user) except Exception as e: errors.append({user['username']: str(e)}) logger.error(e) - # 默认添加用户到 Default 组织 - Organization.default().members.add(*instances) logger.info('End perform import ldap users') return errors diff --git a/utils/migrate_unorg_users_to_default_org.sh b/utils/migrate_unorg_users_to_default_org.sh new file mode 100644 index 000000000..e26ca7525 --- /dev/null +++ b/utils/migrate_unorg_users_to_default_org.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# + +python ../apps/manage.py shell << EOF +from users.models import User +from orgs.models import Organization +unorgs_users = [user for user in User.objects.all() if user.orgs.count() == 0] +Organization.default().members.add(*unorgs_users) +EOF