2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-07 10:58:57 +00:00
|
|
|
from rest_framework import serializers
|
2022-08-24 08:14:32 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers.fields import LabeledChoiceField
|
2023-08-03 03:21:30 +00:00
|
|
|
from common.utils import pretty_string
|
2022-12-07 10:58:57 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2024-02-06 10:28:31 +00:00
|
|
|
from terminal.session_lifecycle import lifecycle_events_map
|
2023-06-13 10:58:26 +00:00
|
|
|
from .terminal import TerminalSmallSerializer
|
2023-10-09 11:04:40 +00:00
|
|
|
from ..const import SessionType, SessionErrorReason
|
2019-12-05 07:09:25 +00:00
|
|
|
from ..models import Session
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
__all__ = [
|
|
|
|
'SessionSerializer', 'SessionDisplaySerializer',
|
2020-04-07 04:26:47 +00:00
|
|
|
'ReplaySerializer', 'SessionJoinValidateSerializer',
|
2024-02-06 10:28:31 +00:00
|
|
|
'SessionLifecycleLogSerializer'
|
2020-03-12 08:24:38 +00:00
|
|
|
]
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
class SessionSerializer(BulkOrgResourceModelSerializer):
|
|
|
|
org_id = serializers.CharField(allow_blank=True)
|
2023-06-27 02:48:57 +00:00
|
|
|
protocol = serializers.CharField(max_length=128, label=_("Protocol"))
|
2023-01-16 11:02:09 +00:00
|
|
|
type = LabeledChoiceField(
|
|
|
|
choices=SessionType.choices, label=_("Type"), default=SessionType.normal
|
|
|
|
)
|
|
|
|
can_replay = serializers.BooleanField(read_only=True, label=_("Can replay"))
|
|
|
|
can_join = serializers.BooleanField(read_only=True, label=_("Can join"))
|
|
|
|
can_terminate = serializers.BooleanField(read_only=True, label=_("Can terminate"))
|
2023-08-03 03:21:30 +00:00
|
|
|
asset = serializers.CharField(label=_("Asset"), style={'base_template': 'textarea.html'})
|
2023-10-09 11:04:40 +00:00
|
|
|
error_reason = LabeledChoiceField(
|
|
|
|
choices=SessionErrorReason.choices, label=_("Error reason"), required=False
|
|
|
|
)
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Session
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ["id"]
|
|
|
|
fields_small = fields_mini + [
|
2023-02-15 11:53:04 +00:00
|
|
|
"user", "asset", "user_id", "asset_id", 'account', 'account_id',
|
2022-12-16 09:16:14 +00:00
|
|
|
"protocol", 'type', "login_from", "remote_addr",
|
|
|
|
"is_success", "is_finished", "has_replay", "has_command",
|
2024-03-20 07:48:01 +00:00
|
|
|
"date_start", "date_end", "duration", "comment", "terminal_display", "is_locked",
|
2023-10-09 11:04:40 +00:00
|
|
|
'command_amount', 'error_reason'
|
2019-12-05 07:09:25 +00:00
|
|
|
]
|
2022-07-01 11:07:42 +00:00
|
|
|
fields_fk = ["terminal", ]
|
2022-12-07 10:58:57 +00:00
|
|
|
fields_custom = ["can_replay", "can_join", "can_terminate"]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields = fields_small + fields_fk + fields_custom
|
2020-06-02 12:02:22 +00:00
|
|
|
extra_kwargs = {
|
2024-03-20 07:48:01 +00:00
|
|
|
"Duration": {'label': _('Duration')},
|
2020-06-02 12:02:22 +00:00
|
|
|
"protocol": {'label': _('Protocol')},
|
2020-11-11 02:27:18 +00:00
|
|
|
'user_id': {'label': _('User ID')},
|
|
|
|
'asset_id': {'label': _('Asset ID')},
|
2021-07-15 10:09:29 +00:00
|
|
|
'login_from_display': {'label': _('Login from display')},
|
2020-11-11 02:27:18 +00:00
|
|
|
'is_success': {'label': _('Is success')},
|
|
|
|
'can_replay': {'label': _('Can replay')},
|
|
|
|
'can_join': {'label': _('Can join')},
|
2023-02-15 06:30:57 +00:00
|
|
|
'terminal': {'label': _('Terminal')},
|
2020-11-11 02:27:18 +00:00
|
|
|
'is_finished': {'label': _('Is finished')},
|
2021-09-15 12:51:59 +00:00
|
|
|
'can_terminate': {'label': _('Can terminate')},
|
2022-07-04 07:14:59 +00:00
|
|
|
'terminal_display': {'label': _('Terminal display')},
|
2020-06-02 12:02:22 +00:00
|
|
|
}
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2023-08-03 03:21:30 +00:00
|
|
|
def validate_asset(self, value):
|
|
|
|
max_length = self.Meta.model.asset.field.max_length
|
|
|
|
value = pretty_string(value, max_length=max_length)
|
|
|
|
return value
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class SessionDisplaySerializer(SessionSerializer):
|
2021-10-21 03:33:06 +00:00
|
|
|
command_amount = serializers.IntegerField(read_only=True, label=_('Command amount'))
|
2023-06-13 10:58:26 +00:00
|
|
|
terminal = TerminalSmallSerializer(read_only=True, label=_('Terminal'))
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
class Meta(SessionSerializer.Meta):
|
2022-07-01 11:07:42 +00:00
|
|
|
fields = SessionSerializer.Meta.fields + ['command_amount', ]
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
class ReplaySerializer(serializers.Serializer):
|
|
|
|
file = serializers.FileField(allow_empty_file=True)
|
2023-04-27 11:31:58 +00:00
|
|
|
version = serializers.IntegerField(write_only=True, required=False, min_value=2, max_value=4)
|
2020-04-07 04:26:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SessionJoinValidateSerializer(serializers.Serializer):
|
|
|
|
user_id = serializers.UUIDField()
|
|
|
|
session_id = serializers.UUIDField()
|
2024-02-06 10:28:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SessionLifecycleLogSerializer(serializers.Serializer):
|
|
|
|
event = serializers.ChoiceField(choices=list(lifecycle_events_map.keys()))
|
|
|
|
reason = serializers.CharField(required=False)
|
|
|
|
user = serializers.CharField(required=False)
|