2016-09-03 11:05:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-05-09 06:51:19 +00:00
|
|
|
from django.core.cache import cache
|
2020-06-03 10:20:33 +00:00
|
|
|
from django.conf import settings
|
2016-09-13 13:45:10 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-09-03 11:05:50 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
from common.utils import validate_ssh_public_key
|
2020-05-18 06:55:16 +00:00
|
|
|
from common.mixins import CommonBulkSerializerMixin
|
2019-09-12 10:56:26 +00:00
|
|
|
from common.permissions import CanUpdateDeleteUser
|
2020-08-07 05:53:17 +00:00
|
|
|
from orgs.models import ROLE as ORG_ROLE
|
2020-10-14 07:17:06 +00:00
|
|
|
from ..models import User
|
2016-09-03 11:05:50 +00:00
|
|
|
|
2019-07-04 08:44:46 +00:00
|
|
|
|
|
|
|
__all__ = [
|
2019-12-16 08:53:29 +00:00
|
|
|
'UserSerializer', 'UserPKUpdateSerializer',
|
2019-10-24 06:36:01 +00:00
|
|
|
'ChangeUserPasswordSerializer', 'ResetOTPSerializer',
|
2020-05-28 08:10:28 +00:00
|
|
|
'UserProfileSerializer', 'UserOrgSerializer',
|
2020-06-11 10:24:56 +00:00
|
|
|
'UserUpdatePasswordSerializer', 'UserUpdatePublicKeySerializer',
|
2020-10-14 07:17:06 +00:00
|
|
|
'UserRetrieveSerializer', 'MiniUserSerializer', 'InviteSerializer'
|
2019-07-04 08:44:46 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-03-20 10:21:27 +00:00
|
|
|
class UserOrgSerializer(serializers.Serializer):
|
|
|
|
id = serializers.CharField()
|
|
|
|
name = serializers.CharField()
|
|
|
|
|
|
|
|
|
2020-07-09 07:41:02 +00:00
|
|
|
class UserOrgLabelSerializer(serializers.Serializer):
|
|
|
|
value = serializers.CharField(source='id')
|
|
|
|
label = serializers.CharField(source='name')
|
|
|
|
|
|
|
|
|
2020-05-18 06:55:16 +00:00
|
|
|
class UserSerializer(CommonBulkSerializerMixin, serializers.ModelSerializer):
|
2020-04-09 02:33:20 +00:00
|
|
|
EMAIL_SET_PASSWORD = _('Reset link will be generated and sent to the user')
|
|
|
|
CUSTOM_PASSWORD = _('Set password')
|
|
|
|
PASSWORD_STRATEGY_CHOICES = (
|
|
|
|
(0, EMAIL_SET_PASSWORD),
|
|
|
|
(1, CUSTOM_PASSWORD)
|
|
|
|
)
|
|
|
|
password_strategy = serializers.ChoiceField(
|
2020-04-09 10:58:22 +00:00
|
|
|
choices=PASSWORD_STRATEGY_CHOICES, required=False, initial=0,
|
2020-04-09 02:33:20 +00:00
|
|
|
label=_('Password strategy'), write_only=True
|
|
|
|
)
|
2020-11-11 02:27:18 +00:00
|
|
|
mfa_level_display = serializers.ReadOnlyField(source='get_mfa_level_display', label=_('MFA level for display'))
|
|
|
|
login_blocked = serializers.SerializerMethodField(label=_('Login blocked'))
|
|
|
|
can_update = serializers.SerializerMethodField(label=_('Can update'))
|
|
|
|
can_delete = serializers.SerializerMethodField(label=_('Can delete'))
|
2020-08-19 09:38:21 +00:00
|
|
|
org_roles = serializers.ListField(label=_('Organization role name'), allow_null=True, required=False,
|
|
|
|
child=serializers.ChoiceField(choices=ORG_ROLE.choices))
|
2020-05-09 06:51:19 +00:00
|
|
|
key_prefix_block = "_LOGIN_BLOCK_{}"
|
|
|
|
|
2016-09-06 08:40:44 +00:00
|
|
|
class Meta:
|
|
|
|
model = User
|
2020-04-23 03:14:02 +00:00
|
|
|
# mini 是指能识别对象的最小单元
|
|
|
|
fields_mini = ['id', 'name', 'username']
|
|
|
|
# small 指的是 不需要计算的直接能从一张表中获取到的数据
|
|
|
|
fields_small = fields_mini + [
|
2020-05-27 09:27:28 +00:00
|
|
|
'password', 'email', 'public_key', 'wechat', 'phone', 'mfa_level', 'mfa_enabled',
|
2020-08-07 05:53:17 +00:00
|
|
|
'mfa_level_display', 'mfa_force_enabled', 'role_display', 'org_role_display',
|
2020-08-07 09:40:10 +00:00
|
|
|
'total_role_display', 'comment', 'source', 'is_valid', 'is_expired',
|
2019-05-21 08:24:01 +00:00
|
|
|
'is_active', 'created_by', 'is_first_login',
|
2020-04-10 06:46:35 +00:00
|
|
|
'password_strategy', 'date_password_last_updated', 'date_expired',
|
2020-04-30 08:58:08 +00:00
|
|
|
'avatar_url', 'source_display', 'date_joined', 'last_login'
|
2020-04-23 03:14:02 +00:00
|
|
|
]
|
|
|
|
fields = fields_small + [
|
|
|
|
'groups', 'role', 'groups_display', 'role_display',
|
2020-08-19 09:38:21 +00:00
|
|
|
'can_update', 'can_delete', 'login_blocked', 'org_roles'
|
2018-04-18 04:48:07 +00:00
|
|
|
]
|
2020-04-23 03:14:02 +00:00
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
extra_kwargs = {
|
2019-07-11 10:54:30 +00:00
|
|
|
'password': {'write_only': True, 'required': False, 'allow_null': True, 'allow_blank': True},
|
2019-06-25 06:32:25 +00:00
|
|
|
'public_key': {'write_only': True},
|
2019-05-21 08:24:01 +00:00
|
|
|
'is_first_login': {'label': _('Is first login'), 'read_only': True},
|
|
|
|
'is_valid': {'label': _('Is valid')},
|
|
|
|
'is_expired': {'label': _('Is expired')},
|
|
|
|
'avatar_url': {'label': _('Avatar url')},
|
2019-09-12 10:56:26 +00:00
|
|
|
'created_by': {'read_only': True, 'allow_blank': True},
|
2020-04-23 03:14:02 +00:00
|
|
|
'groups_display': {'label': _('Groups name')},
|
|
|
|
'source_display': {'label': _('Source name')},
|
2020-08-07 05:53:17 +00:00
|
|
|
'org_role_display': {'label': _('Organization role name')},
|
|
|
|
'role_display': {'label': _('Super role name')},
|
2020-11-11 02:27:18 +00:00
|
|
|
'total_role_display': {'label': _('Total role name')},
|
|
|
|
'mfa_enabled': {'label': _('MFA enabled')},
|
|
|
|
'mfa_force_enabled': {'label': _('MFA force enabled')},
|
2019-05-21 08:24:01 +00:00
|
|
|
}
|
2016-11-09 11:29:15 +00:00
|
|
|
|
2020-04-09 02:33:20 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.set_role_choices()
|
|
|
|
|
|
|
|
def set_role_choices(self):
|
2020-04-23 03:14:02 +00:00
|
|
|
role = self.fields.get('role')
|
|
|
|
if not role:
|
|
|
|
return
|
2020-04-09 10:58:22 +00:00
|
|
|
choices = role._choices
|
2020-07-20 02:42:22 +00:00
|
|
|
choices.pop(User.ROLE.APP, None)
|
2020-07-15 12:10:47 +00:00
|
|
|
request = self.context.get('request')
|
|
|
|
if request and hasattr(request, 'user') and not request.user.is_superuser:
|
2020-07-20 02:42:22 +00:00
|
|
|
choices.pop(User.ROLE.ADMIN, None)
|
|
|
|
choices.pop(User.ROLE.AUDITOR, None)
|
2020-04-09 10:58:22 +00:00
|
|
|
role._choices = choices
|
2020-04-09 02:33:20 +00:00
|
|
|
|
2019-07-02 06:17:56 +00:00
|
|
|
def validate_role(self, value):
|
|
|
|
request = self.context.get('request')
|
2020-07-20 02:42:22 +00:00
|
|
|
if not request.user.is_superuser and value != User.ROLE.USER:
|
|
|
|
role_display = User.ROLE.USER.label
|
2019-07-02 06:17:56 +00:00
|
|
|
msg = _("Role limit to {}".format(role_display))
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
return value
|
|
|
|
|
2019-07-11 10:54:30 +00:00
|
|
|
def validate_password(self, password):
|
2019-06-25 06:32:25 +00:00
|
|
|
from ..utils import check_password_rules
|
2019-07-11 10:54:30 +00:00
|
|
|
password_strategy = self.initial_data.get('password_strategy')
|
|
|
|
if password_strategy == '0':
|
|
|
|
return
|
|
|
|
if password_strategy is None and not password:
|
|
|
|
return
|
|
|
|
if not check_password_rules(password):
|
2019-06-25 06:32:25 +00:00
|
|
|
msg = _('Password does not match security rules')
|
|
|
|
raise serializers.ValidationError(msg)
|
2019-07-11 10:54:30 +00:00
|
|
|
return password
|
2019-06-25 06:32:25 +00:00
|
|
|
|
2019-09-12 10:56:26 +00:00
|
|
|
def validate_groups(self, groups):
|
2020-05-18 06:55:16 +00:00
|
|
|
"""
|
|
|
|
审计员不能加入到组中
|
|
|
|
"""
|
2019-09-12 10:56:26 +00:00
|
|
|
role = self.initial_data.get('role')
|
|
|
|
if self.instance:
|
|
|
|
role = role or self.instance.role
|
2020-07-20 02:42:22 +00:00
|
|
|
if role == User.ROLE.AUDITOR:
|
2019-09-12 10:56:26 +00:00
|
|
|
return []
|
|
|
|
return groups
|
|
|
|
|
2019-06-25 06:32:25 +00:00
|
|
|
@staticmethod
|
2019-09-12 10:56:26 +00:00
|
|
|
def change_password_to_raw(attrs):
|
|
|
|
password = attrs.pop('password', None)
|
2019-06-25 06:32:25 +00:00
|
|
|
if password:
|
2019-09-12 10:56:26 +00:00
|
|
|
attrs['password_raw'] = password
|
|
|
|
return attrs
|
2019-06-25 06:32:25 +00:00
|
|
|
|
2020-01-20 03:27:34 +00:00
|
|
|
@staticmethod
|
|
|
|
def clean_auth_fields(attrs):
|
|
|
|
for field in ('password', 'public_key'):
|
|
|
|
value = attrs.get(field)
|
|
|
|
if not value:
|
|
|
|
attrs.pop(field, None)
|
|
|
|
return attrs
|
|
|
|
|
2019-09-12 10:56:26 +00:00
|
|
|
def validate(self, attrs):
|
|
|
|
attrs = self.change_password_to_raw(attrs)
|
2020-01-20 03:27:34 +00:00
|
|
|
attrs = self.clean_auth_fields(attrs)
|
2020-04-09 02:33:20 +00:00
|
|
|
attrs.pop('password_strategy', None)
|
2019-09-12 10:56:26 +00:00
|
|
|
return attrs
|
2019-06-25 06:32:25 +00:00
|
|
|
|
2019-11-19 07:23:19 +00:00
|
|
|
def get_can_update(self, obj):
|
|
|
|
return CanUpdateDeleteUser.has_update_object_permission(
|
|
|
|
self.context['request'], self.context['view'], obj
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_can_delete(self, obj):
|
|
|
|
return CanUpdateDeleteUser.has_delete_object_permission(
|
|
|
|
self.context['request'], self.context['view'], obj
|
|
|
|
)
|
|
|
|
|
2020-05-09 06:51:19 +00:00
|
|
|
def get_login_blocked(self, obj):
|
|
|
|
key_block = self.key_prefix_block.format(obj.username)
|
|
|
|
blocked = bool(cache.get(key_block))
|
|
|
|
return blocked
|
|
|
|
|
2019-11-19 07:23:19 +00:00
|
|
|
|
2020-06-11 10:24:56 +00:00
|
|
|
class UserRetrieveSerializer(UserSerializer):
|
|
|
|
login_confirm_settings = serializers.PrimaryKeyRelatedField(read_only=True,
|
|
|
|
source='login_confirm_setting.reviewers', many=True)
|
|
|
|
|
|
|
|
class Meta(UserSerializer.Meta):
|
|
|
|
fields = UserSerializer.Meta.fields + ['login_confirm_settings']
|
|
|
|
|
|
|
|
|
2016-09-13 13:45:10 +00:00
|
|
|
class UserPKUpdateSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
2019-06-25 06:32:25 +00:00
|
|
|
fields = ['id', 'public_key']
|
2016-09-18 06:28:34 +00:00
|
|
|
|
2016-10-15 09:14:56 +00:00
|
|
|
@staticmethod
|
2019-06-25 06:32:25 +00:00
|
|
|
def validate_public_key(value):
|
2016-11-09 15:49:10 +00:00
|
|
|
if not validate_ssh_public_key(value):
|
2016-09-18 06:28:34 +00:00
|
|
|
raise serializers.ValidationError(_('Not a valid ssh public key'))
|
2016-09-13 13:45:10 +00:00
|
|
|
return value
|
2016-09-21 07:48:21 +00:00
|
|
|
|
|
|
|
|
2018-01-23 02:36:20 +00:00
|
|
|
class ChangeUserPasswordSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['password']
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ResetOTPSerializer(serializers.Serializer):
|
2019-09-12 10:56:26 +00:00
|
|
|
msg = serializers.CharField(read_only=True)
|
2019-10-24 06:36:01 +00:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
pass
|
2019-11-08 07:48:01 +00:00
|
|
|
|
|
|
|
|
2020-05-13 03:05:58 +00:00
|
|
|
class UserRoleSerializer(serializers.Serializer):
|
|
|
|
name = serializers.CharField(max_length=24)
|
|
|
|
display = serializers.CharField(max_length=64)
|
|
|
|
|
|
|
|
|
2020-05-12 09:57:35 +00:00
|
|
|
class UserProfileSerializer(UserSerializer):
|
2020-04-10 04:47:16 +00:00
|
|
|
admin_or_audit_orgs = UserOrgSerializer(many=True, read_only=True)
|
2020-07-09 07:41:02 +00:00
|
|
|
user_all_orgs = UserOrgLabelSerializer(many=True, read_only=True)
|
2020-05-28 07:02:53 +00:00
|
|
|
current_org_roles = serializers.ListField(read_only=True)
|
2020-05-28 09:47:18 +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
|
|
|
|
)
|
2020-06-03 10:20:33 +00:00
|
|
|
MFA_LEVEL_CHOICES = (
|
|
|
|
(0, _('Disable')),
|
|
|
|
(1, _('Enable')),
|
|
|
|
)
|
2020-06-03 13:00:37 +00:00
|
|
|
mfa_level = serializers.ChoiceField(choices=MFA_LEVEL_CHOICES, label=_('MFA'), required=False)
|
2020-06-03 10:20:33 +00:00
|
|
|
guide_url = serializers.SerializerMethodField()
|
2020-04-09 02:33:20 +00:00
|
|
|
|
2020-05-12 09:57:35 +00:00
|
|
|
class Meta(UserSerializer.Meta):
|
|
|
|
fields = UserSerializer.Meta.fields + [
|
2020-06-03 10:20:33 +00:00
|
|
|
'public_key_comment', 'public_key_hash_md5', 'admin_or_audit_orgs', 'current_org_roles',
|
2020-07-09 07:41:02 +00:00
|
|
|
'guide_url', 'user_all_orgs'
|
2019-11-08 07:48:01 +00:00
|
|
|
]
|
2020-05-28 07:02:53 +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},
|
2020-06-03 12:21:28 +00:00
|
|
|
'is_first_login': {'label': _('Is first login'), 'read_only': False},
|
2020-05-28 07:02:53 +00:00
|
|
|
'source': {'read_only': True},
|
|
|
|
'is_valid': {'read_only': True},
|
|
|
|
'is_active': {'read_only': True},
|
|
|
|
'groups': {'read_only': True},
|
|
|
|
'roles': {'read_only': True},
|
|
|
|
'password_strategy': {'read_only': True},
|
|
|
|
'date_expired': {'read_only': True},
|
|
|
|
'date_joined': {'read_only': True},
|
|
|
|
'last_login': {'read_only': True},
|
|
|
|
'role': {'read_only': True},
|
|
|
|
})
|
|
|
|
|
|
|
|
if 'password' in fields:
|
|
|
|
fields.remove('password')
|
|
|
|
extra_kwargs.pop('password', None)
|
|
|
|
|
2020-06-03 10:20:33 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_guide_url(obj):
|
|
|
|
return settings.USER_GUIDE_URL
|
|
|
|
|
2020-06-03 13:00:37 +00:00
|
|
|
def validate_mfa_level(self, mfa_level):
|
|
|
|
if self.instance and self.instance.mfa_force_enabled:
|
|
|
|
return 2
|
|
|
|
return mfa_level
|
|
|
|
|
2020-06-04 09:24:31 +00:00
|
|
|
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
|
|
|
|
|
2020-05-28 08:10:28 +00:00
|
|
|
|
|
|
|
class UserUpdatePasswordSerializer(serializers.ModelSerializer):
|
|
|
|
old_password = serializers.CharField(required=True, max_length=128, write_only=True)
|
|
|
|
new_password = serializers.CharField(required=True, max_length=128, write_only=True)
|
|
|
|
new_password_again = serializers.CharField(required=True, max_length=128, write_only=True)
|
|
|
|
|
|
|
|
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):
|
2020-06-16 03:04:59 +00:00
|
|
|
msg = _('The old password is incorrect')
|
2020-05-28 08:10:28 +00:00
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
return value
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def validate_new_password(value):
|
|
|
|
from ..utils import check_password_rules
|
|
|
|
if not check_password_rules(value):
|
|
|
|
msg = _('Password does not match security rules')
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def validate_new_password_again(self, value):
|
|
|
|
if value != self.initial_data.get('new_password', ''):
|
2020-06-16 03:04:59 +00:00
|
|
|
msg = _('The newly set password is inconsistent')
|
2020-05-28 08:10:28 +00:00
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
new_password = self.validated_data.get('new_password')
|
|
|
|
instance.reset_password(new_password)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
class UserUpdatePublicKeySerializer(serializers.ModelSerializer):
|
2020-05-28 09:47:18 +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
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2020-07-09 06:06:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MiniUserSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['id', 'name', 'username']
|
2020-10-14 07:17:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InviteSerializer(serializers.Serializer):
|
|
|
|
user = serializers.PrimaryKeyRelatedField(
|
|
|
|
queryset=User.objects.exclude(role=User.ROLE.APP)
|
|
|
|
)
|
|
|
|
role = serializers.ChoiceField(choices=ORG_ROLE.choices)
|