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
|
2020-06-04 12:00:39 +00:00
|
|
|
from django.db.models import F
|
2018-04-06 03:27:52 +00:00
|
|
|
|
2020-06-04 12:00:39 +00:00
|
|
|
from common.mixins import BulkSerializerMixin
|
2019-08-21 12:27:21 +00:00
|
|
|
from terminal.models import Session
|
2020-05-08 08:48:26 +00:00
|
|
|
from ops.models import CommandExecution
|
2019-08-21 12:27:21 +00:00
|
|
|
from . import models
|
2018-04-06 03:27:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FTPLogSerializer(serializers.ModelSerializer):
|
2020-11-11 02:27:18 +00:00
|
|
|
operate_display = serializers.ReadOnlyField(source='get_operate_display', label=_('Operate for display'))
|
2018-04-06 03:27:52 +00:00
|
|
|
|
|
|
|
class Meta:
|
2019-08-21 12:27:21 +00:00
|
|
|
model = models.FTPLog
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'user', 'remote_addr', 'asset', 'system_user', 'org_id',
|
|
|
|
'operate', 'filename', 'operate_display',
|
|
|
|
'is_success',
|
|
|
|
'date_start',
|
|
|
|
]
|
|
|
|
fields = fields_small
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
2020-04-29 11:05:56 +00:00
|
|
|
class UserLoginLogSerializer(serializers.ModelSerializer):
|
2020-11-11 02:27:18 +00:00
|
|
|
type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type for display'))
|
|
|
|
status_display = serializers.ReadOnlyField(source='get_status_display', label=_('Status for display'))
|
|
|
|
mfa_display = serializers.ReadOnlyField(source='get_mfa_display', label=_('MFA for display'))
|
2020-05-06 11:35:19 +00:00
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.UserLoginLog
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'username', 'type', 'type_display', 'ip', 'city', 'user_agent',
|
|
|
|
'mfa', 'mfa_display', 'reason', 'backend',
|
|
|
|
'status', 'status_display',
|
|
|
|
'datetime',
|
|
|
|
]
|
|
|
|
fields = fields_small
|
2020-11-11 02:27:18 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
"user_agent": {'label': _('User agent')}
|
|
|
|
}
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OperateLogSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = models.OperateLog
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'user', 'action', 'resource_type', 'resource', 'remote_addr',
|
|
|
|
'datetime',
|
|
|
|
'org_id'
|
|
|
|
]
|
|
|
|
fields = fields_small
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PasswordChangeLogSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = models.PasswordChangeLog
|
2020-05-08 08:48:26 +00:00
|
|
|
fields = (
|
2020-06-02 07:41:27 +00:00
|
|
|
'id', 'user', 'change_by', 'remote_addr', 'datetime'
|
2020-05-08 08:48:26 +00:00
|
|
|
)
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SessionAuditSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Session
|
2018-04-06 03:27:52 +00:00
|
|
|
fields = '__all__'
|
2019-09-25 08:31:58 +00:00
|
|
|
|
2020-05-08 08:48:26 +00:00
|
|
|
|
|
|
|
class CommandExecutionSerializer(serializers.ModelSerializer):
|
2020-06-04 12:00:39 +00:00
|
|
|
is_success = serializers.BooleanField(read_only=True, label=_('Is success'))
|
2021-03-17 10:21:00 +00:00
|
|
|
hosts_display = serializers.ListSerializer(
|
|
|
|
child=serializers.CharField(), source='hosts', read_only=True, label=_('Hosts for display')
|
|
|
|
)
|
2020-06-04 12:00:39 +00:00
|
|
|
|
2020-05-08 08:48:26 +00:00
|
|
|
class Meta:
|
|
|
|
model = CommandExecution
|
2020-06-04 12:00:39 +00:00
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
2021-06-10 10:02:55 +00:00
|
|
|
'run_as', 'command', 'is_finished', 'user',
|
2020-07-14 09:54:56 +00:00
|
|
|
'date_start', 'result', 'is_success', 'org_id'
|
2020-06-04 12:00:39 +00:00
|
|
|
]
|
2021-03-17 10:09:38 +00:00
|
|
|
fields = fields_small + ['hosts', 'hosts_display', 'run_as_display', 'user_display']
|
2020-05-09 03:28:39 +00:00
|
|
|
extra_kwargs = {
|
2020-05-09 06:50:22 +00:00
|
|
|
'result': {'label': _('Result')}, # model 上的方法,只能在这修改
|
2020-05-09 03:28:39 +00:00
|
|
|
'is_success': {'label': _('Is success')},
|
2020-05-09 06:50:22 +00:00
|
|
|
'hosts': {'label': _('Hosts')}, # 外键,会生成 sql。不在 model 上修改
|
|
|
|
'run_as': {'label': _('Run as')},
|
|
|
|
'user': {'label': _('User')},
|
2020-11-11 02:27:18 +00:00
|
|
|
'run_as_display': {'label': _('Run as for display')},
|
|
|
|
'user_display': {'label': _('User for display')},
|
2020-05-09 03:28:39 +00:00
|
|
|
}
|
2020-06-04 12:00:39 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
2021-01-19 10:57:47 +00:00
|
|
|
queryset = queryset.prefetch_related('user', 'run_as', 'hosts')
|
2020-06-04 12:00:39 +00:00
|
|
|
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'
|
|
|
|
]
|