mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-05 09:21:02 +00:00
Fix rbac (#7728)
* perf: 重命名 signal handlers * fix: 修复 ticket processor 问题 * perf: 修改 ticket 处理人api * fix: 修复创建系统账号bug * fix: 升级celery_beat==2.2.1和flower==1.0.0;修改celery进程启动参数先后顺序 * perf: 修改 authentication token * fix: 修复上传权限bug * fix: 登录页面增加i18n切换; * fix: 系统角色删除限制 * perf: 修改一下 permissions tree * perf: 生成 i18n * perf: 修改一点点 Co-authored-by: ibuler <ibuler@qq.com> Co-authored-by: feng626 <1304903146@qq.com> Co-authored-by: Jiangjie.Bai <bugatti_it@163.com>
This commit is contained in:
95
apps/notifications/signal_handlers.py
Normal file
95
apps/notifications/signal_handlers.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import json
|
||||
from importlib import import_module
|
||||
import inspect
|
||||
|
||||
from django.utils.functional import LazyObject
|
||||
from django.db.models.signals import post_save
|
||||
from django.db.models.signals import post_migrate
|
||||
from django.dispatch import receiver
|
||||
from django.apps import AppConfig
|
||||
|
||||
from notifications.backends import BACKEND
|
||||
from users.models import User
|
||||
from common.utils.connection import RedisPubSub
|
||||
from common.utils import get_logger
|
||||
from common.decorator import on_transaction_commit
|
||||
from .models import SiteMessage, SystemMsgSubscription, UserMsgSubscription
|
||||
from .notifications import SystemMessage
|
||||
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def new_site_msg_pub_sub():
|
||||
return RedisPubSub('notifications.SiteMessageCome')
|
||||
|
||||
|
||||
class NewSiteMsgSubPub(LazyObject):
|
||||
def _setup(self):
|
||||
self._wrapped = new_site_msg_pub_sub()
|
||||
|
||||
|
||||
new_site_msg_chan = NewSiteMsgSubPub()
|
||||
|
||||
|
||||
@receiver(post_save, sender=SiteMessage)
|
||||
@on_transaction_commit
|
||||
def on_site_message_create(sender, instance, created, **kwargs):
|
||||
if not created:
|
||||
return
|
||||
logger.debug('New site msg created, publish it')
|
||||
user_ids = instance.users.all().values_list('id', flat=True)
|
||||
user_ids = [str(i) for i in user_ids]
|
||||
data = {
|
||||
'id': str(instance.id),
|
||||
'subject': instance.subject,
|
||||
'message': instance.message,
|
||||
'users': user_ids
|
||||
}
|
||||
new_site_msg_chan.publish(data)
|
||||
|
||||
|
||||
@receiver(post_migrate, dispatch_uid='notifications.signals_handler.create_system_messages')
|
||||
def create_system_messages(app_config: AppConfig, **kwargs):
|
||||
try:
|
||||
notifications_module = import_module('.notifications', app_config.module.__package__)
|
||||
|
||||
for name, obj in notifications_module.__dict__.items():
|
||||
if name.startswith('_'):
|
||||
continue
|
||||
|
||||
if not inspect.isclass(obj):
|
||||
continue
|
||||
|
||||
if not issubclass(obj, SystemMessage):
|
||||
continue
|
||||
|
||||
attrs = obj.__dict__
|
||||
if 'message_type_label' not in attrs:
|
||||
continue
|
||||
|
||||
if 'category' not in attrs:
|
||||
continue
|
||||
|
||||
if 'category_label' not in attrs:
|
||||
continue
|
||||
|
||||
message_type = obj.get_message_type()
|
||||
sub, created = SystemMsgSubscription.objects.get_or_create(message_type=message_type)
|
||||
if created:
|
||||
obj.post_insert_to_db(sub)
|
||||
logger.info(f'Create SystemMsgSubscription: package={app_config.module.__package__} type={message_type}')
|
||||
except ModuleNotFoundError:
|
||||
pass
|
||||
|
||||
|
||||
@receiver(post_save, sender=User)
|
||||
def on_user_post_save(sender, instance, created, **kwargs):
|
||||
if not created:
|
||||
return
|
||||
receive_backends = []
|
||||
# Todo: IDE 识别不了 get_account
|
||||
for backend in BACKEND:
|
||||
if backend.get_account(instance):
|
||||
receive_backends.append(backend)
|
||||
UserMsgSubscription.objects.create(user=instance, receive_backends=receive_backends)
|
Reference in New Issue
Block a user