2018-08-13 10:26:13 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
2020-12-14 12:31:12 +00:00
|
|
|
|
from collections import defaultdict
|
|
|
|
|
from functools import partial
|
2018-08-13 10:26:13 +00:00
|
|
|
|
|
2022-10-10 05:56:42 +00:00
|
|
|
|
from django.conf import settings
|
2023-02-13 11:42:42 +00:00
|
|
|
|
from django.db.models.signals import post_save, pre_delete, m2m_changed, post_delete
|
2022-11-21 11:54:00 +00:00
|
|
|
|
from django.db.utils import ProgrammingError, OperationalError
|
2022-12-07 07:09:01 +00:00
|
|
|
|
from django.dispatch import receiver
|
2022-11-17 05:44:50 +00:00
|
|
|
|
from django.utils.functional import LazyObject
|
2018-08-13 10:26:13 +00:00
|
|
|
|
|
2021-03-02 11:45:44 +00:00
|
|
|
|
from common.const.signals import PRE_REMOVE, POST_REMOVE
|
2023-02-13 11:42:42 +00:00
|
|
|
|
from common.decorators import delay_run
|
2023-02-08 02:14:09 +00:00
|
|
|
|
from common.decorators import on_transaction_commit
|
2021-03-03 03:20:40 +00:00
|
|
|
|
from common.signals import django_ready
|
|
|
|
|
from common.utils import get_logger
|
|
|
|
|
from common.utils.connection import RedisPubSub
|
2022-12-07 07:09:01 +00:00
|
|
|
|
from orgs.hands import set_current_org, Node, get_current_org
|
|
|
|
|
from orgs.models import Organization
|
|
|
|
|
from orgs.utils import tmp_to_org, set_to_default_org
|
|
|
|
|
from perms.models import AssetPermission
|
|
|
|
|
from users.models import UserGroup, User
|
2022-02-17 12:13:31 +00:00
|
|
|
|
from users.signals import post_user_leave_org
|
2021-03-03 03:20:40 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OrgsMappingForMemoryPubSub(LazyObject):
|
|
|
|
|
def _setup(self):
|
2022-12-07 07:09:01 +00:00
|
|
|
|
self._wrapped = RedisPubSub('fm.orgs_mapping')
|
2021-03-03 03:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
orgs_mapping_for_memory_pub_sub = OrgsMappingForMemoryPubSub()
|
|
|
|
|
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
def expire_orgs_mapping_for_memory(org_id):
|
|
|
|
|
orgs_mapping_for_memory_pub_sub.publish(str(org_id))
|
2021-03-03 03:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(django_ready)
|
|
|
|
|
def subscribe_orgs_mapping_expire(sender, **kwargs):
|
|
|
|
|
logger.debug("Start subscribe for expire orgs mapping from memory")
|
|
|
|
|
|
2022-10-10 05:56:42 +00:00
|
|
|
|
if settings.DEBUG:
|
2022-10-26 09:21:52 +00:00
|
|
|
|
try:
|
|
|
|
|
set_to_default_org()
|
2022-11-21 11:54:00 +00:00
|
|
|
|
except (ProgrammingError, OperationalError):
|
2022-10-26 09:21:52 +00:00
|
|
|
|
pass
|
2021-03-03 03:20:40 +00:00
|
|
|
|
|
2022-11-17 05:44:50 +00:00
|
|
|
|
orgs_mapping_for_memory_pub_sub.subscribe(
|
|
|
|
|
lambda org_id: Organization.expire_orgs_mapping()
|
|
|
|
|
)
|
2018-08-13 10:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
2023-02-13 11:42:42 +00:00
|
|
|
|
@delay_run(ttl=5)
|
2023-11-17 09:07:24 +00:00
|
|
|
|
def expire_user_orgs():
|
2023-02-13 11:42:42 +00:00
|
|
|
|
User.expire_users_rbac_perms_cache()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Organization)
|
|
|
|
|
def on_org_create(sender, instance, created=False, **kwargs):
|
|
|
|
|
expire_user_orgs()
|
|
|
|
|
|
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
|
# 创建对应的root
|
2018-08-13 10:26:13 +00:00
|
|
|
|
@receiver(post_save, sender=Organization)
|
2023-02-13 11:42:42 +00:00
|
|
|
|
def on_org_create_or_update(sender, instance, **kwargs):
|
2021-03-03 03:20:40 +00:00
|
|
|
|
# 必须放到最开始, 因为下面调用Node.save方法时会获取当前组织的org_id(即instance.org_id), 如果不过期会找不到
|
2021-07-08 06:23:18 +00:00
|
|
|
|
expire_orgs_mapping_for_memory(instance.id)
|
2022-12-07 09:24:30 +00:00
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
old_org = get_current_org()
|
|
|
|
|
set_current_org(instance)
|
|
|
|
|
node_root = Node.org_root()
|
|
|
|
|
if node_root.value != instance.name:
|
|
|
|
|
node_root.value = instance.name
|
|
|
|
|
node_root.save()
|
|
|
|
|
set_current_org(old_org)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(pre_delete, sender=Organization)
|
2023-05-16 08:30:41 +00:00
|
|
|
|
def delete_org_root_node_on_org_delete(sender, instance, **kwargs):
|
2021-07-08 06:23:18 +00:00
|
|
|
|
expire_orgs_mapping_for_memory(instance.id)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
2021-03-15 09:46:43 +00:00
|
|
|
|
# 删除该组织下所有 节点
|
|
|
|
|
with tmp_to_org(instance):
|
|
|
|
|
root_node = Node.org_root()
|
|
|
|
|
if root_node:
|
|
|
|
|
root_node.delete()
|
|
|
|
|
|
|
|
|
|
|
2023-02-13 11:42:42 +00:00
|
|
|
|
@receiver(post_delete, sender=Organization)
|
2023-05-16 08:30:41 +00:00
|
|
|
|
def expire_user_orgs_on_org_delete(sender, instance, **kwargs):
|
2023-02-13 11:42:42 +00:00
|
|
|
|
expire_user_orgs()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=User)
|
|
|
|
|
@on_transaction_commit
|
|
|
|
|
def on_user_created_set_default_org(sender, instance, created, **kwargs):
|
|
|
|
|
if not instance.id:
|
|
|
|
|
# 用户已被手动删除,instance.orgs 时会使用 id 进行查找报错,所以判断不存在id时不做处理
|
|
|
|
|
return
|
|
|
|
|
if not created:
|
|
|
|
|
return
|
|
|
|
|
if instance.orgs.count() > 0:
|
|
|
|
|
return
|
2023-07-28 08:15:12 +00:00
|
|
|
|
default_org = Organization.default()
|
|
|
|
|
with tmp_to_org(default_org):
|
|
|
|
|
default_org.add_member(instance)
|
|
|
|
|
default_group = UserGroup.objects.filter(name='Default').first()
|
|
|
|
|
if default_group:
|
|
|
|
|
default_group.users.add(instance)
|
2023-02-13 11:42:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _remove_user_resource(model, users, org, user_field_name='users'):
|
2020-08-20 07:19:06 +00:00
|
|
|
|
with tmp_to_org(org):
|
|
|
|
|
if not isinstance(users, (tuple, list, set)):
|
2022-11-17 05:44:50 +00:00
|
|
|
|
users = (users,)
|
2020-07-20 02:42:22 +00:00
|
|
|
|
|
2021-09-15 08:57:45 +00:00
|
|
|
|
user_field = getattr(model, user_field_name)
|
|
|
|
|
m2m_model = user_field.through
|
|
|
|
|
reverse = user_field.reverse
|
2020-12-14 12:31:12 +00:00
|
|
|
|
if reverse:
|
2021-09-15 08:57:45 +00:00
|
|
|
|
m2m_field_name = user_field.field.m2m_reverse_field_name()
|
2020-08-20 07:19:06 +00:00
|
|
|
|
else:
|
2021-09-15 08:57:45 +00:00
|
|
|
|
m2m_field_name = user_field.field.m2m_field_name()
|
2020-12-14 12:31:12 +00:00
|
|
|
|
relations = m2m_model.objects.filter(**{
|
|
|
|
|
'user__in': users,
|
|
|
|
|
f'{m2m_field_name}__org_id': org.id
|
|
|
|
|
})
|
|
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
|
object_id_user_ids_map = defaultdict(set)
|
2020-12-14 12:31:12 +00:00
|
|
|
|
|
|
|
|
|
m2m_field_attr_name = f'{m2m_field_name}_id'
|
|
|
|
|
for relation in relations:
|
|
|
|
|
object_id = getattr(relation, m2m_field_attr_name)
|
2021-03-08 02:08:51 +00:00
|
|
|
|
object_id_user_ids_map[object_id].add(relation.user_id)
|
2020-12-14 12:31:12 +00:00
|
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
|
objects = model.objects.filter(id__in=object_id_user_ids_map.keys())
|
2020-12-14 12:31:12 +00:00
|
|
|
|
send_m2m_change_signal = partial(
|
2023-02-13 11:42:42 +00:00
|
|
|
|
m2m_changed.send, sender=m2m_model, reverse=reverse,
|
|
|
|
|
model=User, using=model.objects.db
|
2020-12-14 12:31:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for obj in objects:
|
|
|
|
|
send_m2m_change_signal(
|
|
|
|
|
instance=obj,
|
2021-03-08 02:08:51 +00:00
|
|
|
|
pk_set=object_id_user_ids_map[obj.id],
|
2020-12-14 12:31:12 +00:00
|
|
|
|
action=PRE_REMOVE
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
relations.delete()
|
|
|
|
|
|
|
|
|
|
for obj in objects:
|
|
|
|
|
send_m2m_change_signal(
|
|
|
|
|
instance=obj,
|
2021-03-08 02:08:51 +00:00
|
|
|
|
pk_set=object_id_user_ids_map[obj.id],
|
2020-12-14 12:31:12 +00:00
|
|
|
|
action=POST_REMOVE
|
|
|
|
|
)
|
2020-07-20 02:42:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _clear_users_from_org(org, users):
|
2020-08-20 07:19:06 +00:00
|
|
|
|
"""
|
2023-02-13 11:42:42 +00:00
|
|
|
|
清理用户在该组织下的相关数据, 包括用户组, 资产授权, 用户授权
|
2020-08-20 07:19:06 +00:00
|
|
|
|
"""
|
2020-07-20 02:42:22 +00:00
|
|
|
|
if not users:
|
|
|
|
|
return
|
|
|
|
|
|
2022-08-17 03:54:18 +00:00
|
|
|
|
models = (AssetPermission, UserGroup)
|
2020-08-20 07:19:06 +00:00
|
|
|
|
|
|
|
|
|
for m in models:
|
2023-02-13 11:42:42 +00:00
|
|
|
|
_remove_user_resource(m, users, org)
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_user_leave_org)
|
|
|
|
|
def on_user_leave_org(sender, user=None, org=None, **kwargs):
|
|
|
|
|
logger.debug('User leave org signal recv: {} <> {}'.format(user, org))
|
|
|
|
|
_clear_users_from_org(org, [user])
|