2018-01-11 12:10:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-01-10 03:50:08 +00:00
|
|
|
import json
|
|
|
|
|
2022-03-25 06:45:08 +00:00
|
|
|
from django.conf import LazySettings
|
2023-02-03 04:22:27 +00:00
|
|
|
from django.db.models.signals import post_save
|
2022-03-25 06:45:08 +00:00
|
|
|
from django.db.utils import ProgrammingError, OperationalError
|
2018-01-11 12:10:27 +00:00
|
|
|
from django.dispatch import receiver
|
2021-01-26 09:54:12 +00:00
|
|
|
from django.utils.functional import LazyObject
|
2018-01-11 12:10:27 +00:00
|
|
|
|
2023-02-08 02:14:09 +00:00
|
|
|
from common.decorators import on_transaction_commit
|
2022-11-17 05:44:50 +00:00
|
|
|
from common.signals import django_ready
|
2019-02-26 04:38:20 +00:00
|
|
|
from common.utils import get_logger, ssh_key_gen
|
2021-01-26 09:54:12 +00:00
|
|
|
from common.utils.connection import RedisPubSub
|
2018-01-11 12:10:27 +00:00
|
|
|
from .models import Setting
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2021-01-26 09:54:12 +00:00
|
|
|
class SettingSubPub(LazyObject):
|
|
|
|
def _setup(self):
|
2022-12-07 07:09:01 +00:00
|
|
|
self._wrapped = RedisPubSub('settings')
|
2021-01-26 09:54:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
setting_pub_sub = SettingSubPub()
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Setting)
|
|
|
|
@on_transaction_commit
|
2018-01-11 12:10:27 +00:00
|
|
|
def refresh_settings_on_changed(sender, instance=None, **kwargs):
|
2021-09-14 06:46:13 +00:00
|
|
|
if not instance:
|
|
|
|
return
|
|
|
|
setting_pub_sub.publish(instance.name)
|
2022-12-09 05:05:34 +00:00
|
|
|
if instance.is_name('PERM_SINGLE_ASSET_TO_UNGROUP_NODE'):
|
|
|
|
""" 过期所有用户授权树 """
|
|
|
|
logger.debug('Expire all user perm tree')
|
2022-12-09 06:03:00 +00:00
|
|
|
from perms.utils import UserPermTreeExpireUtil
|
2022-12-09 05:05:34 +00:00
|
|
|
UserPermTreeExpireUtil().expire_perm_tree_for_all_user()
|
2021-09-14 06:36:36 +00:00
|
|
|
|
2018-01-11 12:10:27 +00:00
|
|
|
|
2019-09-17 04:34:47 +00:00
|
|
|
@receiver(django_ready)
|
2019-12-05 07:09:25 +00:00
|
|
|
def on_django_ready_add_db_config(sender, **kwargs):
|
2021-01-26 09:54:12 +00:00
|
|
|
Setting.refresh_all_settings()
|
2018-01-12 07:43:26 +00:00
|
|
|
|
|
|
|
|
2019-01-10 03:50:08 +00:00
|
|
|
@receiver(django_ready)
|
|
|
|
def auto_generate_terminal_host_key(sender, **kwargs):
|
2019-01-15 02:23:30 +00:00
|
|
|
try:
|
|
|
|
if Setting.objects.filter(name='TERMINAL_HOST_KEY').exists():
|
|
|
|
return
|
2019-01-29 06:07:46 +00:00
|
|
|
private_key, public_key = ssh_key_gen()
|
|
|
|
value = json.dumps(private_key)
|
|
|
|
Setting.objects.create(name='TERMINAL_HOST_KEY', value=value)
|
2019-01-29 04:27:51 +00:00
|
|
|
except:
|
2019-01-29 06:07:46 +00:00
|
|
|
pass
|
2019-01-10 03:50:08 +00:00
|
|
|
|
|
|
|
|
2021-01-26 09:54:12 +00:00
|
|
|
@receiver(django_ready)
|
|
|
|
def subscribe_settings_change(sender, **kwargs):
|
|
|
|
logger.debug("Start subscribe setting change")
|
|
|
|
|
2022-11-17 05:44:50 +00:00
|
|
|
setting_pub_sub.subscribe(lambda name: Setting.refresh_item(name))
|
2022-03-25 06:45:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(django_ready)
|
|
|
|
def monkey_patch_settings(sender, **kwargs):
|
|
|
|
def monkey_patch_getattr(self, name):
|
|
|
|
val = getattr(self._wrapped, name)
|
2022-03-28 03:27:35 +00:00
|
|
|
# 只解析 defaults 中的 callable
|
2022-04-06 06:58:13 +00:00
|
|
|
if callable(val) and val.__module__.endswith('jumpserver.conf'):
|
2022-03-25 06:45:08 +00:00
|
|
|
val = val()
|
|
|
|
return val
|
|
|
|
|
|
|
|
try:
|
|
|
|
LazySettings.__getattr__ = monkey_patch_getattr
|
|
|
|
except (ProgrammingError, OperationalError):
|
|
|
|
pass
|