2016-09-03 11:05:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-09-12 10:56:26 +00:00
|
|
|
import copy
|
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
|
2017-12-12 04:19:45 +00:00
|
|
|
from common.mixins import BulkSerializerMixin
|
2019-07-04 08:44:46 +00:00
|
|
|
from common.fields import StringManyToManyField
|
2019-04-25 02:11:50 +00:00
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
2019-09-12 10:56:26 +00:00
|
|
|
from common.permissions import CanUpdateDeleteUser
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2018-11-23 02:25:35 +00:00
|
|
|
from ..models import User, UserGroup
|
2016-09-03 11:05:50 +00:00
|
|
|
|
2019-07-04 08:44:46 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'UserSerializer', 'UserPKUpdateSerializer', 'UserUpdateGroupSerializer',
|
|
|
|
'UserGroupSerializer', 'UserGroupListSerializer',
|
2019-08-21 12:27:21 +00:00
|
|
|
'UserGroupUpdateMemberSerializer', 'ChangeUserPasswordSerializer',
|
|
|
|
'ResetOTPSerializer',
|
2019-07-04 08:44:46 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2016-11-09 11:29:15 +00:00
|
|
|
class UserSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
|
|
|
|
2019-09-12 10:56:26 +00:00
|
|
|
can_update = serializers.SerializerMethodField()
|
|
|
|
can_delete = serializers.SerializerMethodField()
|
|
|
|
|
2016-09-06 08:40:44 +00:00
|
|
|
class Meta:
|
|
|
|
model = User
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2018-12-17 12:09:25 +00:00
|
|
|
fields = [
|
2019-06-25 06:32:25 +00:00
|
|
|
'id', 'name', 'username', 'password', 'email', 'public_key',
|
|
|
|
'groups', 'groups_display',
|
2019-05-21 08:24:01 +00:00
|
|
|
'role', 'role_display', 'wechat', 'phone', 'otp_level',
|
|
|
|
'comment', 'source', 'source_display', 'is_valid', 'is_expired',
|
|
|
|
'is_active', 'created_by', 'is_first_login',
|
|
|
|
'date_password_last_updated', 'date_expired', 'avatar_url',
|
2019-09-12 10:56:26 +00:00
|
|
|
'can_update', 'can_delete',
|
2018-04-18 04:48:07 +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
|
|
|
'groups_display': {'label': _('Groups name')},
|
|
|
|
'source_display': {'label': _('Source name')},
|
|
|
|
'is_first_login': {'label': _('Is first login'), 'read_only': True},
|
|
|
|
'role_display': {'label': _('Role name')},
|
|
|
|
'is_valid': {'label': _('Is valid')},
|
|
|
|
'is_expired': {'label': _('Is expired')},
|
|
|
|
'avatar_url': {'label': _('Avatar url')},
|
2019-08-21 12:27:21 +00:00
|
|
|
'source': {'read_only': True},
|
2019-09-12 10:56:26 +00:00
|
|
|
'created_by': {'read_only': True, 'allow_blank': True},
|
|
|
|
'can_update': {'read_only': True},
|
|
|
|
'can_delete': {'read_only': True},
|
2019-05-21 08:24:01 +00:00
|
|
|
}
|
2016-11-09 11:29:15 +00:00
|
|
|
|
2019-09-12 10:56:26 +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
|
|
|
|
)
|
|
|
|
|
2019-07-02 06:17:56 +00:00
|
|
|
def validate_role(self, value):
|
|
|
|
request = self.context.get('request')
|
|
|
|
if not request.user.is_superuser and value != User.ROLE_USER:
|
|
|
|
role_display = dict(User.ROLE_CHOICES)[User.ROLE_USER]
|
|
|
|
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):
|
|
|
|
role = self.initial_data.get('role')
|
|
|
|
if self.instance:
|
|
|
|
role = role or self.instance.role
|
|
|
|
if role == User.ROLE_AUDITOR:
|
|
|
|
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
|
|
|
|
2019-09-12 10:56:26 +00:00
|
|
|
def validate(self, attrs):
|
|
|
|
attrs = self.change_password_to_raw(attrs)
|
|
|
|
return attrs
|
2019-06-25 06:32:25 +00:00
|
|
|
|
2016-09-06 13:44:23 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
2016-11-14 16:48:48 +00:00
|
|
|
class UserUpdateGroupSerializer(serializers.ModelSerializer):
|
2018-07-14 16:55:05 +00:00
|
|
|
groups = serializers.PrimaryKeyRelatedField(many=True, queryset=UserGroup.objects.all())
|
2016-10-02 03:09:27 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['id', 'groups']
|
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
class UserGroupSerializer(BulkOrgResourceModelSerializer):
|
2019-05-21 08:24:01 +00:00
|
|
|
users = serializers.PrimaryKeyRelatedField(
|
|
|
|
required=False, many=True, queryset=User.objects.all(), label=_('User')
|
|
|
|
)
|
2016-11-14 16:48:48 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserGroup
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2019-05-21 08:24:01 +00:00
|
|
|
fields = [
|
2019-07-04 08:30:26 +00:00
|
|
|
'id', 'name', 'users', 'comment', 'date_created',
|
2019-05-21 08:24:01 +00:00
|
|
|
'created_by',
|
|
|
|
]
|
|
|
|
extra_kwargs = {
|
|
|
|
'created_by': {'label': _('Created by'), 'read_only': True}
|
|
|
|
}
|
2016-11-14 16:48:48 +00:00
|
|
|
|
2019-09-12 10:56:26 +00:00
|
|
|
def validate_users(self, users):
|
|
|
|
for user in users:
|
|
|
|
if user.is_super_auditor:
|
|
|
|
msg = _('Auditors cannot be join in the group')
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
return users
|
|
|
|
|
2016-11-14 16:48:48 +00:00
|
|
|
|
2019-07-04 08:44:46 +00:00
|
|
|
class UserGroupListSerializer(UserGroupSerializer):
|
|
|
|
users = StringManyToManyField(many=True, read_only=True)
|
|
|
|
|
|
|
|
|
2018-12-17 12:09:25 +00:00
|
|
|
class UserGroupUpdateMemberSerializer(serializers.ModelSerializer):
|
2018-08-12 07:52:16 +00:00
|
|
|
users = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all())
|
2016-11-14 16:48:48 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserGroup
|
|
|
|
fields = ['id', 'users']
|
|
|
|
|
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)
|