2019-03-18 02:15:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-03-12 08:24:38 +00:00
|
|
|
from django.conf import settings
|
2019-03-18 02:15:33 +00:00
|
|
|
from rest_framework.response import Response
|
2020-03-12 08:24:38 +00:00
|
|
|
from rest_framework import generics, filters
|
2019-06-19 03:31:38 +00:00
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
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
|
2020-03-12 08:24:38 +00:00
|
|
|
from ..models import Asset, Node, SystemUser
|
2019-03-18 02:15:33 +00:00
|
|
|
from .. import serializers
|
2020-03-12 08:24:38 +00:00
|
|
|
from ..tasks import (
|
|
|
|
test_asset_users_connectivity_manual, push_system_user_a_asset_manual
|
|
|
|
)
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
2020-03-12 08:24:38 +00:00
|
|
|
'AssetUserViewSet', 'AssetUserAuthInfoViewSet', 'AssetUserTaskCreateAPI',
|
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
|
2020-03-12 08:24:38 +00:00
|
|
|
if field == "node_id":
|
|
|
|
value = get_object_or_none(Node, pk=value)
|
|
|
|
kwargs["node"] = value
|
2019-06-19 03:31:38 +00:00
|
|
|
continue
|
2020-03-12 08:24:38 +00:00
|
|
|
elif field == "asset_id":
|
|
|
|
field = "asset"
|
2019-06-19 03:31:38 +00:00
|
|
|
kwargs[field] = value
|
2020-03-12 08:24:38 +00:00
|
|
|
if kwargs:
|
|
|
|
queryset = queryset.filter(**kwargs)
|
|
|
|
logger.debug("Filter {}".format(kwargs))
|
|
|
|
return queryset
|
2019-06-19 03:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetUserSearchBackend(filters.BaseFilterBackend):
|
|
|
|
def filter_queryset(self, request, queryset, view):
|
|
|
|
value = request.GET.get('search')
|
|
|
|
if not value:
|
|
|
|
return queryset
|
2020-03-12 08:24:38 +00:00
|
|
|
queryset = queryset.search(value)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class AssetUserLatestFilterBackend(filters.BaseFilterBackend):
|
|
|
|
def filter_queryset(self, request, queryset, view):
|
|
|
|
latest = request.GET.get('latest') == '1'
|
|
|
|
if latest:
|
|
|
|
queryset = queryset.distinct()
|
|
|
|
return queryset
|
2019-06-19 03:31:38 +00:00
|
|
|
|
|
|
|
|
2019-09-18 14:06:46 +00:00
|
|
|
class AssetUserViewSet(CommonApiMixin, BulkModelViewSet):
|
2020-03-12 08:24:38 +00:00
|
|
|
serializer_classes = {
|
|
|
|
'default': serializers.AssetUserWriteSerializer,
|
|
|
|
'list': serializers.AssetUserReadSerializer,
|
|
|
|
'retrieve': serializers.AssetUserReadSerializer,
|
|
|
|
}
|
2019-06-24 12:39:45 +00:00
|
|
|
permission_classes = [IsOrgAdminOrAppUser]
|
2019-06-19 03:31:38 +00:00
|
|
|
filter_fields = [
|
2020-03-12 08:24:38 +00:00
|
|
|
"id", "ip", "hostname", "username",
|
|
|
|
"asset_id", "node_id",
|
|
|
|
"prefer", "prefer_id",
|
2019-06-19 03:31:38 +00:00
|
|
|
]
|
2020-03-12 08:24:38 +00:00
|
|
|
search_fields = ["ip", "hostname", "username"]
|
|
|
|
filter_backends = [
|
2019-06-19 03:31:38 +00:00
|
|
|
AssetUserFilterBackend, AssetUserSearchBackend,
|
2020-03-12 08:24:38 +00:00
|
|
|
AssetUserLatestFilterBackend,
|
|
|
|
]
|
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
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def get_object(self):
|
|
|
|
pk = self.kwargs.get("pk")
|
|
|
|
queryset = self.get_queryset()
|
|
|
|
obj = queryset.get(id=pk)
|
|
|
|
return obj
|
2019-06-19 03:31:38 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def get_exception_handler(self):
|
|
|
|
def handler(e, context):
|
|
|
|
return Response({"error": str(e)}, status=400)
|
|
|
|
return handler
|
|
|
|
|
|
|
|
def perform_destroy(self, instance):
|
|
|
|
manager = AssetUserManager()
|
|
|
|
manager.delete(instance)
|
2019-06-19 03:31:38 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def get_queryset(self):
|
2019-06-19 03:31:38 +00:00
|
|
|
manager = AssetUserManager()
|
2020-03-12 08:24:38 +00:00
|
|
|
queryset = manager.all()
|
2019-03-18 02:15:33 +00:00
|
|
|
return queryset
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class AssetUserAuthInfoViewSet(AssetUserViewSet):
|
|
|
|
serializer_classes = {"default": serializers.AssetUserAuthInfoSerializer}
|
2019-06-19 03:31:38 +00:00
|
|
|
http_method_names = ['get']
|
2019-10-22 03:31:15 +00:00
|
|
|
permission_classes = [IsOrgAdminOrAppUser]
|
|
|
|
|
|
|
|
def get_permissions(self):
|
2019-12-05 07:09:25 +00:00
|
|
|
if settings.SECURITY_VIEW_AUTH_NEED_MFA:
|
2019-10-22 03:31:15 +00:00
|
|
|
self.permission_classes = [IsOrgAdminOrAppUser, NeedMFAVerify]
|
|
|
|
return super().get_permissions()
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class AssetUserTaskCreateAPI(generics.CreateAPIView):
|
2019-06-19 03:31:38 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2020-03-12 08:24:38 +00:00
|
|
|
serializer_class = serializers.AssetUserTaskSerializer
|
|
|
|
filter_backends = AssetUserViewSet.filter_backends
|
|
|
|
filter_fields = AssetUserViewSet.filter_fields
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
def get_asset_users(self):
|
2019-06-19 03:31:38 +00:00
|
|
|
manager = AssetUserManager()
|
2020-03-12 08:24:38 +00:00
|
|
|
queryset = manager.all()
|
|
|
|
for cls in self.filter_backends:
|
|
|
|
queryset = cls().filter_queryset(self.request, queryset, self)
|
|
|
|
return list(queryset)
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def perform_create(self, serializer):
|
2019-03-18 02:15:33 +00:00
|
|
|
asset_users = self.get_asset_users()
|
2020-03-12 08:24:38 +00:00
|
|
|
# action = serializer.validated_data["action"]
|
|
|
|
# only this
|
|
|
|
# if action == "test":
|
|
|
|
task = test_asset_users_connectivity_manual.delay(asset_users)
|
|
|
|
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)
|
|
|
|
return handler
|