2021-07-12 07:55:29 +00:00
|
|
|
|
from django.db.models import F, Q
|
2021-07-08 06:23:18 +00:00
|
|
|
|
from rest_framework.decorators import action
|
2021-07-12 07:55:29 +00:00
|
|
|
|
from django_filters import rest_framework as filters
|
2021-07-08 06:23:18 +00:00
|
|
|
|
from rest_framework.response import Response
|
2021-07-22 08:17:23 +00:00
|
|
|
|
from django.shortcuts import get_object_or_404
|
2021-07-13 04:18:19 +00:00
|
|
|
|
from rest_framework.generics import CreateAPIView
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
|
|
|
|
from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser, NeedMFAVerify
|
2021-07-12 07:55:29 +00:00
|
|
|
|
from common.drf.filters import BaseFilterSet
|
2021-07-08 06:23:18 +00:00
|
|
|
|
from ..tasks.account_connectivity import test_accounts_connectivity_manual
|
2021-07-22 08:17:23 +00:00
|
|
|
|
from ..models import AuthBook, Node
|
2021-07-08 06:23:18 +00:00
|
|
|
|
from .. import serializers
|
|
|
|
|
|
2021-07-13 04:18:19 +00:00
|
|
|
|
__all__ = ['AccountViewSet', 'AccountSecretsViewSet', 'AccountTaskCreateAPI']
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
|
|
2021-07-12 07:55:29 +00:00
|
|
|
|
class AccountFilterSet(BaseFilterSet):
|
|
|
|
|
username = filters.CharFilter(method='do_nothing')
|
|
|
|
|
ip = filters.CharFilter(field_name='ip', lookup_expr='exact')
|
|
|
|
|
hostname = filters.CharFilter(field_name='hostname', lookup_expr='exact')
|
2021-07-22 08:17:23 +00:00
|
|
|
|
node = filters.CharFilter(method='do_nothing')
|
2021-07-12 07:55:29 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def qs(self):
|
|
|
|
|
qs = super().qs
|
|
|
|
|
qs = self.filter_username(qs)
|
2021-07-22 08:17:23 +00:00
|
|
|
|
qs = self.filter_node(qs)
|
2021-07-12 07:55:29 +00:00
|
|
|
|
return qs
|
|
|
|
|
|
|
|
|
|
def filter_username(self, qs):
|
|
|
|
|
username = self.get_query_param('username')
|
|
|
|
|
if not username:
|
|
|
|
|
return qs
|
|
|
|
|
qs = qs.filter(Q(username=username) | Q(systemuser__username=username)).distinct()
|
|
|
|
|
return qs
|
|
|
|
|
|
2021-07-22 08:17:23 +00:00
|
|
|
|
def filter_node(self, qs):
|
|
|
|
|
node_id = self.get_query_param('node')
|
|
|
|
|
if not node_id:
|
|
|
|
|
return qs
|
|
|
|
|
node = get_object_or_404(Node, pk=node_id)
|
2021-08-18 06:51:16 +00:00
|
|
|
|
node_ids = node.get_all_children(with_self=True).values_list('id', flat=True)
|
2021-07-22 08:17:23 +00:00
|
|
|
|
node_ids = list(node_ids)
|
|
|
|
|
qs = qs.filter(asset__nodes__in=node_ids)
|
|
|
|
|
return qs
|
|
|
|
|
|
2021-07-12 07:55:29 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
model = AuthBook
|
|
|
|
|
fields = [
|
|
|
|
|
'asset', 'systemuser', 'id',
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
class AccountViewSet(OrgBulkModelViewSet):
|
|
|
|
|
model = AuthBook
|
2021-07-12 07:55:29 +00:00
|
|
|
|
filterset_fields = ("username", "asset", "systemuser", 'ip', 'hostname')
|
|
|
|
|
search_fields = ('username', 'ip', 'hostname', 'systemuser__username')
|
|
|
|
|
filterset_class = AccountFilterSet
|
2021-07-08 06:23:18 +00:00
|
|
|
|
serializer_classes = {
|
|
|
|
|
'default': serializers.AccountSerializer,
|
|
|
|
|
'verify_account': serializers.AssetTaskSerializer
|
|
|
|
|
}
|
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-01-17 07:58:39 +00:00
|
|
|
|
queryset = AuthBook.get_queryset()
|
2021-07-08 06:23:18 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
@action(methods=['post'], detail=True, url_path='verify')
|
|
|
|
|
def verify_account(self, request, *args, **kwargs):
|
|
|
|
|
account = super().get_object()
|
|
|
|
|
task = test_accounts_connectivity_manual.delay([account])
|
|
|
|
|
return Response(data={'task': task.id})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AccountSecretsViewSet(AccountViewSet):
|
|
|
|
|
"""
|
|
|
|
|
因为可能要导出所有账号,所以单独建立了一个 viewset
|
|
|
|
|
"""
|
|
|
|
|
serializer_classes = {
|
|
|
|
|
'default': serializers.AccountSecretSerializer
|
|
|
|
|
}
|
|
|
|
|
permission_classes = (IsOrgAdmin, NeedMFAVerify)
|
|
|
|
|
http_method_names = ['get']
|
|
|
|
|
|
2021-07-13 04:18:19 +00:00
|
|
|
|
|
|
|
|
|
class AccountTaskCreateAPI(CreateAPIView):
|
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
|
serializer_class = serializers.AccountTaskSerializer
|
|
|
|
|
filterset_fields = AccountViewSet.filterset_fields
|
|
|
|
|
search_fields = AccountViewSet.search_fields
|
|
|
|
|
filterset_class = AccountViewSet.filterset_class
|
|
|
|
|
|
|
|
|
|
def get_accounts(self):
|
|
|
|
|
queryset = AuthBook.objects.all()
|
|
|
|
|
queryset = self.filter_queryset(queryset)
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
|
accounts = self.get_accounts()
|
|
|
|
|
task = test_accounts_connectivity_manual.delay(accounts)
|
|
|
|
|
data = getattr(serializer, '_data', {})
|
|
|
|
|
data["task"] = task.id
|
|
|
|
|
setattr(serializer, '_data', data)
|
|
|
|
|
return task
|
|
|
|
|
|
|
|
|
|
def get_exception_handler(self):
|
|
|
|
|
def handler(e, context):
|
|
|
|
|
return Response({"error": str(e)}, status=400)
|
2021-08-26 07:01:43 +00:00
|
|
|
|
|
2021-07-13 04:18:19 +00:00
|
|
|
|
return handler
|