mirror of https://github.com/jumpserver/jumpserver
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.7 KiB
52 lines
1.7 KiB
3 years ago
|
from django.dispatch import receiver
|
||
|
from django.apps import apps
|
||
|
from simple_history.signals import pre_create_historical_record
|
||
3 years ago
|
from django.db.models.signals import post_save, pre_save, pre_delete
|
||
3 years ago
|
|
||
|
from common.utils import get_logger
|
||
|
from ..models import AuthBook, SystemUser
|
||
|
|
||
|
AuthBookHistory = apps.get_model('assets', 'HistoricalAuthBook')
|
||
|
logger = get_logger(__name__)
|
||
|
|
||
|
|
||
|
@receiver(pre_create_historical_record, sender=AuthBookHistory)
|
||
3 years ago
|
def pre_create_historical_record_callback(sender, history_instance=None, **kwargs):
|
||
3 years ago
|
attrs_to_copy = ['username', 'password', 'private_key']
|
||
|
|
||
|
for attr in attrs_to_copy:
|
||
|
if getattr(history_instance, attr):
|
||
|
continue
|
||
3 years ago
|
try:
|
||
|
system_user = history_instance.systemuser
|
||
|
except SystemUser.DoesNotExist:
|
||
|
continue
|
||
|
if not system_user:
|
||
3 years ago
|
continue
|
||
|
system_user_attr_value = getattr(history_instance.systemuser, attr)
|
||
|
if system_user_attr_value:
|
||
|
setattr(history_instance, attr, system_user_attr_value)
|
||
|
|
||
|
|
||
3 years ago
|
@receiver(pre_delete, sender=AuthBook)
|
||
3 years ago
|
def on_authbook_post_delete(sender, instance, **kwargs):
|
||
|
instance.remove_asset_admin_user_if_need()
|
||
|
|
||
|
|
||
3 years ago
|
@receiver(post_save, sender=AuthBook)
|
||
3 years ago
|
def on_authbook_post_create(sender, instance, created, **kwargs):
|
||
3 years ago
|
instance.sync_to_system_user_account()
|
||
3 years ago
|
if created:
|
||
3 years ago
|
pass
|
||
|
# # 不再自动更新资产管理用户,只允许用户手动指定。
|
||
3 years ago
|
# 只在创建时进行更新资产的管理用户
|
||
3 years ago
|
# instance.update_asset_admin_user_if_need()
|
||
3 years ago
|
|
||
|
|
||
|
@receiver(pre_save, sender=AuthBook)
|
||
|
def on_authbook_pre_create(sender, instance, **kwargs):
|
||
|
# 升级版本号
|
||
3 years ago
|
instance.version += 1
|
||
3 years ago
|
# 即使在 root 组织也不怕
|
||
|
instance.org_id = instance.asset.org_id
|