[Update] 修改common settings配置

pull/2067/head
ibuler 6 years ago
parent c9aab608a9
commit dda367a956

@ -4,11 +4,10 @@ import json
from django import forms from django import forms
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.db import transaction from django.db import transaction
from django.conf import settings
from .models import Setting, common_settings from .models import Setting, common_settings
from .fields import FormDictField, FormEncryptCharField, \ from .fields import FormDictField, FormEncryptCharField, \
FormEncryptMixin, FormEncryptDictField FormEncryptMixin
class BaseForm(forms.Form): class BaseForm(forms.Form):
@ -16,17 +15,17 @@ class BaseForm(forms.Form):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
for name, field in self.fields.items(): for name, field in self.fields.items():
db_value = getattr(common_settings, name) 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 continue
if db_value is False or db_value: if db_value is not None:
if isinstance(db_value, dict): if isinstance(db_value, dict):
db_value = json.dumps(db_value) db_value = json.dumps(db_value)
initial_value = db_value initial_value = db_value
elif django_value is False or django_value: # elif django_value is False or django_value:
initial_value = django_value # initial_value = django_value
else: else:
initial_value = '' initial_value = ''
field.initial = initial_value field.initial = initial_value

@ -40,11 +40,15 @@ class Setting(models.Model):
return self.name return self.name
def __getattr__(self, item): def __getattr__(self, item):
default = getattr(settings, item, None)
try:
instances = self.__class__.objects.filter(name=item) instances = self.__class__.objects.filter(name=item)
except Exception:
return default
if len(instances) == 1: if len(instances) == 1:
return instances[0].cleaned_value return instances[0].cleaned_value
else: else:
return None return default
@property @property
def cleaned_value(self): def cleaned_value(self):

@ -38,7 +38,7 @@ def reverse(view_name, urlconf=None, args=None, kwargs=None,
if external: if external:
from common.models import common_settings 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 url = site_url.strip('/') + url
return url return url
@ -389,53 +389,20 @@ def get_request_ip(request):
return login_ip return login_ip
def get_command_storage_or_create_default_storage(): def get_command_storage_setting():
from common.models import common_settings, Setting from common.models import common_settings
name = 'TERMINAL_COMMAND_STORAGE' default = settings.TERMINAL_COMMAND_STORAGE
default = {'default': {'TYPE': 'server'}} value = common_settings.TERMINAL_COMMAND_STORAGE
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) value.update(default)
obj.cleaned_value = value return value
obj.save()
command_storage = common_settings.TERMINAL_COMMAND_STORAGE
return command_storage
def get_replay_storage_or_create_default_storage(): def get_replay_storage_setting():
from common.models import common_settings, Setting from common.models import common_settings
name = 'TERMINAL_REPLAY_STORAGE' default = settings.TERMINAL_REPLAY_STORAGE
default = {'default': {'TYPE': 'server'}} value = common_settings.TERMINAL_REPLAY_STORAGE
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) value.update(default)
obj.cleaned_value = value return value
obj.save()
replay_storage = common_settings.TERMINAL_REPLAY_STORAGE
return replay_storage
class TeeObj: class TeeObj:

