2021-02-05 05:29:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.db.models.signals import m2m_changed, pre_delete, pre_save, post_save
|
|
|
|
from django.dispatch import receiver
|
|
|
|
|
|
|
|
from assets.models import Asset
|
|
|
|
from common.const.signals import POST_ADD, POST_REMOVE, POST_CLEAR
|
2024-01-02 08:11:56 +00:00
|
|
|
from common.exceptions import M2MReverseNotAllowed
|
|
|
|
from common.utils import get_logger, get_object_or_none
|
2021-02-05 05:29:29 +00:00
|
|
|
from perms.models import AssetPermission
|
2022-12-09 06:03:00 +00:00
|
|
|
from perms.utils import UserPermTreeExpireUtil
|
2024-01-02 08:11:56 +00:00
|
|
|
from users.models import User, UserGroup
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2021-07-12 09:54:08 +00:00
|
|
|
@receiver(pre_delete, sender=UserGroup)
|
|
|
|
def on_user_group_delete(sender, instance: UserGroup, using, **kwargs):
|
|
|
|
exists = AssetPermission.user_groups.through.objects.filter(usergroup_id=instance.id).exists()
|
|
|
|
if not exists:
|
|
|
|
return
|
2022-12-09 05:05:34 +00:00
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_user_group(instance)
|
2021-07-12 09:54:08 +00:00
|
|
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
@receiver(m2m_changed, sender=User.groups.through)
|
|
|
|
def on_user_groups_change(sender, instance, action, reverse, pk_set, **kwargs):
|
2021-03-17 07:04:34 +00:00
|
|
|
if not action.startswith('post'):
|
|
|
|
return
|
|
|
|
if reverse:
|
|
|
|
group_ids = [instance.id]
|
|
|
|
user_ids = pk_set
|
2021-03-18 02:50:36 +00:00
|
|
|
org_id = instance.org_id
|
2021-03-17 07:04:34 +00:00
|
|
|
else:
|
|
|
|
group_ids = pk_set
|
|
|
|
user_ids = [instance.id]
|
2021-03-19 06:31:57 +00:00
|
|
|
group = UserGroup.objects.get(id=list(group_ids)[0])
|
2021-03-18 02:50:36 +00:00
|
|
|
org_id = group.org_id
|
|
|
|
|
2024-01-02 08:11:56 +00:00
|
|
|
has_group_perm = AssetPermission.user_groups.through.objects \
|
2022-12-09 05:05:34 +00:00
|
|
|
.filter(usergroup_id__in=group_ids).exists()
|
|
|
|
if not has_group_perm:
|
2021-03-17 07:04:34 +00:00
|
|
|
return
|
2021-03-18 02:50:36 +00:00
|
|
|
|
2022-12-09 05:05:34 +00:00
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_users_orgs(user_ids, [org_id])
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver([pre_delete], sender=AssetPermission)
|
|
|
|
def on_asset_perm_pre_delete(sender, instance, **kwargs):
|
2022-12-09 05:05:34 +00:00
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_perms([instance.id])
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver([pre_save], sender=AssetPermission)
|
|
|
|
def on_asset_perm_pre_save(sender, instance, **kwargs):
|
2022-12-09 05:05:34 +00:00
|
|
|
old = get_object_or_none(AssetPermission, pk=instance.id)
|
|
|
|
if not old:
|
|
|
|
return
|
|
|
|
if old.is_valid == instance.is_valid:
|
|
|
|
return
|
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_perms([instance.id])
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver([post_save], sender=AssetPermission)
|
|
|
|
def on_asset_perm_post_save(sender, instance, created, **kwargs):
|
2021-03-17 07:04:34 +00:00
|
|
|
if not created:
|
|
|
|
return
|
2022-12-09 05:05:34 +00:00
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_perms([instance.id])
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def need_rebuild_mapping_node(action):
|
|
|
|
return action in (POST_REMOVE, POST_ADD, POST_CLEAR)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=AssetPermission.nodes.through)
|
|
|
|
def on_permission_nodes_changed(sender, instance, action, reverse, **kwargs):
|
2021-03-17 07:04:34 +00:00
|
|
|
if not need_rebuild_mapping_node(action):
|
|
|
|
return
|
2022-12-09 05:05:34 +00:00
|
|
|
if reverse:
|
|
|
|
raise M2MReverseNotAllowed
|
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_perms([instance.id])
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=AssetPermission.assets.through)
|
|
|
|
def on_permission_assets_changed(sender, instance, action, reverse, pk_set, model, **kwargs):
|
2021-03-17 07:04:34 +00:00
|
|
|
if not need_rebuild_mapping_node(action):
|
|
|
|
return
|
2022-12-09 05:05:34 +00:00
|
|
|
if reverse:
|
|
|
|
raise M2MReverseNotAllowed
|
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_perms([instance.id])
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=AssetPermission.users.through)
|
2021-03-17 07:04:34 +00:00
|
|
|
def on_asset_permission_users_changed(sender, action, reverse, instance, pk_set, **kwargs):
|
2021-02-05 05:29:29 +00:00
|
|
|
if reverse:
|
|
|
|
raise M2MReverseNotAllowed
|
2021-03-17 07:04:34 +00:00
|
|
|
if not need_rebuild_mapping_node(action):
|
|
|
|
return
|
2022-12-09 05:05:34 +00:00
|
|
|
user_ids = pk_set
|
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_users_orgs(user_ids, [instance.org.id])
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=AssetPermission.user_groups.through)
|
2021-03-17 07:04:34 +00:00
|
|
|
def on_asset_permission_user_groups_changed(sender, instance, action, pk_set, reverse, **kwargs):
|
|
|
|
if not need_rebuild_mapping_node(action):
|
|
|
|
return
|
2022-12-09 05:05:34 +00:00
|
|
|
if reverse:
|
|
|
|
raise M2MReverseNotAllowed
|
2021-03-17 07:04:34 +00:00
|
|
|
|
2022-12-09 05:05:34 +00:00
|
|
|
group_ids = pk_set
|
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_user_groups_orgs(group_ids, [instance.org.id])
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=Asset.nodes.through)
|
|
|
|
def on_node_asset_change(action, instance, reverse, pk_set, **kwargs):
|
|
|
|
if not need_rebuild_mapping_node(action):
|
|
|
|
return
|
2024-03-27 08:04:16 +00:00
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
if reverse:
|
2022-12-09 05:05:34 +00:00
|
|
|
asset_ids = pk_set
|
|
|
|
node_ids = [instance.id]
|
2021-02-05 05:29:29 +00:00
|
|
|
else:
|
2022-12-09 05:05:34 +00:00
|
|
|
asset_ids = [instance.id]
|
|
|
|
node_ids = pk_set
|
2021-02-05 05:29:29 +00:00
|
|
|
|
2022-12-09 05:05:34 +00:00
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_nodes_assets(node_ids, asset_ids)
|