jumpserver/apps/authentication/serializers/connection_token.py

50 lines
1.7 KiB
Python
Raw Normal View History

2022-11-14 06:44:18 +00:00
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from common.drf.fields import EncryptedField
2022-11-14 06:44:18 +00:00
from orgs.mixins.serializers import OrgResourceModelSerializerMixin
from ..models import ConnectionToken
__all__ = [
2022-12-05 02:48:19 +00:00
'ConnectionTokenSerializer', 'SuperConnectionTokenSerializer',
]
class ConnectionTokenSerializer(OrgResourceModelSerializerMixin):
expire_time = serializers.IntegerField(read_only=True, label=_('Expired time'))
input_secret = EncryptedField(
label=_("Input secret"), max_length=40960, required=False, allow_blank=True
)
class Meta:
model = ConnectionToken
2022-11-25 15:09:55 +00:00
fields_mini = ['id', 'value']
fields_small = fields_mini + [
'user', 'asset', 'account', 'input_username',
2022-12-05 02:48:19 +00:00
'input_secret', 'connect_method', 'protocol', 'actions',
'date_expired', 'date_created', 'date_updated', 'created_by',
2022-11-22 13:54:40 +00:00
'updated_by', 'org_id', 'org_name',
]
read_only_fields = [
# 普通 Token 不支持指定 user
2022-11-23 08:11:17 +00:00
'user', 'expire_time',
'user_display', 'asset_display',
]
fields = fields_small + read_only_fields
2022-11-25 15:09:55 +00:00
extra_kwargs = {
'value': {'read_only': True},
}
2022-12-05 05:37:37 +00:00
def get_user(self, attrs):
request = self.context.get('request')
user = request.user if request else None
return user
class SuperConnectionTokenSerializer(ConnectionTokenSerializer):
class Meta(ConnectionTokenSerializer.Meta):
2022-12-05 05:37:37 +00:00
read_only_fields = list(set(ConnectionTokenSerializer.Meta.read_only_fields) - {'user'})
def get_user(self, attrs):
2022-12-05 05:37:37 +00:00
return attrs.get('user')