2018-04-06 03:27:52 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-06-16 08:33:53 +00:00
|
|
|
from rest_framework.mixins import ListModelMixin, CreateModelMixin
|
2020-06-04 12:00:39 +00:00
|
|
|
from django.db.models import F, Value
|
|
|
|
from django.db.models.functions import Concat
|
2018-04-06 03:27:52 +00:00
|
|
|
|
2020-04-29 11:05:56 +00:00
|
|
|
from common.permissions import IsOrgAdminOrAppUser, IsOrgAuditor, IsOrgAdmin
|
2020-06-05 06:20:34 +00:00
|
|
|
from common.drf.filters import DatetimeRangeFilter
|
2020-05-08 08:48:26 +00:00
|
|
|
from common.api import CommonGenericViewSet
|
2020-06-04 12:00:39 +00:00
|
|
|
from orgs.mixins.api import OrgGenericViewSet, OrgBulkModelViewSet, OrgRelationMixin
|
2020-04-29 11:05:56 +00:00
|
|
|
from orgs.utils import current_org
|
2020-05-08 08:48:26 +00:00
|
|
|
from ops.models import CommandExecution
|
|
|
|
from .models import FTPLog, UserLoginLog, OperateLog, PasswordChangeLog
|
|
|
|
from .serializers import FTPLogSerializer, UserLoginLogSerializer, CommandExecutionSerializer
|
2020-06-04 12:00:39 +00:00
|
|
|
from .serializers import OperateLogSerializer, PasswordChangeLogSerializer, CommandExecutionHostsRelationSerializer
|
2018-04-06 03:27:52 +00:00
|
|
|
|
|
|
|
|
2020-06-16 08:33:53 +00:00
|
|
|
class FTPLogViewSet(CreateModelMixin,
|
|
|
|
ListModelMixin,
|
|
|
|
OrgGenericViewSet):
|
2019-10-18 07:05:45 +00:00
|
|
|
model = FTPLog
|
2018-04-06 03:27:52 +00:00
|
|
|
serializer_class = FTPLogSerializer
|
2019-09-12 10:56:26 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser | IsOrgAuditor,)
|
2020-05-08 04:46:18 +00:00
|
|
|
extra_filter_backends = [DatetimeRangeFilter]
|
|
|
|
date_range_filter_fields = [
|
|
|
|
('date_start', ('date_from', 'date_to'))
|
|
|
|
]
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ['user', 'asset', 'system_user', 'filename']
|
|
|
|
search_fields = filterset_fields
|
2020-06-23 10:24:45 +00:00
|
|
|
ordering = ['-date_start']
|
2020-04-29 11:05:56 +00:00
|
|
|
|
|
|
|
|
2020-06-02 12:02:22 +00:00
|
|
|
class UserLoginLogViewSet(ListModelMixin, CommonGenericViewSet):
|
2020-04-29 11:05:56 +00:00
|
|
|
queryset = UserLoginLog.objects.all()
|
|
|
|
permission_classes = [IsOrgAdmin | IsOrgAuditor]
|
|
|
|
serializer_class = UserLoginLogSerializer
|
|
|
|
extra_filter_backends = [DatetimeRangeFilter]
|
|
|
|
date_range_filter_fields = [
|
|
|
|
('datetime', ('date_from', 'date_to'))
|
|
|
|
]
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ['username', 'ip', 'city', 'type', 'status', 'mfa']
|
2021-10-19 07:10:18 +00:00
|
|
|
search_fields = ['username', 'ip', 'city']
|
2020-04-29 11:05:56 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_org_members():
|
2020-07-20 02:42:22 +00:00
|
|
|
users = current_org.get_members().values_list('username', flat=True)
|
2020-04-29 11:05:56 +00:00
|
|
|
return users
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2021-10-19 07:10:18 +00:00
|
|
|
if current_org.is_root():
|
|
|
|
return queryset
|
|
|
|
users = self.get_org_members()
|
|
|
|
queryset = queryset.filter(username__in=users)
|
2020-04-29 11:05:56 +00:00
|
|
|
return queryset
|
2020-05-08 08:48:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OperateLogViewSet(ListModelMixin, OrgGenericViewSet):
|
|
|
|
model = OperateLog
|
|
|
|
serializer_class = OperateLogSerializer
|
|
|
|
permission_classes = [IsOrgAdmin | IsOrgAuditor]
|
|
|
|
extra_filter_backends = [DatetimeRangeFilter]
|
|
|
|
date_range_filter_fields = [
|
|
|
|
('datetime', ('date_from', 'date_to'))
|
|
|
|
]
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ['user', 'action', 'resource_type', 'resource', 'remote_addr']
|
2020-06-02 07:41:27 +00:00
|
|
|
search_fields = ['resource']
|
2020-06-02 07:40:07 +00:00
|
|
|
ordering = ['-datetime']
|
2020-05-08 08:48:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PasswordChangeLogViewSet(ListModelMixin, CommonGenericViewSet):
|
|
|
|
queryset = PasswordChangeLog.objects.all()
|
|
|
|
permission_classes = [IsOrgAdmin | IsOrgAuditor]
|
|
|
|
serializer_class = PasswordChangeLogSerializer
|
|
|
|
extra_filter_backends = [DatetimeRangeFilter]
|
|
|
|
date_range_filter_fields = [
|
|
|
|
('datetime', ('date_from', 'date_to'))
|
|
|
|
]
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ['user', 'change_by', 'remote_addr']
|
2021-07-13 10:03:02 +00:00
|
|
|
search_fields = filterset_fields
|
2020-06-02 07:40:07 +00:00
|
|
|
ordering = ['-datetime']
|
2020-05-08 08:48:26 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
users = current_org.get_members()
|
2020-05-08 08:48:26 +00:00
|
|
|
queryset = super().get_queryset().filter(
|
|
|
|
user__in=[user.__str__() for user in users]
|
|
|
|
)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class CommandExecutionViewSet(ListModelMixin, OrgGenericViewSet):
|
|
|
|
model = CommandExecution
|
|
|
|
serializer_class = CommandExecutionSerializer
|
|
|
|
permission_classes = [IsOrgAdmin | IsOrgAuditor]
|
2020-06-05 06:20:34 +00:00
|
|
|
extra_filter_backends = [DatetimeRangeFilter]
|
2020-05-08 08:48:26 +00:00
|
|
|
date_range_filter_fields = [
|
|
|
|
('date_start', ('date_from', 'date_to'))
|
|
|
|
]
|
2021-06-10 10:02:55 +00:00
|
|
|
filterset_fields = [
|
|
|
|
'user__name', 'user__username', 'command',
|
|
|
|
'run_as__name', 'run_as__username', 'is_finished'
|
|
|
|
]
|
|
|
|
search_fields = [
|
|
|
|
'command', 'user__name', 'user__username',
|
|
|
|
'run_as__name', 'run_as__username',
|
|
|
|
]
|
2020-06-02 07:40:07 +00:00
|
|
|
ordering = ['-date_created']
|
2020-06-04 12:00:39 +00:00
|
|
|
|
2020-06-05 06:20:34 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2021-03-18 02:27:14 +00:00
|
|
|
if current_org.is_root():
|
|
|
|
return queryset
|
2020-06-05 06:20:34 +00:00
|
|
|
queryset = queryset.filter(run_as__org_id=current_org.org_id())
|
|
|
|
return queryset
|
|
|
|
|
2020-06-04 12:00:39 +00:00
|
|
|
|
|
|
|
class CommandExecutionHostRelationViewSet(OrgRelationMixin, OrgBulkModelViewSet):
|
|
|
|
serializer_class = CommandExecutionHostsRelationSerializer
|
|
|
|
m2m_field = CommandExecution.hosts.field
|
2020-08-19 03:17:02 +00:00
|
|
|
permission_classes = [IsOrgAdmin | IsOrgAuditor]
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2020-06-04 12:00:39 +00:00
|
|
|
'id', 'asset', 'commandexecution'
|
|
|
|
]
|
|
|
|
search_fields = ('asset__hostname', )
|
|
|
|
http_method_names = ['options', 'get']
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
asset_display=Concat(
|
|
|
|
F('asset__hostname'), Value('('),
|
|
|
|
F('asset__ip'), Value(')')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return queryset
|