jumpserver/apps/users/serializers.py

86 lines
2.5 KiB
Python
Raw Normal View History

2016-09-03 11:05:50 +00:00
# -*- coding: utf-8 -*-
#
from django.utils.translation import ugettext_lazy as _
2016-09-03 11:05:50 +00:00
from rest_framework import serializers
2017-12-12 04:19:45 +00:00
from rest_framework_bulk import BulkListSerializer
2016-09-03 11:05:50 +00:00
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
2016-09-03 11:05:50 +00:00
from .models import User, UserGroup
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-11-14 16:48:48 +00:00
groups_display = serializers.SerializerMethodField()
2018-07-14 16:55:05 +00:00
groups = serializers.PrimaryKeyRelatedField(many=True, queryset = UserGroup.objects.all(), required=False)
2016-11-09 11:29:15 +00:00
2016-09-06 08:40:44 +00:00
class Meta:
model = User
2016-11-09 11:29:15 +00:00
list_serializer_class = BulkListSerializer
2018-04-18 04:48:07 +00:00
exclude = [
'first_name', 'last_name', 'password', '_private_key',
2018-04-19 03:13:11 +00:00
'_public_key', '_otp_secret_key', 'user_permissions'
2018-04-18 04:48:07 +00:00
]
2016-11-09 11:29:15 +00:00
def get_field_names(self, declared_fields, info):
fields = super(UserSerializer, self).get_field_names(declared_fields, info)
2018-06-06 08:59:32 +00:00
fields.extend([
'groups_display', 'get_role_display',
'get_source_display', 'is_valid'
])
2016-11-09 11:29:15 +00:00
return fields
@staticmethod
2016-11-14 16:48:48 +00:00
def get_groups_display(obj):
2016-11-09 11:29:15 +00:00
return " ".join([group.name for group in obj.groups.all()])
class UserPKUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = User
2016-09-18 06:28:34 +00:00
fields = ['id', '_public_key']
2016-10-15 09:14:56 +00:00
@staticmethod
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'))
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())
class Meta:
model = User
fields = ['id', 'groups']
2016-11-14 16:48:48 +00:00
class UserGroupSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2018-01-30 10:07:51 +00:00
users = serializers.SerializerMethodField()
2016-11-14 16:48:48 +00:00
class Meta:
model = UserGroup
list_serializer_class = BulkListSerializer
fields = '__all__'
2016-11-14 16:48:48 +00:00
@staticmethod
2018-01-30 10:07:51 +00:00
def get_users(obj):
return [user.name for user in obj.users.all()]
2016-11-14 16:48:48 +00:00
class UserGroupUpdateMemeberSerializer(serializers.ModelSerializer):
2018-07-14 16:55:05 +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']