2018-04-06 03:27:52 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-05-09 03:28:39 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-04-06 03:27:52 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2022-11-10 06:44:23 +00:00
|
|
|
from common.drf.fields import LabeledChoiceField
|
2019-08-21 12:27:21 +00:00
|
|
|
from terminal.models import Session
|
|
|
|
from . import models
|
2022-11-10 06:44:23 +00:00
|
|
|
from .const import (
|
2022-11-11 07:11:10 +00:00
|
|
|
ActionChoices,
|
|
|
|
OperateChoices,
|
|
|
|
MFAChoices,
|
|
|
|
LoginStatusChoices,
|
|
|
|
LoginTypeChoices,
|
2022-11-10 06:44:23 +00:00
|
|
|
)
|
2018-04-06 03:27:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FTPLogSerializer(serializers.ModelSerializer):
|
2022-11-11 07:11:10 +00:00
|
|
|
operate = LabeledChoiceField(choices=OperateChoices.choices, label=_("Operate"))
|
2018-04-06 03:27:52 +00:00
|
|
|
|
|
|
|
class Meta:
|
2019-08-21 12:27:21 +00:00
|
|
|
model = models.FTPLog
|
2022-11-11 07:11:10 +00:00
|
|
|
fields_mini = ["id"]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_small = fields_mini + [
|
2022-11-11 07:11:10 +00:00
|
|
|
"user",
|
|
|
|
"remote_addr",
|
|
|
|
"asset",
|
|
|
|
"system_user",
|
|
|
|
"org_id",
|
|
|
|
"operate",
|
|
|
|
"filename",
|
|
|
|
"is_success",
|
|
|
|
"date_start",
|
2021-04-29 11:10:45 +00:00
|
|
|
]
|
|
|
|
fields = fields_small
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
2020-04-29 11:05:56 +00:00
|
|
|
class UserLoginLogSerializer(serializers.ModelSerializer):
|
2022-11-11 07:11:10 +00:00
|
|
|
mfa = LabeledChoiceField(choices=MFAChoices.choices, label=_("MFA"))
|
|
|
|
type = LabeledChoiceField(choices=LoginTypeChoices.choices, label=_("Type"))
|
|
|
|
status = LabeledChoiceField(choices=LoginStatusChoices.choices, label=_("Status"))
|
2020-05-06 11:35:19 +00:00
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.UserLoginLog
|
2022-11-11 07:11:10 +00:00
|
|
|
fields_mini = ["id"]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_small = fields_mini + [
|
2022-11-11 07:11:10 +00:00
|
|
|
"username",
|
|
|
|
"type",
|
|
|
|
"ip",
|
|
|
|
"city",
|
|
|
|
"user_agent",
|
|
|
|
"mfa",
|
|
|
|
"reason",
|
|
|
|
"reason_display",
|
|
|
|
"backend",
|
|
|
|
"backend_display",
|
|
|
|
"status",
|
|
|
|
"datetime",
|
2021-04-29 11:10:45 +00:00
|
|
|
]
|
|
|
|
fields = fields_small
|
2020-11-11 02:27:18 +00:00
|
|
|
extra_kwargs = {
|
2022-11-11 07:11:10 +00:00
|
|
|
"user_agent": {"label": _("User agent")},
|
|
|
|
"reason_display": {"label": _("Reason display")},
|
|
|
|
"backend_display": {"label": _("Authentication backend")},
|
2020-11-11 02:27:18 +00:00
|
|
|
}
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
2022-11-04 06:22:38 +00:00
|
|
|
class OperateLogActionDetailSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = models.OperateLog
|
|
|
|
fields = ('before', 'after')
|
|
|
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
class OperateLogSerializer(serializers.ModelSerializer):
|
2022-11-11 07:11:10 +00:00
|
|
|
action = LabeledChoiceField(choices=ActionChoices.choices, label=_("Action"))
|
2022-01-18 07:20:29 +00:00
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.OperateLog
|
2022-11-11 07:11:10 +00:00
|
|
|
fields_mini = ["id"]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_small = fields_mini + [
|
2022-11-11 07:11:10 +00:00
|
|
|
"user",
|
|
|
|
"action",
|
|
|
|
"resource_type",
|
|
|
|
"resource_type_display",
|
|
|
|
"resource",
|
|
|
|
"remote_addr",
|
|
|
|
"datetime",
|
|
|
|
"org_id",
|
2021-04-29 11:10:45 +00:00
|
|
|
]
|
|
|
|
fields = fields_small
|
2022-11-11 07:11:10 +00:00
|
|
|
extra_kwargs = {"resource_type_display": {"label": _("Resource Type")}}
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PasswordChangeLogSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = models.PasswordChangeLog
|
2022-11-11 07:11:10 +00:00
|
|
|
fields = ("id", "user", "change_by", "remote_addr", "datetime")
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SessionAuditSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Session
|
2022-11-11 07:11:10 +00:00
|
|
|
fields = "__all__"
|
2020-06-04 12:00:39 +00:00
|
|
|
|
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
#
|
|
|
|
# class CommandExecutionSerializer(serializers.ModelSerializer):
|
|
|
|
# is_success = serializers.BooleanField(read_only=True, label=_('Is success'))
|
|
|
|
# hosts_display = serializers.ListSerializer(
|
|
|
|
# child=serializers.CharField(), source='hosts', read_only=True, label=_('Hosts display')
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# class Meta:
|
|
|
|
# model = CommandExecution
|
|
|
|
# fields_mini = ['id']
|
|
|
|
# fields_small = fields_mini + [
|
|
|
|
# 'command', 'is_finished', 'user',
|
|
|
|
# 'date_start', 'result', 'is_success', 'org_id'
|
|
|
|
# ]
|
|
|
|
# fields = fields_small + ['hosts', 'hosts_display', 'user_display']
|
|
|
|
# extra_kwargs = {
|
|
|
|
# 'result': {'label': _('Result')}, # model 上的方法,只能在这修改
|
|
|
|
# 'is_success': {'label': _('Is success')},
|
|
|
|
# 'hosts': {'label': _('Hosts')}, # 外键,会生成 sql。不在 model 上修改
|
|
|
|
# 'user': {'label': _('User')},
|
|
|
|
# 'user_display': {'label': _('User display')},
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# @classmethod
|
|
|
|
# def setup_eager_loading(cls, queryset):
|
|
|
|
# """ Perform necessary eager loading of data. """
|
|
|
|
# queryset = queryset.prefetch_related('user', 'hosts')
|
|
|
|
# return queryset
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# class CommandExecutionHostsRelationSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
|
|
|
# asset_display = serializers.ReadOnlyField()
|
|
|
|
# commandexecution_display = serializers.ReadOnlyField()
|
|
|
|
#
|
|
|
|
# class Meta:
|
|
|
|
# model = CommandExecution.hosts.through
|
|
|
|
# fields = [
|
|
|
|
# 'id', 'asset', 'asset_display', 'commandexecution', 'commandexecution_display'
|
|
|
|
# ]
|