2018-04-06 03:27:52 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2018-04-06 03:27:52 +00:00
|
|
|
from rest_framework import serializers
|
2023-07-24 03:52:25 +00:00
|
|
|
|
2023-01-17 04:43:07 +00:00
|
|
|
from audits.backends.db import OperateLogStore
|
2023-09-15 09:15:15 +00:00
|
|
|
from common.serializers.fields import LabeledChoiceField, ObjectRelatedField
|
2023-02-14 12:01:29 +00:00
|
|
|
from common.utils import reverse, i18n_trans
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.utils.timezone import as_current_tz
|
2022-12-16 07:52:02 +00:00
|
|
|
from ops.serializers.job import JobExecutionSerializer
|
2023-07-24 03:52:25 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2019-08-21 12:27:21 +00:00
|
|
|
from terminal.models import Session
|
2023-09-15 09:15:15 +00:00
|
|
|
from users.models import User
|
2019-08-21 12:27:21 +00:00
|
|
|
from . import models
|
2022-11-10 06:44:23 +00:00
|
|
|
from .const import (
|
2022-12-20 10:12:35 +00:00
|
|
|
ActionChoices, OperateChoices,
|
|
|
|
MFAChoices, LoginStatusChoices,
|
2023-02-07 00:52:48 +00:00
|
|
|
LoginTypeChoices, ActivityChoices,
|
2022-11-10 06:44:23 +00:00
|
|
|
)
|
2018-04-06 03:27:52 +00:00
|
|
|
|
|
|
|
|
2023-02-17 11:58:43 +00:00
|
|
|
class JobLogSerializer(JobExecutionSerializer):
|
2022-12-16 07:52:02 +00:00
|
|
|
class Meta:
|
2023-02-17 11:58:43 +00:00
|
|
|
model = models.JobLog
|
2022-12-20 10:12:35 +00:00
|
|
|
read_only_fields = [
|
2022-12-20 12:23:42 +00:00
|
|
|
"id", "material", "time_cost", 'date_start',
|
|
|
|
'date_finished', 'date_created',
|
|
|
|
'is_finished', 'is_success', 'created_by',
|
2023-01-16 11:02:09 +00:00
|
|
|
'task_id'
|
2022-12-20 10:12:35 +00:00
|
|
|
]
|
2022-12-16 07:52:02 +00:00
|
|
|
fields = read_only_fields + []
|
|
|
|
|
|
|
|
|
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-12-20 10:12:35 +00:00
|
|
|
"user", "remote_addr", "asset", "account",
|
2023-06-08 10:04:07 +00:00
|
|
|
"org_id", "operate", "filename", "date_start",
|
|
|
|
"is_success", "has_file",
|
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-12-07 10:58:57 +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
|
2023-01-17 04:43:07 +00:00
|
|
|
fields = ('diff',)
|
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
data = super().to_representation(instance)
|
|
|
|
diff = OperateLogStore.convert_diff_friendly(data['diff'])
|
|
|
|
data['diff'] = diff
|
|
|
|
return data
|
2022-11-04 06:22:38 +00:00
|
|
|
|
|
|
|
|
2023-03-08 07:01:05 +00:00
|
|
|
class OperateLogSerializer(BulkOrgResourceModelSerializer):
|
2022-11-11 07:11:10 +00:00
|
|
|
action = LabeledChoiceField(choices=ActionChoices.choices, label=_("Action"))
|
2023-02-23 08:20:43 +00:00
|
|
|
resource = serializers.SerializerMethodField(label=_("Resource"))
|
2023-02-23 08:25:02 +00:00
|
|
|
resource_type = serializers.SerializerMethodField(label=_('Resource Type'))
|
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-12-07 10:58:57 +00:00
|
|
|
"user", "action", "resource_type",
|
2023-02-17 08:54:51 +00:00
|
|
|
"resource", "remote_addr", "datetime",
|
|
|
|
"org_id",
|
2021-04-29 11:10:45 +00:00
|
|
|
]
|
|
|
|
fields = fields_small
|
2019-08-21 12:27:21 +00:00
|
|
|
|
2023-02-23 08:20:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_resource_type(instance):
|
|
|
|
return _(instance.resource_type)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_resource(instance):
|
|
|
|
return i18n_trans(instance.resource)
|
|
|
|
|
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__"
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
|
2023-02-14 12:01:29 +00:00
|
|
|
class ActivityUnionLogSerializer(serializers.Serializer):
|
2023-02-17 09:14:53 +00:00
|
|
|
id = serializers.CharField()
|
2023-01-16 11:02:09 +00:00
|
|
|
timestamp = serializers.SerializerMethodField()
|
2023-02-07 00:52:48 +00:00
|
|
|
detail_url = serializers.SerializerMethodField()
|
2023-01-16 11:02:09 +00:00
|
|
|
content = serializers.SerializerMethodField()
|
2023-02-15 02:46:37 +00:00
|
|
|
r_type = serializers.CharField(read_only=True)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_timestamp(obj):
|
2023-02-07 00:52:48 +00:00
|
|
|
return as_current_tz(obj['datetime']).strftime('%Y-%m-%d %H:%M:%S')
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_content(obj):
|
2023-02-07 00:52:48 +00:00
|
|
|
if not obj['r_detail']:
|
|
|
|
action = obj['r_action'].replace('_', ' ').capitalize()
|
2023-02-17 09:14:53 +00:00
|
|
|
ctn = _('User %s %s this resource') % (obj['r_user'], _(action))
|
2023-01-17 04:43:07 +00:00
|
|
|
else:
|
2023-02-14 12:01:29 +00:00
|
|
|
ctn = i18n_trans(obj['r_detail'])
|
2023-01-16 11:02:09 +00:00
|
|
|
return ctn
|
2023-02-07 00:52:48 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_detail_url(obj):
|
2023-02-08 03:32:05 +00:00
|
|
|
detail_url = ''
|
|
|
|
detail_id, obj_type = obj['r_detail_id'], obj['r_type']
|
2023-02-09 03:02:09 +00:00
|
|
|
if not detail_id:
|
|
|
|
return detail_url
|
|
|
|
|
|
|
|
if obj_type == ActivityChoices.operate_log:
|
2023-02-15 02:46:37 +00:00
|
|
|
detail_url = '%s?%s' % (
|
|
|
|
reverse(
|
|
|
|
'audits:operate-log-detail',
|
|
|
|
kwargs={'pk': obj['id']},
|
|
|
|
), 'type=action_detail')
|
2023-02-09 03:02:09 +00:00
|
|
|
elif obj_type == ActivityChoices.task:
|
|
|
|
detail_url = reverse(
|
|
|
|
'ops:celery-task-log', kwargs={'pk': detail_id}
|
|
|
|
)
|
|
|
|
elif obj_type == ActivityChoices.login_log:
|
2023-02-15 02:46:37 +00:00
|
|
|
detail_url = reverse(
|
|
|
|
'audits:login-log-detail',
|
|
|
|
kwargs={'pk': detail_id},
|
|
|
|
api_to_ui=True, is_audit=True
|
2023-02-09 03:02:09 +00:00
|
|
|
)
|
2023-02-07 00:52:48 +00:00
|
|
|
return detail_url
|
2023-06-08 10:04:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FileSerializer(serializers.Serializer):
|
|
|
|
file = serializers.FileField(allow_empty_file=True)
|
2023-09-15 09:15:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserSessionSerializer(serializers.ModelSerializer):
|
|
|
|
type = LabeledChoiceField(choices=LoginTypeChoices.choices, label=_("Type"))
|
|
|
|
user = ObjectRelatedField(required=False, queryset=User.objects, label=_('User'))
|
2023-10-18 07:50:38 +00:00
|
|
|
date_expired = serializers.DateTimeField(format="%Y/%m/%d %H:%M:%S", label=_('Date expired'))
|
2023-09-15 09:15:15 +00:00
|
|
|
is_current_user_session = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.UserSession
|
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'type', 'ip', 'city', 'user_agent', 'user', 'is_current_user_session',
|
2023-11-08 08:49:38 +00:00
|
|
|
'backend', 'backend_display', 'is_active', 'date_created', 'date_expired'
|
2023-09-15 09:15:15 +00:00
|
|
|
]
|
|
|
|
fields = fields_small
|
|
|
|
extra_kwargs = {
|
|
|
|
"backend_display": {"label": _("Authentication backend")},
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_is_current_user_session(self, obj):
|
|
|
|
request = self.context.get('request')
|
|
|
|
if not request:
|
|
|
|
return False
|
|
|
|
return request.session.session_key == obj.key
|