Browse Source

fix: 修改资产和特权用户发生变化时,更新资产的admin_user (#6871)

* fix: 修改资产和特权用户发生变化时,更新资产的admin_user

* fix: 修改资产和特权用户发生变化时,更新资产的admin_user

* fix: 修改资产和特权用户发生变化时,更新资产的admin_user

Co-authored-by: Michael Bai <baijiangjie@gmail.com>
pull/6873/head
fit2bot 3 years ago committed by GitHub
parent
commit
fbe5f9a63a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      apps/assets/models/authbook.py
  2. 11
      apps/assets/signals_handler/authbook.py

23
apps/assets/models/authbook.py

@ -5,9 +5,12 @@ from django.db import models
from django.utils.translation import ugettext_lazy as _
from simple_history.models import HistoricalRecords
from common.utils import lazyproperty
from common.utils import lazyproperty, get_logger
from .base import BaseUser, AbsConnectivity
logger = get_logger(__name__)
__all__ = ['AuthBook']
@ -93,6 +96,24 @@ class AuthBook(BaseUser, AbsConnectivity):
i.comment = 'Update triggered by account {}'.format(self.id)
i.save(update_fields=['password', 'private_key', 'public_key'])
def remove_asset_admin_user_if_need(self):
if not self.asset or not self.asset.admin_user:
return
if not self.systemuser.is_admin_user:
return
logger.debug('Remove asset admin user: {} {}'.format(self.asset, self.systemuser))
self.asset.admin_user = None
self.asset.save()
def update_asset_admin_user_if_need(self):
if not self.systemuser or not self.systemuser.is_admin_user:
return
if not self.asset or self.asset.admin_user == self.systemuser:
return
logger.debug('Update asset admin user: {} {}'.format(self.asset, self.systemuser))
self.asset.admin_user = self.systemuser
self.asset.save()
def __str__(self):
return self.smart_name

11
apps/assets/signals_handler/authbook.py

@ -1,7 +1,7 @@
from django.dispatch import receiver
from django.apps import apps
from simple_history.signals import pre_create_historical_record
from django.db.models.signals import post_save, pre_save
from django.db.models.signals import post_save, pre_save, post_delete
from common.utils import get_logger
from ..models import AuthBook, SystemUser
@ -28,10 +28,15 @@ def pre_create_historical_record_callback(sender, history_instance=None, **kwarg
setattr(history_instance, attr, system_user_attr_value)
@receiver(post_delete, sender=AuthBook)
def on_authbook_post_delete(sender, instance, **kwargs):
instance.remove_asset_admin_user_if_need()
@receiver(post_save, sender=AuthBook)
def on_authbook_post_create(sender, instance, **kwargs):
if not instance.systemuser:
instance.sync_to_system_user_account()
instance.sync_to_system_user_account()
instance.update_asset_admin_user_if_need()
@receiver(pre_save, sender=AuthBook)

Loading…
Cancel
Save