From dda367a9568cad23e6a243eba3b3b8a95c6b2353 Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 19 Nov 2018 12:18:52 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E4=BF=AE=E6=94=B9common=20settings?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/forms.py | 13 +++---- apps/common/models.py | 8 +++- apps/common/utils.py | 59 +++++++----------------------- apps/common/views.py | 6 +-- apps/jumpserver/settings.py | 8 ++-- apps/terminal/backends/__init__.py | 2 +- apps/terminal/forms.py | 4 +- apps/terminal/models.py | 3 +- apps/users/utils.py | 12 ++---- 9 files changed, 39 insertions(+), 76 deletions(-) diff --git a/apps/common/forms.py b/apps/common/forms.py index 10ac4ec17..4fb6de03f 100644 --- a/apps/common/forms.py +++ b/apps/common/forms.py @@ -4,11 +4,10 @@ import json from django import forms from django.utils.translation import ugettext_lazy as _ from django.db import transaction -from django.conf import settings from .models import Setting, common_settings from .fields import FormDictField, FormEncryptCharField, \ - FormEncryptMixin, FormEncryptDictField + FormEncryptMixin class BaseForm(forms.Form): @@ -16,17 +15,17 @@ class BaseForm(forms.Form): super().__init__(*args, **kwargs) for name, field in self.fields.items(): db_value = getattr(common_settings, name) - django_value = getattr(settings, name) if hasattr(settings, name) else None + # django_value = getattr(settings, name) if hasattr(settings, name) else None - if db_value is None and django_value is None: + if db_value is None: # and django_value is None: continue - if db_value is False or db_value: + if db_value is not None: if isinstance(db_value, dict): db_value = json.dumps(db_value) initial_value = db_value - elif django_value is False or django_value: - initial_value = django_value + # elif django_value is False or django_value: + # initial_value = django_value else: initial_value = '' field.initial = initial_value diff --git a/apps/common/models.py b/apps/common/models.py index 812d491a9..a1ae9b9ad 100644 --- a/apps/common/models.py +++ b/apps/common/models.py @@ -40,11 +40,15 @@ class Setting(models.Model): return self.name def __getattr__(self, item): - instances = self.__class__.objects.filter(name=item) + default = getattr(settings, item, None) + try: + instances = self.__class__.objects.filter(name=item) + except Exception: + return default if len(instances) == 1: return instances[0].cleaned_value else: - return None + return default @property def cleaned_value(self): diff --git a/apps/common/utils.py b/apps/common/utils.py index bb4f0132c..74baec06b 100644 --- a/apps/common/utils.py +++ b/apps/common/utils.py @@ -38,7 +38,7 @@ def reverse(view_name, urlconf=None, args=None, kwargs=None, if external: from common.models import common_settings - site_url = common_settings.SITE_URL or settings.SITE_URL + site_url = common_settings.SITE_URL url = site_url.strip('/') + url return url @@ -389,53 +389,20 @@ def get_request_ip(request): return login_ip -def get_command_storage_or_create_default_storage(): - from common.models import common_settings, Setting - name = 'TERMINAL_COMMAND_STORAGE' - default = {'default': {'TYPE': 'server'}} - try: - command_storage = common_settings.TERMINAL_COMMAND_STORAGE - except Exception: - return default - if command_storage is None: - obj = Setting() - obj.name = name - obj.encrypted = True - obj.cleaned_value = default - obj.save() - if isinstance(command_storage, dict) and not command_storage: - obj = Setting.objects.get(name=name) - value = obj.cleaned_value - value.update(default) - obj.cleaned_value = value - obj.save() - command_storage = common_settings.TERMINAL_COMMAND_STORAGE - return command_storage +def get_command_storage_setting(): + from common.models import common_settings + default = settings.TERMINAL_COMMAND_STORAGE + value = common_settings.TERMINAL_COMMAND_STORAGE + value.update(default) + return value -def get_replay_storage_or_create_default_storage(): - from common.models import common_settings, Setting - name = 'TERMINAL_REPLAY_STORAGE' - default = {'default': {'TYPE': 'server'}} - try: - replay_storage = common_settings.TERMINAL_REPLAY_STORAGE - except Exception: - return default - if replay_storage is None: - obj = Setting() - obj.name = name - obj.encrypted = True - obj.cleaned_value = default - obj.save() - replay_storage = common_settings.TERMINAL_REPLAY_STORAGE - if isinstance(replay_storage, dict) and not replay_storage: - obj = Setting.objects.get(name=name) - value = obj.cleaned_value - value.update(default) - obj.cleaned_value = value - obj.save() - replay_storage = common_settings.TERMINAL_REPLAY_STORAGE - return replay_storage +def get_replay_storage_setting(): + from common.models import common_settings + default = settings.TERMINAL_REPLAY_STORAGE + value = common_settings.TERMINAL_REPLAY_STORAGE + value.update(default) + return value class TeeObj: diff --git a/apps/common/views.py b/apps/common/views.py index 04d844c04..80f4c3b2b 100644 --- a/apps/common/views.py +++ b/apps/common/views.py @@ -2,9 +2,7 @@ from django.views.generic import TemplateView from django.shortcuts import render, redirect from django.contrib import messages from django.utils.translation import ugettext as _ -from django.conf import settings -from common.models import common_settings from .forms import EmailSettingForm, LDAPSettingForm, BasicSettingForm, \ TerminalSettingForm, SecuritySettingForm from common.permissions import SuperUserRequiredMixin @@ -97,8 +95,8 @@ class TerminalSettingView(SuperUserRequiredMixin, TemplateView): template_name = "common/terminal_setting.html" def get_context_data(self, **kwargs): - command_storage = utils.get_command_storage_or_create_default_storage() - replay_storage = utils.get_replay_storage_or_create_default_storage() + command_storage = utils.get_command_storage_setting() + replay_storage = utils.get_replay_storage_setting() context = { 'app': _('Settings'), diff --git a/apps/jumpserver/settings.py b/apps/jumpserver/settings.py index 212878386..4f469eed3 100644 --- a/apps/jumpserver/settings.py +++ b/apps/jumpserver/settings.py @@ -471,10 +471,10 @@ TERMINAL_REPLAY_STORAGE = { } -DEFAULT_PASSWORD_MIN_LENGTH = 6 -DEFAULT_LOGIN_LIMIT_COUNT = 7 -DEFAULT_LOGIN_LIMIT_TIME = 30 # Unit: minute -DEFAULT_SECURITY_MAX_IDLE_TIME = 30 # Unit: minute +SECURITY_PASSWORD_MIN_LENGTH = 6 +SECURITY_LOGIN_LIMIT_COUNT = 7 +SECURITY_LOGIN_LIMIT_TIME = 30 # Unit: minute +SECURITY_MAX_IDLE_TIME = 30 # Unit: minute # Django bootstrap3 setting, more see http://django-bootstrap3.readthedocs.io/en/latest/settings.html BOOTSTRAP3 = { diff --git a/apps/terminal/backends/__init__.py b/apps/terminal/backends/__init__.py index 0f0ec0cc5..12b85b7e3 100644 --- a/apps/terminal/backends/__init__.py +++ b/apps/terminal/backends/__init__.py @@ -18,7 +18,7 @@ def get_command_storage(): def get_terminal_command_storages(): storage_list = {} - command_storage = utils.get_command_storage_or_create_default_storage() + command_storage = utils.get_command_storage_setting() for name, params in command_storage.items(): tp = params['TYPE'] diff --git a/apps/terminal/forms.py b/apps/terminal/forms.py index 893f15b12..207363ab0 100644 --- a/apps/terminal/forms.py +++ b/apps/terminal/forms.py @@ -9,14 +9,14 @@ from .models import Terminal def get_all_command_storage(): from common import utils - command_storage = utils.get_command_storage_or_create_default_storage() + command_storage = utils.get_command_storage_setting() for k, v in command_storage.items(): yield (k, k) def get_all_replay_storage(): from common import utils - replay_storage = utils.get_replay_storage_or_create_default_storage() + replay_storage = utils.get_replay_storage_setting() for k, v in replay_storage.items(): yield (k, k) diff --git a/apps/terminal/models.py b/apps/terminal/models.py index ab17853a2..5ab0071a5 100644 --- a/apps/terminal/models.py +++ b/apps/terminal/models.py @@ -64,8 +64,7 @@ class Terminal(models.Model): configs.update(self.get_common_storage()) configs.update(self.get_replay_storage()) configs.update({ - 'SECURITY_MAX_IDLE_TIME': common_settings.SECURITY_MAX_IDLE_TIME or - settings.DEFAULT_SECURITY_MAX_IDLE_TIME, + 'SECURITY_MAX_IDLE_TIME': common_settings.SECURITY_MAX_IDLE_TIME }) return configs diff --git a/apps/users/utils.py b/apps/users/utils.py index 4d8e4c5a7..0098f1b99 100644 --- a/apps/users/utils.py +++ b/apps/users/utils.py @@ -307,8 +307,7 @@ def check_password_rules(password): lower_field_name = 'SECURITY_PASSWORD_LOWER_CASE' number_field_name = 'SECURITY_PASSWORD_NUMBER' special_field_name = 'SECURITY_PASSWORD_SPECIAL_CHAR' - min_length = getattr(common_settings, min_field_name) or \ - settings.DEFAULT_PASSWORD_MIN_LENGTH + min_length = getattr(common_settings, min_field_name) password_setting = Setting.objects.filter(name__startswith='SECURITY_PASSWORD') if not password_setting: @@ -340,8 +339,7 @@ def increase_login_failed_count(username, ip): count = cache.get(key_limit) count = count + 1 if count else 1 - limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME or \ - settings.DEFAULT_LOGIN_LIMIT_TIME + limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME cache.set(key_limit, count, int(limit_time)*60) @@ -357,10 +355,8 @@ def is_block_login(username, ip): key_block = key_prefix_block.format(username) count = cache.get(key_limit, 0) - limit_count = common_settings.SECURITY_LOGIN_LIMIT_COUNT or \ - settings.DEFAULT_LOGIN_LIMIT_COUNT - limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME or \ - settings.DEFAULT_LOGIN_LIMIT_TIME + limit_count = common_settings.SECURITY_LOGIN_LIMIT_COUNT + limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME if count >= limit_count: cache.set(key_block, 1, int(limit_time)*60)