2018-02-09 03:12:40 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-03-05 11:47:14 +00:00
|
|
|
from django.db.models.signals import m2m_changed, post_save, post_delete
|
2018-02-09 03:12:40 +00:00
|
|
|
from django.dispatch import receiver
|
|
|
|
|
|
|
|
from common.utils import get_logger
|
2019-08-23 10:23:07 +00:00
|
|
|
from common.decorator import on_transaction_commit
|
2019-07-02 08:45:26 +00:00
|
|
|
from .models import AssetPermission
|
2019-09-11 10:52:42 +00:00
|
|
|
from .utils.asset_permission import AssetPermissionUtilV2
|
2018-02-09 03:12:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
2019-09-11 10:52:42 +00:00
|
|
|
permission_m2m_senders = (
|
|
|
|
AssetPermission.nodes.through,
|
|
|
|
AssetPermission.assets.through,
|
|
|
|
AssetPermission.users.through,
|
|
|
|
AssetPermission.user_groups.through,
|
|
|
|
)
|
|
|
|
|
2018-02-09 03:12:40 +00:00
|
|
|
|
2019-09-11 10:52:42 +00:00
|
|
|
@receiver([post_save, post_delete], sender=AssetPermission)
|
|
|
|
@on_transaction_commit
|
|
|
|
def on_permission_change(sender, action='', **kwargs):
|
|
|
|
logger.debug('Asset permission changed, refresh user tree cache')
|
|
|
|
AssetPermissionUtilV2.expire_all_user_tree_cache()
|
|
|
|
|
|
|
|
# Todo: 检查授权规则到期,从而修改授权规则
|
2019-03-05 11:47:14 +00:00
|
|
|
|
|
|
|
|
2018-04-08 12:02:40 +00:00
|
|
|
@receiver(m2m_changed, sender=AssetPermission.nodes.through)
|
2019-08-28 03:43:55 +00:00
|
|
|
def on_permission_nodes_changed(sender, instance=None, action='', **kwargs):
|
|
|
|
if action != 'post_add':
|
|
|
|
return
|
|
|
|
if isinstance(instance, AssetPermission):
|
2018-04-08 12:02:40 +00:00
|
|
|
logger.debug("Asset permission nodes change signal received")
|
|
|
|
nodes = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
2019-09-12 09:58:52 +00:00
|
|
|
system_users = instance.system_users.all()
|
2018-04-08 12:02:40 +00:00
|
|
|
for system_user in system_users:
|
|
|
|
system_user.nodes.add(*tuple(nodes))
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=AssetPermission.assets.through)
|
2019-08-28 03:43:55 +00:00
|
|
|
def on_permission_assets_changed(sender, instance=None, action='', **kwargs):
|
|
|
|
if action != 'post_add':
|
|
|
|
return
|
|
|
|
if isinstance(instance, AssetPermission):
|
2018-04-08 12:02:40 +00:00
|
|
|
logger.debug("Asset permission assets change signal received")
|
|
|
|
assets = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
2019-09-12 09:58:52 +00:00
|
|
|
system_users = instance.system_users.all()
|
2018-04-08 12:02:40 +00:00
|
|
|
for system_user in system_users:
|
|
|
|
system_user.assets.add(*tuple(assets))
|
|
|
|
|
2018-02-09 03:12:40 +00:00
|
|
|
|
2018-04-08 12:02:40 +00:00
|
|
|
@receiver(m2m_changed, sender=AssetPermission.system_users.through)
|
2019-08-28 03:43:55 +00:00
|
|
|
def on_permission_system_users_changed(sender, instance=None, action='', **kwargs):
|
|
|
|
if action != 'post_add':
|
|
|
|
return
|
|
|
|
if isinstance(instance, AssetPermission):
|
2018-04-08 12:02:40 +00:00
|
|
|
system_users = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
2019-08-28 03:43:55 +00:00
|
|
|
logger.debug("Asset permission system_users change signal received")
|
2019-09-11 10:52:42 +00:00
|
|
|
assets = instance.assets.all().values_list('id', flat=True)
|
|
|
|
nodes = instance.nodes.all().values_list('id', flat=True)
|
2018-04-08 12:02:40 +00:00
|
|
|
for system_user in system_users:
|
|
|
|
system_user.nodes.add(*tuple(nodes))
|
|
|
|
system_user.assets.add(*tuple(assets))
|
2019-08-28 03:43:55 +00:00
|
|
|
|