2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-07-11 10:09:06 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2023-07-13 08:00:24 +00:00
|
|
|
from common.serializers import CommonModelSerializer
|
2023-02-08 07:16:00 +00:00
|
|
|
from common.serializers.fields import EncryptedField
|
2023-05-09 05:43:46 +00:00
|
|
|
from perms.serializers.permission import ActionChoicesField
|
2022-12-06 03:03:14 +00:00
|
|
|
from ..models import ConnectionToken
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
__all__ = [
|
2022-12-05 02:48:19 +00:00
|
|
|
'ConnectionTokenSerializer', 'SuperConnectionTokenSerializer',
|
2023-07-13 08:00:24 +00:00
|
|
|
'ConnectionTokenReusableSerializer',
|
2022-07-11 10:09:06 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-07-13 08:00:24 +00:00
|
|
|
class ConnectionTokenSerializer(CommonModelSerializer):
|
2022-07-12 10:27:07 +00:00
|
|
|
expire_time = serializers.IntegerField(read_only=True, label=_('Expired time'))
|
2022-12-21 09:17:54 +00:00
|
|
|
input_secret = EncryptedField(
|
2023-01-16 11:02:09 +00:00
|
|
|
label=_("Input secret"), max_length=40960, required=False, allow_blank=True
|
2022-12-21 09:17:54 +00:00
|
|
|
)
|
2023-01-16 11:02:09 +00:00
|
|
|
from_ticket_info = serializers.SerializerMethodField(label=_("Ticket info"))
|
2023-02-08 12:14:33 +00:00
|
|
|
actions = ActionChoicesField(read_only=True, label=_("Actions"))
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ConnectionToken
|
2022-11-25 15:09:55 +00:00
|
|
|
fields_mini = ['id', 'value']
|
2022-07-11 10:09:06 +00:00
|
|
|
fields_small = fields_mini + [
|
2023-06-05 09:27:44 +00:00
|
|
|
'user', 'asset', 'account', 'input_username', 'input_secret',
|
|
|
|
'connect_method', 'connect_options', 'protocol', 'actions',
|
2023-05-09 05:43:46 +00:00
|
|
|
'is_active', 'is_reusable', 'from_ticket', 'from_ticket_info',
|
2022-12-05 02:48:19 +00:00
|
|
|
'date_expired', 'date_created', 'date_updated', 'created_by',
|
2022-11-22 13:54:40 +00:00
|
|
|
'updated_by', 'org_id', 'org_name',
|
2022-07-11 10:09:06 +00:00
|
|
|
]
|
|
|
|
read_only_fields = [
|
|
|
|
# 普通 Token 不支持指定 user
|
2023-05-09 05:43:46 +00:00
|
|
|
'user', 'expire_time', 'is_expired', 'date_expired',
|
2022-10-28 07:58:05 +00:00
|
|
|
'user_display', 'asset_display',
|
2022-07-11 10:09:06 +00:00
|
|
|
]
|
2022-11-29 06:42:04 +00:00
|
|
|
fields = fields_small + read_only_fields
|
2022-11-25 15:09:55 +00:00
|
|
|
extra_kwargs = {
|
2023-01-16 11:02:09 +00:00
|
|
|
'from_ticket': {'read_only': True},
|
2022-11-25 15:09:55 +00:00
|
|
|
'value': {'read_only': True},
|
2023-02-15 08:28:21 +00:00
|
|
|
'is_expired': {'read_only': True, 'label': _('Is expired')},
|
2022-11-25 15:09:55 +00:00
|
|
|
}
|
2022-07-11 10:09:06 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
def get_request_user(self):
|
2022-10-28 07:58:05 +00:00
|
|
|
request = self.context.get('request')
|
|
|
|
user = request.user if request else None
|
|
|
|
return user
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
def get_user(self, attrs):
|
|
|
|
return self.get_request_user()
|
|
|
|
|
|
|
|
def get_from_ticket_info(self, instance):
|
|
|
|
if not instance.from_ticket:
|
|
|
|
return {}
|
|
|
|
user = self.get_request_user()
|
|
|
|
info = instance.from_ticket.get_extra_info_of_review(user=user)
|
|
|
|
return info
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
2023-07-13 08:00:24 +00:00
|
|
|
class ConnectionTokenReusableSerializer(CommonModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ConnectionToken
|
|
|
|
fields = ['id', 'date_expired', 'is_reusable']
|
2023-05-09 05:43:46 +00:00
|
|
|
can_update_fields = ['is_reusable']
|
2023-07-13 08:00:24 +00:00
|
|
|
read_only_fields = list(set(fields) - set(can_update_fields))
|
2023-05-09 05:43:46 +00:00
|
|
|
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
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'})
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
def get_user(self, attrs):
|
2022-12-05 05:37:37 +00:00
|
|
|
return attrs.get('user')
|