2021-01-20 10:33:38 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from common.utils import validate_ssh_public_key
|
2023-01-30 05:43:27 +00:00
|
|
|
from common.serializers.fields import EncryptedField, LabeledChoiceField
|
2021-01-20 10:33:38 +00:00
|
|
|
from ..models import User
|
|
|
|
|
|
|
|
from .user import UserSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class UserOrgSerializer(serializers.Serializer):
|
|
|
|
id = serializers.CharField()
|
|
|
|
name = serializers.CharField()
|
2021-03-15 06:53:19 +00:00
|
|
|
is_default = serializers.BooleanField(read_only=True)
|
|
|
|
is_root = serializers.BooleanField(read_only=True)
|
2021-01-20 10:33:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserUpdatePasswordSerializer(serializers.ModelSerializer):
|
2022-05-20 02:01:41 +00:00
|
|
|
old_password = EncryptedField(required=True, max_length=128)
|
|
|
|
new_password = EncryptedField(required=True, max_length=128)
|
|
|
|
new_password_again = EncryptedField(required=True, max_length=128)
|
2021-01-20 10:33:38 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['old_password', 'new_password', 'new_password_again']
|
|
|
|
|
|
|
|
def validate_old_password(self, value):
|
|
|
|
if not self.instance.check_password(value):
|
|
|
|
msg = _('The old password is incorrect')
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
return value
|
|
|
|
|
2021-04-28 09:03:20 +00:00
|
|
|
def validate_new_password(self, value):
|
2021-01-20 10:33:38 +00:00
|
|
|
from ..utils import check_password_rules
|
2021-08-17 08:01:27 +00:00
|
|
|
if not check_password_rules(value, is_org_admin=self.instance.is_org_admin):
|
2021-01-20 10:33:38 +00:00
|
|
|
msg = _('Password does not match security rules')
|
|
|
|
raise serializers.ValidationError(msg)
|
2021-04-28 09:03:20 +00:00
|
|
|
if self.instance.is_history_password(value):
|
|
|
|
limit_count = settings.OLD_PASSWORD_HISTORY_LIMIT_COUNT
|
|
|
|
msg = _('The new password cannot be the last {} passwords').format(limit_count)
|
|
|
|
raise serializers.ValidationError(msg)
|
2021-01-20 10:33:38 +00:00
|
|
|
return value
|
|
|
|
|
2022-05-07 08:20:12 +00:00
|
|
|
def validate(self, values):
|
|
|
|
new_password = values.get('new_password', '')
|
|
|
|
new_password_again = values.get('new_password_again', '')
|
|
|
|
if new_password != new_password_again:
|
2021-01-20 10:33:38 +00:00
|
|
|
msg = _('The newly set password is inconsistent')
|
2022-05-07 08:20:12 +00:00
|
|
|
raise serializers.ValidationError({'new_password_again': msg})
|
|
|
|
return values
|
2021-01-20 10:33:38 +00:00
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
new_password = self.validated_data.get('new_password')
|
|
|
|
instance.reset_password(new_password)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2021-12-03 07:50:04 +00:00
|
|
|
class UserUpdateSecretKeySerializer(serializers.ModelSerializer):
|
2022-05-20 02:01:41 +00:00
|
|
|
new_secret_key = EncryptedField(required=True, max_length=128)
|
|
|
|
new_secret_key_again = EncryptedField(required=True, max_length=128)
|
2021-12-03 07:50:04 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['new_secret_key', 'new_secret_key_again']
|
|
|
|
|
2022-05-20 02:01:41 +00:00
|
|
|
def validate(self, values):
|
|
|
|
new_secret_key = values.get('new_secret_key', '')
|
|
|
|
new_secret_key_again = values.get('new_secret_key_again', '')
|
|
|
|
if new_secret_key != new_secret_key_again:
|
2021-12-03 07:50:04 +00:00
|
|
|
msg = _('The newly set password is inconsistent')
|
2022-05-20 02:01:41 +00:00
|
|
|
raise serializers.ValidationError({'new_secret_key_again': msg})
|
|
|
|
return values
|
2021-12-03 07:50:04 +00:00
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
new_secret_key = self.validated_data.get('new_secret_key')
|
|
|
|
instance.secret_key = new_secret_key
|
|
|
|
instance.save()
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2021-01-20 10:33:38 +00:00
|
|
|
class UserUpdatePublicKeySerializer(serializers.ModelSerializer):
|
|
|
|
public_key_comment = serializers.CharField(
|
|
|
|
source='get_public_key_comment', required=False, read_only=True, max_length=128
|
|
|
|
)
|
|
|
|
public_key_hash_md5 = serializers.CharField(
|
|
|
|
source='get_public_key_hash_md5', required=False, read_only=True, max_length=128
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['public_key_comment', 'public_key_hash_md5', 'public_key']
|
|
|
|
extra_kwargs = {
|
|
|
|
'public_key': {'required': True, 'write_only': True, 'max_length': 2048}
|
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def validate_public_key(value):
|
|
|
|
if not validate_ssh_public_key(value):
|
|
|
|
raise serializers.ValidationError(_('Not a valid ssh public key'))
|
|
|
|
return value
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
new_public_key = self.validated_data.get('public_key')
|
|
|
|
instance.set_public_key(new_public_key)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
class UserRoleSerializer(serializers.Serializer):
|
|
|
|
name = serializers.CharField(max_length=24)
|
|
|
|
display = serializers.CharField(max_length=64)
|
|
|
|
|
|
|
|
|
|
|
|
class UserProfileSerializer(UserSerializer):
|
2022-02-17 12:13:31 +00:00
|
|
|
MFA_LEVEL_CHOICES = (
|
|
|
|
(0, _('Disable')),
|
|
|
|
(1, _('Enable')),
|
|
|
|
)
|
|
|
|
|
2021-01-20 10:33:38 +00:00
|
|
|
public_key_comment = serializers.CharField(
|
|
|
|
source='get_public_key_comment', required=False, read_only=True, max_length=128
|
|
|
|
)
|
|
|
|
public_key_hash_md5 = serializers.CharField(
|
|
|
|
source='get_public_key_hash_md5', required=False, read_only=True, max_length=128
|
|
|
|
)
|
2023-01-30 05:43:27 +00:00
|
|
|
mfa_level = LabeledChoiceField(choices=MFA_LEVEL_CHOICES, label=_("MFA"), required=False)
|
2021-01-20 10:33:38 +00:00
|
|
|
guide_url = serializers.SerializerMethodField()
|
2021-09-13 03:29:03 +00:00
|
|
|
receive_backends = serializers.ListField(child=serializers.CharField(), read_only=True)
|
2022-04-18 09:17:23 +00:00
|
|
|
console_orgs = UserOrgSerializer(many=True, read_only=True)
|
|
|
|
audit_orgs = UserOrgSerializer(many=True, read_only=True)
|
|
|
|
workbench_orgs = UserOrgSerializer(many=True, read_only=True)
|
2022-02-17 12:13:31 +00:00
|
|
|
perms = serializers.ListField(label=_("Perms"), read_only=True)
|
2021-01-20 10:33:38 +00:00
|
|
|
|
|
|
|
class Meta(UserSerializer.Meta):
|
|
|
|
read_only_fields = [
|
2022-02-17 12:13:31 +00:00
|
|
|
'date_joined', 'last_login', 'created_by', 'source',
|
2022-04-18 09:17:23 +00:00
|
|
|
'console_orgs', 'audit_orgs', 'workbench_orgs',
|
|
|
|
'receive_backends', 'perms',
|
2021-01-20 10:33:38 +00:00
|
|
|
]
|
2022-02-17 12:13:31 +00:00
|
|
|
fields = UserSerializer.Meta.fields + [
|
|
|
|
'public_key_comment', 'public_key_hash_md5', 'guide_url',
|
|
|
|
] + read_only_fields
|
|
|
|
|
2021-01-20 10:33:38 +00:00
|
|
|
extra_kwargs = dict(UserSerializer.Meta.extra_kwargs)
|
|
|
|
extra_kwargs.update({
|
|
|
|
'name': {'read_only': True, 'max_length': 128},
|
|
|
|
'username': {'read_only': True, 'max_length': 128},
|
|
|
|
'email': {'read_only': True},
|
|
|
|
'is_first_login': {'label': _('Is first login'), 'read_only': False},
|
|
|
|
'source': {'read_only': True},
|
|
|
|
'is_valid': {'read_only': True},
|
|
|
|
'is_active': {'read_only': True},
|
|
|
|
'groups': {'read_only': True},
|
|
|
|
'password_strategy': {'read_only': True},
|
|
|
|
'date_expired': {'read_only': True},
|
|
|
|
'date_joined': {'read_only': True},
|
|
|
|
'last_login': {'read_only': True},
|
|
|
|
})
|
|
|
|
|
|
|
|
if 'password' in fields:
|
|
|
|
fields.remove('password')
|
|
|
|
extra_kwargs.pop('password', None)
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
system_roles_field = self.fields.get('system_roles')
|
|
|
|
org_roles_field = self.fields.get('org_roles')
|
|
|
|
system_roles_field.read_only = True
|
|
|
|
org_roles_field.read_only = True
|
|
|
|
|
2021-01-20 10:33:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_guide_url(obj):
|
|
|
|
return settings.USER_GUIDE_URL
|
|
|
|
|
|
|
|
def validate_mfa_level(self, mfa_level):
|
|
|
|
if self.instance and self.instance.mfa_force_enabled:
|
|
|
|
return 2
|
|
|
|
return mfa_level
|
|
|
|
|
|
|
|
def validate_public_key(self, public_key):
|
|
|
|
if self.instance and self.instance.can_update_ssh_key():
|
|
|
|
if not validate_ssh_public_key(public_key):
|
|
|
|
raise serializers.ValidationError(_('Not a valid ssh public key'))
|
|
|
|
return public_key
|
|
|
|
return None
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
def validate_password(self, password):
|
|
|
|
from rbac.models import Role
|
|
|
|
from ..utils import check_password_rules
|
|
|
|
if not self.instance:
|
|
|
|
return password
|
|
|
|
|
|
|
|
is_org_admin = self.instance.org_roles.filter(
|
|
|
|
name=Role.BuiltinRole.org_admin.name
|
|
|
|
).exsits()
|
|
|
|
if not check_password_rules(password, is_org_admin=is_org_admin):
|
|
|
|
msg = _('Password does not match security rules')
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
return password
|
|
|
|
|
2021-01-20 10:33:38 +00:00
|
|
|
|
|
|
|
class UserPKUpdateSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['id', 'public_key']
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def validate_public_key(value):
|
|
|
|
if not validate_ssh_public_key(value):
|
|
|
|
raise serializers.ValidationError(_('Not a valid ssh public key'))
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
class ChangeUserPasswordSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['password']
|
|
|
|
|
2021-04-25 10:13:41 +00:00
|
|
|
|
2021-01-20 10:33:38 +00:00
|
|
|
class ResetOTPSerializer(serializers.Serializer):
|
|
|
|
msg = serializers.CharField(read_only=True)
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
pass
|