2019-03-18 02:15:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from rest_framework.response import Response
|
2019-08-21 12:27:21 +00:00
|
|
|
from rest_framework import generics
|
2019-06-19 03:31:38 +00:00
|
|
|
from rest_framework import filters
|
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
|
|
|
from django.shortcuts import get_object_or_404
|
2019-08-21 12:27:21 +00:00
|
|
|
from django.http import Http404
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2019-06-24 12:39:45 +00:00
|
|
|
from common.permissions import IsOrgAdminOrAppUser, NeedMFAVerify
|
2019-03-18 02:15:33 +00:00
|
|
|
from common.utils import get_object_or_none, get_logger
|
2019-09-18 14:06:46 +00:00
|
|
|
from common.mixins import CommonApiMixin
|
2019-06-19 03:31:38 +00:00
|
|
|
from ..backends import AssetUserManager
|
|
|
|
from ..models import Asset, Node, SystemUser, AdminUser
|
2019-03-18 02:15:33 +00:00
|
|
|
from .. import serializers
|
|
|
|
from ..tasks import test_asset_users_connectivity_manual
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'AssetUserViewSet', 'AssetUserAuthInfoApi', 'AssetUserTestConnectiveApi',
|
2019-06-19 03:31:38 +00:00
|
|
|
'AssetUserExportViewSet',
|
2019-03-18 02:15:33 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class AssetUserFilterBackend(filters.BaseFilterBackend):
|
|
|
|
def filter_queryset(self, request, queryset, view):
|
|
|
|
kwargs = {}
|
|
|
|
for field in view.filter_fields:
|
|
|
|
value = request.GET.get(field)
|
|
|
|
if not value:
|
|
|
|
continue
|
|
|
|
if field in ("node_id", "system_user_id", "admin_user_id"):
|
|
|
|
continue
|
|
|
|
kwargs[field] = value
|
|
|
|
return queryset.filter(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class AssetUserSearchBackend(filters.BaseFilterBackend):
|
|
|
|
def filter_queryset(self, request, queryset, view):
|
|
|
|
value = request.GET.get('search')
|
|
|
|
if not value:
|
|
|
|
return queryset
|
|
|
|
_queryset = AssetUserManager.none()
|
|
|
|
for field in view.search_fields:
|
|
|
|
if field in ("node_id", "system_user_id", "admin_user_id"):
|
|
|
|
continue
|
|
|
|
_queryset |= queryset.filter(**{field: value})
|
|
|
|
return _queryset
|
|
|
|
|
|
|
|
|
2019-09-18 14:06:46 +00:00
|
|
|
class AssetUserViewSet(CommonApiMixin, BulkModelViewSet):
|
2019-03-18 02:15:33 +00:00
|
|
|
serializer_class = serializers.AssetUserSerializer
|
2019-06-24 12:39:45 +00:00
|
|
|
permission_classes = [IsOrgAdminOrAppUser]
|
2019-03-18 02:15:33 +00:00
|
|
|
http_method_names = ['get', 'post']
|
2019-06-19 03:31:38 +00:00
|
|
|
filter_fields = [
|
|
|
|
"id", "ip", "hostname", "username", "asset_id", "node_id",
|
|
|
|
"system_user_id", "admin_user_id"
|
|
|
|
]
|
|
|
|
search_fields = filter_fields
|
|
|
|
filter_backends = (
|
|
|
|
filters.OrderingFilter,
|
|
|
|
AssetUserFilterBackend, AssetUserSearchBackend,
|
|
|
|
)
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
def allow_bulk_destroy(self, qs, filtered):
|
|
|
|
return False
|
|
|
|
|
2019-03-18 02:15:33 +00:00
|
|
|
def get_queryset(self):
|
2019-06-19 03:31:38 +00:00
|
|
|
# 尽可能先返回更少的数据
|
2019-03-18 02:15:33 +00:00
|
|
|
username = self.request.GET.get('username')
|
|
|
|
asset_id = self.request.GET.get('asset_id')
|
2019-06-19 03:31:38 +00:00
|
|
|
node_id = self.request.GET.get('node_id')
|
|
|
|
admin_user_id = self.request.GET.get("admin_user_id")
|
|
|
|
system_user_id = self.request.GET.get("system_user_id")
|
|
|
|
|
|
|
|
kwargs = {}
|
2019-06-24 12:16:18 +00:00
|
|
|
assets = None
|
2019-06-19 03:31:38 +00:00
|
|
|
|
|
|
|
manager = AssetUserManager()
|
|
|
|
if system_user_id:
|
|
|
|
system_user = get_object_or_404(SystemUser, id=system_user_id)
|
2019-07-26 08:16:06 +00:00
|
|
|
assets = system_user.get_all_assets()
|
2019-06-19 03:31:38 +00:00
|
|
|
username = system_user.username
|
|
|
|
elif admin_user_id:
|
|
|
|
admin_user = get_object_or_404(AdminUser, id=admin_user_id)
|
|
|
|
assets = admin_user.assets.all()
|
|
|
|
username = admin_user.username
|
|
|
|
manager.prefer('admin_user')
|
|
|
|
|
|
|
|
if asset_id:
|
2019-06-24 12:16:18 +00:00
|
|
|
asset = get_object_or_404(Asset, id=asset_id)
|
2019-06-19 03:31:38 +00:00
|
|
|
assets = [asset]
|
|
|
|
elif node_id:
|
|
|
|
node = get_object_or_404(Node, id=node_id)
|
2019-06-19 11:25:21 +00:00
|
|
|
assets = node.get_all_assets()
|
2019-06-19 03:31:38 +00:00
|
|
|
|
|
|
|
if username:
|
|
|
|
kwargs['username'] = username
|
2019-06-24 12:16:18 +00:00
|
|
|
if assets is not None:
|
2019-06-19 03:31:38 +00:00
|
|
|
kwargs['assets'] = assets
|
|
|
|
|
|
|
|
queryset = manager.filter(**kwargs)
|
2019-03-18 02:15:33 +00:00
|
|
|
return queryset
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
|
|
|
|
class AssetUserExportViewSet(AssetUserViewSet):
|
|
|
|
serializer_class = serializers.AssetUserExportSerializer
|
|
|
|
http_method_names = ['get']
|
2019-07-04 09:26:05 +00:00
|
|
|
permission_classes = [IsOrgAdminOrAppUser, NeedMFAVerify]
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetUserAuthInfoApi(generics.RetrieveAPIView):
|
|
|
|
serializer_class = serializers.AssetUserAuthInfoSerializer
|
2019-06-24 12:39:45 +00:00
|
|
|
permission_classes = [IsOrgAdminOrAppUser, NeedMFAVerify]
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
2019-07-04 07:36:57 +00:00
|
|
|
query_params = self.request.query_params
|
|
|
|
username = query_params.get('username')
|
|
|
|
asset_id = query_params.get('asset_id')
|
|
|
|
prefer = query_params.get("prefer")
|
2019-03-18 02:15:33 +00:00
|
|
|
asset = get_object_or_none(Asset, pk=asset_id)
|
|
|
|
try:
|
2019-06-19 03:31:38 +00:00
|
|
|
manger = AssetUserManager()
|
2019-07-04 07:36:57 +00:00
|
|
|
instance = manger.get(username, asset, prefer=prefer)
|
2019-03-18 02:15:33 +00:00
|
|
|
except Exception as e:
|
2019-08-21 12:27:21 +00:00
|
|
|
raise Http404("Not found")
|
2019-03-18 02:15:33 +00:00
|
|
|
else:
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
class AssetUserTestConnectiveApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
|
|
|
Test asset users connective
|
|
|
|
"""
|
2019-06-19 03:31:38 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2019-06-27 13:43:10 +00:00
|
|
|
serializer_class = serializers.TaskIDSerializer
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
def get_asset_users(self):
|
|
|
|
username = self.request.GET.get('username')
|
|
|
|
asset_id = self.request.GET.get('asset_id')
|
2019-07-04 13:17:39 +00:00
|
|
|
prefer = self.request.GET.get("prefer")
|
2019-03-18 02:15:33 +00:00
|
|
|
asset = get_object_or_none(Asset, pk=asset_id)
|
2019-06-19 03:31:38 +00:00
|
|
|
manager = AssetUserManager()
|
2019-07-04 13:17:39 +00:00
|
|
|
asset_users = manager.filter(username=username, assets=[asset], prefer=prefer)
|
2019-03-18 02:15:33 +00:00
|
|
|
return asset_users
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
asset_users = self.get_asset_users()
|
2019-06-19 03:31:38 +00:00
|
|
|
prefer = self.request.GET.get("prefer")
|
|
|
|
kwargs = {}
|
|
|
|
if prefer == "admin_user":
|
|
|
|
kwargs["run_as_admin"] = True
|
|
|
|
task = test_asset_users_connectivity_manual.delay(asset_users, **kwargs)
|
2019-03-18 02:15:33 +00:00
|
|
|
return Response({"task": task.id})
|
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class AssetUserPushApi(generics.CreateAPIView):
|
|
|
|
"""
|
|
|
|
Test asset users connective
|
|
|
|
"""
|
|
|
|
serializer_class = serializers.AssetUserPushSerializer
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
asset = serializer.validated_data["asset"]
|
|
|
|
username = serializer.validated_data["username"]
|
|
|
|
pass
|