2018-08-13 10:26:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2021-03-03 03:20:40 +00:00
|
|
|
import threading
|
2020-12-14 12:31:12 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
from functools import partial
|
2018-08-13 10:26:13 +00:00
|
|
|
|
|
|
|
from django.dispatch import receiver
|
2021-03-03 03:20:40 +00:00
|
|
|
from django.utils.functional import LazyObject
|
|
|
|
from django.db.models.signals import m2m_changed
|
2021-03-15 09:46:43 +00:00
|
|
|
from django.db.models.signals import post_save, post_delete, pre_delete
|
2018-08-13 10:26:13 +00:00
|
|
|
|
2020-08-20 07:19:06 +00:00
|
|
|
from orgs.utils import tmp_to_org
|
2021-03-02 11:45:44 +00:00
|
|
|
from orgs.models import Organization, OrganizationMember
|
|
|
|
from orgs.hands import set_current_org, Node, get_current_org
|
2020-12-14 12:31:12 +00:00
|
|
|
from perms.models import (AssetPermission, ApplicationPermission)
|
|
|
|
from users.models import UserGroup, User
|
2021-03-02 11:45:44 +00:00
|
|
|
from common.const.signals import PRE_REMOVE, POST_REMOVE
|
2021-06-21 10:36:38 +00:00
|
|
|
from common.decorator 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
|
2021-09-15 08:57:45 +00:00
|
|
|
from assets.models import CommandFilterRule
|
2021-03-03 03:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
def get_orgs_mapping_for_memory_pub_sub():
|
|
|
|
return RedisPubSub('fm.orgs_mapping')
|
|
|
|
|
|
|
|
|
|
|
|
class OrgsMappingForMemoryPubSub(LazyObject):
|
|
|
|
def _setup(self):
|
|
|
|
self._wrapped = get_orgs_mapping_for_memory_pub_sub()
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
def keep_subscribe():
|
2021-04-12 09:16:30 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
subscribe = orgs_mapping_for_memory_pub_sub.subscribe()
|
|
|
|
for message in subscribe.listen():
|
|
|
|
if message['type'] != 'message':
|
|
|
|
continue
|
|
|
|
if message['data'] == b'error':
|
|
|
|
raise ValueError
|
|
|
|
Organization.expire_orgs_mapping()
|
2021-07-08 06:23:18 +00:00
|
|
|
logger.debug('Expire orgs mapping: ' + str(message['data']))
|
2021-04-12 09:16:30 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.exception(f'subscribe_orgs_mapping_expire: {e}')
|
|
|
|
Organization.expire_orgs_mapping()
|
2021-03-03 03:20:40 +00:00
|
|
|
|
|
|
|
t = threading.Thread(target=keep_subscribe)
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
2018-08-13 10:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Organization)
|
2021-07-08 06:23:18 +00:00
|
|
|
def on_org_create_or_update(sender, instance, created=False, **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)
|
|
|
|
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)
|
|
|
|
def on_org_delete(sender, instance, **kwargs):
|
|
|
|
expire_orgs_mapping_for_memory(instance.id)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
|
2021-03-15 09:46:43 +00:00
|
|
|
@receiver(pre_delete, sender=Organization)
|
|
|
|
def on_org_delete(sender, instance, **kwargs):
|
|
|
|
# 删除该组织下所有 节点
|
|
|
|
with tmp_to_org(instance):
|
|
|
|
root_node = Node.org_root()
|
|
|
|
if root_node:
|
|
|
|
root_node.delete()
|
|
|
|
|
|
|
|
|
2021-09-15 08:57:45 +00:00
|
|
|
def _remove_users(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)):
|
|
|
|
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(
|
|
|
|
m2m_changed.send,
|
|
|
|
sender=m2m_model, reverse=reverse, model=User, using=model.objects.db
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
清理用户在该组织下的相关数据
|
|
|
|
"""
|
2020-07-20 02:42:22 +00:00
|
|
|
if not users:
|
|
|
|
return
|
|
|
|
|
2020-12-14 12:31:12 +00:00
|
|
|
models = (AssetPermission, ApplicationPermission, UserGroup)
|
2020-08-20 07:19:06 +00:00
|
|
|
|
|
|
|
for m in models:
|
|
|
|
_remove_users(m, users, org)
|
2019-07-02 14:08:50 +00:00
|
|
|
|
2021-09-15 08:57:45 +00:00
|
|
|
_remove_users(CommandFilterRule, users, org, user_field_name='reviewers')
|
|
|
|
|
2019-07-02 14:08:50 +00:00
|
|
|
|
2020-07-20 02:42:22 +00:00
|
|
|
@receiver(m2m_changed, sender=OrganizationMember)
|
2020-08-20 07:19:06 +00:00
|
|
|
def on_org_user_changed(action, instance, reverse, pk_set, **kwargs):
|
2020-07-20 02:42:22 +00:00
|
|
|
if action == 'post_remove':
|
2020-08-20 07:19:06 +00:00
|
|
|
if reverse:
|
|
|
|
user = instance
|
|
|
|
org_pk_set = pk_set
|
|
|
|
|
|
|
|
orgs = Organization.objects.filter(id__in=org_pk_set)
|
|
|
|
for org in orgs:
|
|
|
|
if not org.members.filter(id=user.id).exists():
|
|
|
|
_clear_users_from_org(org, user)
|
|
|
|
else:
|
|
|
|
org = instance
|
|
|
|
user_pk_set = pk_set
|
|
|
|
|
|
|
|
leaved_users = set(pk_set) - set(org.members.filter(id__in=user_pk_set).values_list('id', flat=True))
|
|
|
|
_clear_users_from_org(org, leaved_users)
|
2021-06-21 10:36:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=User)
|
|
|
|
@on_transaction_commit
|
|
|
|
def on_user_created_set_default_org(sender, instance, created, **kwargs):
|
|
|
|
if not created:
|
|
|
|
return
|
|
|
|
if instance.orgs.count() > 0:
|
|
|
|
return
|
|
|
|
Organization.default().members.add(instance)
|