2016-09-03 11:05:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
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
|
|
|
|
|
2017-12-24 10:53:07 +00:00
|
|
|
from common.utils import get_signer, 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-06-19 08:45:14 +00:00
|
|
|
from orgs.mixins 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',
|
|
|
|
'UserGroupUpdateMemberSerializer', 'ChangeUserPasswordSerializer'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-12-24 10:53:07 +00:00
|
|
|
signer = get_signer()
|
|
|
|
|
2016-09-03 11:05:50 +00:00
|
|
|
|
2016-11-09 11:29:15 +00:00
|
|
|
class UserSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
|
|
|
|
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',
|
2018-04-18 04:48:07 +00:00
|
|
|
]
|
2019-05-21 08:24:01 +00:00
|
|
|
extra_kwargs = {
|
2019-07-02 04:08:25 +00:00
|
|
|
'password': {'write_only': True, 'required': False},
|
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')},
|
|
|
|
'created_by': {'read_only': True}, 'source': {'read_only': True}
|
|
|
|
}
|
2016-11-09 11:29:15 +00:00
|
|
|
|
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-06-25 06:32:25 +00:00
|
|
|
@staticmethod
|
|
|
|
def validate_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
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def change_password_to_raw(validated_data):
|
|
|
|
password = validated_data.pop('password', None)
|
|
|
|
if password:
|
|
|
|
validated_data['password_raw'] = password
|
|
|
|
return validated_data
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
validated_data = self.change_password_to_raw(validated_data)
|
|
|
|
return super().create(validated_data)
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
validated_data = self.change_password_to_raw(validated_data)
|
|
|
|
return super().update(instance, validated_data)
|
|
|
|
|
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-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']
|