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