2020-04-29 03:08:09 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
from ..models import Setting
|
|
|
|
|
2020-04-29 09:04:48 +00:00
|
|
|
__all__ = ['SettingsSerializer']
|
2020-04-29 03:08:09 +00:00
|
|
|
|
|
|
|
|
2020-04-29 09:04:48 +00:00
|
|
|
class BasicSettingSerializer(serializers.Serializer):
|
2020-04-29 03:08:09 +00:00
|
|
|
SITE_URL = serializers.URLField(required=True)
|
2020-04-29 09:04:48 +00:00
|
|
|
USER_GUIDE_URL = serializers.URLField(required=False, allow_blank=True, )
|
2020-04-29 03:08:09 +00:00
|
|
|
EMAIL_SUBJECT_PREFIX = serializers.CharField(max_length=1024, required=True)
|
|
|
|
|
|
|
|
|
2020-04-29 09:04:48 +00:00
|
|
|
class EmailSettingSerializer(serializers.Serializer):
|
2020-04-29 03:08:09 +00:00
|
|
|
encrypt_fields = ["EMAIL_HOST_PASSWORD", ]
|
|
|
|
|
|
|
|
EMAIL_HOST = serializers.CharField(max_length=1024, required=True)
|
|
|
|
EMAIL_PORT = serializers.CharField(max_length=5, required=True)
|
|
|
|
EMAIL_HOST_USER = serializers.CharField(max_length=128, required=True)
|
2020-04-29 10:10:42 +00:00
|
|
|
EMAIL_HOST_PASSWORD = serializers.CharField(max_length=1024, write_only=True, required=False, )
|
2020-04-29 09:04:48 +00:00
|
|
|
EMAIL_FROM = serializers.CharField(max_length=128, allow_blank=True, required=False)
|
|
|
|
EMAIL_RECIPIENT = serializers.CharField(max_length=128, allow_blank=True, required=False)
|
2020-04-29 03:08:09 +00:00
|
|
|
EMAIL_USE_SSL = serializers.BooleanField(required=False)
|
|
|
|
EMAIL_USE_TLS = serializers.BooleanField(required=False)
|
|
|
|
|
|
|
|
|
2020-04-29 09:04:48 +00:00
|
|
|
class EmailContentSettingSerializer(serializers.Serializer):
|
|
|
|
EMAIL_CUSTOM_USER_CREATED_SUBJECT = serializers.CharField(max_length=1024, allow_blank=True, required=False, )
|
|
|
|
EMAIL_CUSTOM_USER_CREATED_HONORIFIC = serializers.CharField(max_length=1024, allow_blank=True, required=False, )
|
|
|
|
EMAIL_CUSTOM_USER_CREATED_BODY = serializers.CharField(max_length=4096, allow_blank=True, required=False)
|
|
|
|
EMAIL_CUSTOM_USER_CREATED_SIGNATURE = serializers.CharField(max_length=512, allow_blank=True, required=False)
|
2020-04-29 03:08:09 +00:00
|
|
|
|
|
|
|
|
2020-04-29 09:04:48 +00:00
|
|
|
class LdapSettingSerializer(serializers.Serializer):
|
2020-04-29 03:08:09 +00:00
|
|
|
encrypt_fields = ["AUTH_LDAP_BIND_PASSWORD", ]
|
|
|
|
|
|
|
|
AUTH_LDAP_SERVER_URI = serializers.CharField(required=True)
|
|
|
|
AUTH_LDAP_BIND_DN = serializers.CharField(required=False)
|
2020-04-29 10:10:42 +00:00
|
|
|
AUTH_LDAP_BIND_PASSWORD = serializers.CharField(max_length=1024, write_only=True, required=False)
|
2020-04-29 09:04:48 +00:00
|
|
|
AUTH_LDAP_SEARCH_OU = serializers.CharField(max_length=1024, allow_blank=True, required=False)
|
2020-04-29 03:08:09 +00:00
|
|
|
AUTH_LDAP_SEARCH_FILTER = serializers.CharField(max_length=1024, required=True)
|
2020-05-08 08:48:36 +00:00
|
|
|
AUTH_LDAP_USER_ATTR_MAP = serializers.DictField(required=True)
|
2020-04-29 03:08:09 +00:00
|
|
|
AUTH_LDAP = serializers.BooleanField(required=False)
|
|
|
|
|
|
|
|
|
2020-04-29 09:04:48 +00:00
|
|
|
class TerminalSettingSerializer(serializers.Serializer):
|
2020-04-29 03:08:09 +00:00
|
|
|
SORT_BY_CHOICES = (
|
|
|
|
('hostname', _('Hostname')),
|
|
|
|
('ip', _('IP'))
|
|
|
|
)
|
|
|
|
|
|
|
|
PAGE_SIZE_CHOICES = (
|
|
|
|
('all', _('All')),
|
|
|
|
('auto', _('Auto')),
|
2020-06-03 06:06:44 +00:00
|
|
|
('10', '10'),
|
|
|
|
('15', '15'),
|
|
|
|
('25', '25'),
|
|
|
|
('50', '50'),
|
2020-04-29 03:08:09 +00:00
|
|
|
)
|
|
|
|
TERMINAL_PASSWORD_AUTH = serializers.BooleanField(required=False)
|
|
|
|
TERMINAL_PUBLIC_KEY_AUTH = serializers.BooleanField(required=False)
|
2020-12-21 06:24:53 +00:00
|
|
|
TERMINAL_HEARTBEAT_INTERVAL = serializers.IntegerField(min_value=5, max_value=99999, required=False)
|
2020-04-29 10:10:42 +00:00
|
|
|
TERMINAL_ASSET_LIST_SORT_BY = serializers.ChoiceField(SORT_BY_CHOICES, required=False)
|
|
|
|
TERMINAL_ASSET_LIST_PAGE_SIZE = serializers.ChoiceField(PAGE_SIZE_CHOICES, required=False)
|
2020-04-29 03:08:09 +00:00
|
|
|
TERMINAL_SESSION_KEEP_DURATION = serializers.IntegerField(min_value=1, max_value=99999, required=True)
|
2020-04-29 10:10:42 +00:00
|
|
|
TERMINAL_TELNET_REGEX = serializers.CharField(allow_blank=True, required=False)
|
2020-04-29 03:08:09 +00:00
|
|
|
|
|
|
|
|
2020-04-29 09:04:48 +00:00
|
|
|
class SecuritySettingSerializer(serializers.Serializer):
|
2020-04-29 03:08:09 +00:00
|
|
|
SECURITY_MFA_AUTH = serializers.BooleanField(required=False)
|
|
|
|
SECURITY_COMMAND_EXECUTION = serializers.BooleanField(required=False)
|
|
|
|
SECURITY_SERVICE_ACCOUNT_REGISTRATION = serializers.BooleanField(required=True)
|
|
|
|
SECURITY_LOGIN_LIMIT_COUNT = serializers.IntegerField(min_value=3, max_value=99999, required=True)
|
|
|
|
SECURITY_LOGIN_LIMIT_TIME = serializers.IntegerField(min_value=5, max_value=99999, required=True)
|
2020-05-07 08:57:57 +00:00
|
|
|
SECURITY_MAX_IDLE_TIME = serializers.IntegerField(min_value=1, max_value=99999, required=False)
|
2020-04-29 03:08:09 +00:00
|
|
|
SECURITY_PASSWORD_EXPIRATION_TIME = serializers.IntegerField(min_value=1, max_value=99999, required=True)
|
|
|
|
SECURITY_PASSWORD_MIN_LENGTH = serializers.IntegerField(min_value=6, max_value=30, required=True)
|
|
|
|
SECURITY_PASSWORD_UPPER_CASE = serializers.BooleanField(required=False)
|
|
|
|
SECURITY_PASSWORD_LOWER_CASE = serializers.BooleanField(required=False)
|
|
|
|
SECURITY_PASSWORD_NUMBER = serializers.BooleanField(required=False)
|
|
|
|
SECURITY_PASSWORD_SPECIAL_CHAR = serializers.BooleanField(required=False)
|
2020-10-27 03:35:31 +00:00
|
|
|
SECURITY_INSECURE_COMMAND = serializers.BooleanField(required=False)
|
2020-11-17 11:14:28 +00:00
|
|
|
SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER = serializers.CharField(max_length=8192, required=False, allow_blank=True)
|
2020-04-29 09:04:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SettingsSerializer(serializers.Serializer):
|
|
|
|
basic = BasicSettingSerializer(required=False)
|
|
|
|
email = EmailSettingSerializer(required=False)
|
|
|
|
email_content = EmailContentSettingSerializer(required=False)
|
|
|
|
ldap = LdapSettingSerializer(required=False)
|
|
|
|
terminal = TerminalSettingSerializer(required=False)
|
|
|
|
security = SecuritySettingSerializer(required=False)
|
|
|
|
|
|
|
|
encrypt_fields = ["EMAIL_HOST_PASSWORD", "AUTH_LDAP_BIND_PASSWORD"]
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
for category, category_data in validated_data.items():
|
|
|
|
if not category_data:
|
|
|
|
continue
|
|
|
|
self.update_validated_settings(category_data)
|
|
|
|
for field_name, field_value in category_data.items():
|
|
|
|
setattr(getattr(instance, category), field_name, field_value)
|
|
|
|
|
|
|
|
return instance
|
|
|
|
|
|
|
|
def update_validated_settings(self, validated_data, category='default'):
|
|
|
|
if not validated_data:
|
|
|
|
return
|
|
|
|
with transaction.atomic():
|
|
|
|
for field_name, field_value in validated_data.items():
|
|
|
|
try:
|
|
|
|
setting = Setting.objects.get(name=field_name)
|
|
|
|
except Setting.DoesNotExist:
|
|
|
|
setting = Setting()
|
|
|
|
encrypted = True if field_name in self.encrypt_fields else False
|
|
|
|
setting.name = field_name
|
|
|
|
setting.category = category
|
|
|
|
setting.encrypted = encrypted
|
|
|
|
setting.cleaned_value = field_value
|
2020-05-06 08:36:36 +00:00
|
|
|
setting.save()
|