diff --git a/apps/assets/models/account.py b/apps/assets/models/account.py index 7660ff682..3c0588cdf 100644 --- a/apps/assets/models/account.py +++ b/apps/assets/models/account.py @@ -14,8 +14,10 @@ class Account(BaseUser, AbsConnectivity, ProtocolMixin): common = 'common', _('Common user') admin = 'admin', _('Admin user') - protocol = models.CharField(max_length=16, choices=ProtocolMixin.Protocol.choices, - default='ssh', verbose_name=_('Protocol')) + protocol = models.CharField( + max_length=16, choices=ProtocolMixin.Protocol.choices, + default='ssh', verbose_name=_('Protocol') + ) type = models.CharField(max_length=16, choices=Type.choices, default=Type.common, verbose_name=_("Type")) asset = models.ForeignKey('assets.Asset', on_delete=models.CASCADE, verbose_name=_('Asset')) version = models.IntegerField(default=0, verbose_name=_('Version')) diff --git a/apps/assets/models/user.py b/apps/assets/models/user.py index 661f89480..5affa7367 100644 --- a/apps/assets/models/user.py +++ b/apps/assets/models/user.py @@ -199,6 +199,10 @@ class SystemUser(ProtocolMixin, BaseUser): return self.su_from.assets.add(*tuple(assets_or_ids)) + @classmethod + def create_accounts_with_assets(cls, asset_ids, system_user_ids): + pass + class Meta: ordering = ['name'] unique_together = [('name', 'org_id')] @@ -208,6 +212,12 @@ class SystemUser(ProtocolMixin, BaseUser): ] +class SystemUserAccount(models.Model): + system_user = models.ForeignKey('SystemUser', on_delete=models.CASCADE, related_name='accounts') + account = models.ForeignKey('assets.Account', on_delete=models.CASCADE, related_name='system_users') + date_created = models.DateTimeField(auto_now_add=True) + + # Deprecated: 准备废弃 class AdminUser(BaseUser): """ diff --git a/apps/assets/signal_handlers/system_user.py b/apps/assets/signal_handlers/system_user.py index 64a656c29..e0dfe3ebf 100644 --- a/apps/assets/signal_handlers/system_user.py +++ b/apps/assets/signal_handlers/system_user.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # from django.db.models.signals import ( - post_save, m2m_changed, pre_save, pre_delete, post_delete + post_save, m2m_changed ) from django.dispatch import receiver @@ -32,6 +32,9 @@ def on_system_user_assets_change(instance, action, model, pk_set, **kwargs): logger.debug('No system user found') return + if action != POST_ADD: + return + if model == Asset: system_user_ids = [instance.id] asset_ids = pk_set @@ -39,6 +42,7 @@ def on_system_user_assets_change(instance, action, model, pk_set, **kwargs): system_user_ids = pk_set asset_ids = [instance.id] # todo: Auto create account if need + SystemUser.create_accounts_with_assets(asset_ids, system_user_ids) @receiver(m2m_changed, sender=SystemUser.users.through)