@ -2,9 +2,7 @@ from django.views.generic import TemplateView
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.contrib import messages from django.contrib import messages
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.conf import settings
from common.models import common_settings
from .forms import EmailSettingForm, LDAPSettingForm, BasicSettingForm, \ from .forms import EmailSettingForm, LDAPSettingForm, BasicSettingForm, \
TerminalSettingForm, SecuritySettingForm TerminalSettingForm, SecuritySettingForm
from common.permissions import SuperUserRequiredMixin from common.permissions import SuperUserRequiredMixin
@ -97,8 +95,8 @@ class TerminalSettingView(SuperUserRequiredMixin, TemplateView):
template_name = "common/terminal_setting.html" template_name = "common/terminal_setting.html"
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
command_storage = utils.get_command_storage_or_create_default_storage() command_storage = utils.get_command_storage_setting()
replay_storage = utils.get_replay_storage_or_create_default_storage() replay_storage = utils.get_replay_storage_setting()
context = { context = {
'app': _('Settings'), 'app': _('Settings'),

@ -471,10 +471,10 @@ TERMINAL_REPLAY_STORAGE = {
} }
DEFAULT_PASSWORD_MIN_LENGTH = 6 SECURITY_PASSWORD_MIN_LENGTH = 6
DEFAULT_LOGIN_LIMIT_COUNT = 7 SECURITY_LOGIN_LIMIT_COUNT = 7
DEFAULT_LOGIN_LIMIT_TIME = 30 # Unit: minute SECURITY_LOGIN_LIMIT_TIME = 30 # Unit: minute
DEFAULT_SECURITY_MAX_IDLE_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 # Django bootstrap3 setting, more see http://django-bootstrap3.readthedocs.io/en/latest/settings.html
BOOTSTRAP3 = { BOOTSTRAP3 = {

@ -18,7 +18,7 @@ def get_command_storage():
def get_terminal_command_storages(): def get_terminal_command_storages():
storage_list = {} 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(): for name, params in command_storage.items():
tp = params['TYPE'] tp = params['TYPE']

@ -9,14 +9,14 @@ from .models import Terminal
def get_all_command_storage(): def get_all_command_storage():
from common import utils 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(): for k, v in command_storage.items():
yield (k, k) yield (k, k)
def get_all_replay_storage(): def get_all_replay_storage():
from common import utils 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(): for k, v in replay_storage.items():
yield (k, k) yield (k, k)

@ -64,8 +64,7 @@ class Terminal(models.Model):
configs.update(self.get_common_storage()) configs.update(self.get_common_storage())
configs.update(self.get_replay_storage()) configs.update(self.get_replay_storage())
configs.update({ configs.update({
'SECURITY_MAX_IDLE_TIME': common_settings.SECURITY_MAX_IDLE_TIME or 'SECURITY_MAX_IDLE_TIME': common_settings.SECURITY_MAX_IDLE_TIME
settings.DEFAULT_SECURITY_MAX_IDLE_TIME,
}) })
return configs return configs

@ -307,8 +307,7 @@ def check_password_rules(password):
lower_field_name = 'SECURITY_PASSWORD_LOWER_CASE' lower_field_name = 'SECURITY_PASSWORD_LOWER_CASE'
number_field_name = 'SECURITY_PASSWORD_NUMBER' number_field_name = 'SECURITY_PASSWORD_NUMBER'
special_field_name = 'SECURITY_PASSWORD_SPECIAL_CHAR' special_field_name = 'SECURITY_PASSWORD_SPECIAL_CHAR'
min_length = getattr(common_settings, min_field_name) or \ min_length = getattr(common_settings, min_field_name)
settings.DEFAULT_PASSWORD_MIN_LENGTH
password_setting = Setting.objects.filter(name__startswith='SECURITY_PASSWORD') password_setting = Setting.objects.filter(name__startswith='SECURITY_PASSWORD')
if not password_setting: if not password_setting:
@ -340,8 +339,7 @@ def increase_login_failed_count(username, ip):
count = cache.get(key_limit) count = cache.get(key_limit)
count = count + 1 if count else 1 count = count + 1 if count else 1
limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME or \ limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME
settings.DEFAULT_LOGIN_LIMIT_TIME
cache.set(key_limit, count, int(limit_time)*60) 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) key_block = key_prefix_block.format(username)
count = cache.get(key_limit, 0) count = cache.get(key_limit, 0)
limit_count = common_settings.SECURITY_LOGIN_LIMIT_COUNT or \ limit_count = common_settings.SECURITY_LOGIN_LIMIT_COUNT
settings.DEFAULT_LOGIN_LIMIT_COUNT limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME
limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME or \
settings.DEFAULT_LOGIN_LIMIT_TIME
if count >= limit_count: if count >= limit_count:
cache.set(key_block, 1, int(limit_time)*60) cache.set(key_block, 1, int(limit_time)*60)

Loading…
Cancel
Save