• BaiJiangJie's avatar
    [Feature] 授权规则添加 actions 选项,控制用户对资产的操作行为 (#2610) · f235e201
    BaiJiangJie authored
    * [Feature] 1. perms actions - 添加 Action Model
    
    * [Feature] 2. perms actions - 添加 Action API
    
    * [Feature] 3. perms actions - 授权规则: 添加actions字段
    
    * [Feature] 4. perms actions - 授权规则创建页面: 设置 actions 默认 all
    
    * [Feature] 5. perms actions - 资产授权工具: 动态给system_user设置actions属性; 修改授权相关的API-serializer类: 添加actions字段值
    
    * [Feature] 6. perms actions - 更新API(用户使用系统用户连接资产时权限校验): 添加actions校验
    
    * [Feature] 7. perms actions - 迁移文件中为已经存在的perms添加默认的action
    
    * [Feature] 8. perms actions - 创建授权规则时设置默认action(如果actions字段值为空)
    
    * [Feature] 9. check actions - 修改校验用户资产权限API逻辑(添加actions校验)
    
    * [Feature] 10. check actions - 修改注释
    
    * [Feature] 11. check actions - 添加API: 获取用户指定资产和系统用户被授权的actions
    
    * [Feature] 12. check actions - 添加翻译信息
    f235e201
signals_handler.py 2.81 KB
# -*- coding: utf-8 -*-
#
from django.db.models.signals import m2m_changed, post_save, post_delete
from django.dispatch import receiver
from django.db import transaction

from common.utils import get_logger
from .utils import AssetPermissionUtil
from .models import AssetPermission, Action


logger = get_logger(__file__)


def on_transaction_commit(func):
    """
    如果不调用on_commit, 对象创建时添加多对多字段值失败
    """
    def inner(*args, **kwargs):
        transaction.on_commit(lambda: func(*args, **kwargs))
    return inner


@receiver(post_save, sender=AssetPermission, dispatch_uid="my_unique_identifier")
@on_transaction_commit
def on_permission_created(sender, instance=None, created=False, **kwargs):
    actions = instance.actions.all()
    if created and not actions:
        default_action = Action.get_action_all()
        instance.actions.add(default_action)
        logger.debug(
            "Set default action to perms: {}".format(default_action, instance)
        )


@receiver(post_save, sender=AssetPermission)
def on_permission_update(sender, **kwargs):
    AssetPermissionUtil.expire_all_cache()


@receiver(post_delete, sender=AssetPermission)
def on_permission_delete(sender, **kwargs):
    AssetPermissionUtil.expire_all_cache()


@receiver(m2m_changed, sender=AssetPermission.nodes.through)
def on_permission_nodes_changed(sender, instance=None, **kwargs):
    if isinstance(instance, AssetPermission) and kwargs['action'] == 'post_add':
        logger.debug("Asset permission nodes change signal received")
        nodes = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
        system_users = instance.system_users.all()
        for system_user in system_users:
            system_user.nodes.add(*tuple(nodes))


@receiver(m2m_changed, sender=AssetPermission.assets.through)
def on_permission_assets_changed(sender, instance=None, **kwargs):
    if isinstance(instance, AssetPermission) and kwargs['action'] == 'post_add':
        logger.debug("Asset permission assets change signal received")
        assets = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
        system_users = instance.system_users.all()
        for system_user in system_users:
            system_user.assets.add(*tuple(assets))


@receiver(m2m_changed, sender=AssetPermission.system_users.through)
def on_permission_system_users_changed(sender, instance=None, **kwargs):
    if isinstance(instance, AssetPermission) and kwargs['action'] == 'post_add':
        logger.debug("Asset permission system_users change signal received")
        system_users = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
        assets = instance.assets.all()
        nodes = instance.nodes.all()
        for system_user in system_users:
            system_user.nodes.add(*tuple(nodes))
            system_user.assets.add(*tuple(assets))