2018-02-06 10:32:02 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2018-09-03 03:24:25 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2018-02-06 10:32:02 +00:00
|
|
|
from rest_framework.response import Response
|
2018-07-23 04:55:13 +00:00
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
from common.utils import get_logger
|
2019-11-13 03:18:29 +00:00
|
|
|
from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser, IsAppUser
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
2019-10-18 07:05:45 +00:00
|
|
|
from orgs.mixins import generics
|
2020-03-12 08:24:38 +00:00
|
|
|
from orgs.utils import tmp_to_org
|
2018-09-03 03:24:25 +00:00
|
|
|
from ..models import SystemUser, Asset
|
2018-02-06 10:32:02 +00:00
|
|
|
from .. import serializers
|
2020-03-12 08:24:38 +00:00
|
|
|
from ..serializers import SystemUserWithAuthInfoSerializer
|
2019-08-21 12:27:21 +00:00
|
|
|
from ..tasks import (
|
|
|
|
push_system_user_to_assets_manual, test_system_user_connectivity_manual,
|
2020-03-12 08:24:38 +00:00
|
|
|
push_system_user_a_asset_manual,
|
2019-08-21 12:27:21 +00:00
|
|
|
)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
__all__ = [
|
2019-03-18 02:15:33 +00:00
|
|
|
'SystemUserViewSet', 'SystemUserAuthInfoApi', 'SystemUserAssetAuthInfoApi',
|
2020-03-12 08:24:38 +00:00
|
|
|
'SystemUserCommandFilterRuleListApi', 'SystemUserTaskApi',
|
2018-02-06 10:32:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-07-04 07:36:57 +00:00
|
|
|
class SystemUserViewSet(OrgBulkModelViewSet):
|
2018-02-06 10:32:02 +00:00
|
|
|
"""
|
|
|
|
System user api set, for add,delete,update,list,retrieve resource
|
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = SystemUser
|
2018-10-31 07:31:09 +00:00
|
|
|
filter_fields = ("name", "username")
|
|
|
|
search_fields = filter_fields
|
2018-02-06 10:32:02 +00:00
|
|
|
serializer_class = serializers.SystemUserSerializer
|
2020-03-12 08:24:38 +00:00
|
|
|
serializer_classes = {
|
|
|
|
'default': serializers.SystemUserSerializer,
|
|
|
|
'list': serializers.SystemUserListSerializer,
|
|
|
|
}
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2018-10-31 07:31:09 +00:00
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
|
2018-05-17 03:39:04 +00:00
|
|
|
class SystemUserAuthInfoApi(generics.RetrieveUpdateDestroyAPIView):
|
2018-02-06 10:32:02 +00:00
|
|
|
"""
|
|
|
|
Get system user auth info
|
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = SystemUser
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2020-03-12 08:24:38 +00:00
|
|
|
serializer_class = SystemUserWithAuthInfoSerializer
|
2018-02-06 10:32:02 +00:00
|
|
|
|
2018-05-17 03:39:04 +00:00
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
instance = self.get_object()
|
|
|
|
instance.clear_auth()
|
|
|
|
return Response(status=204)
|
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
|
2019-03-18 02:15:33 +00:00
|
|
|
class SystemUserAssetAuthInfoApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
|
|
|
Get system user with asset auth info
|
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = SystemUser
|
2020-03-12 08:24:38 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
serializer_class = SystemUserWithAuthInfoSerializer
|
|
|
|
|
|
|
|
def get_exception_handler(self):
|
|
|
|
def handler(e, context):
|
|
|
|
return Response({"error": str(e)}, status=400)
|
|
|
|
return handler
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
instance = super().get_object()
|
2020-03-12 08:24:38 +00:00
|
|
|
username = instance.username
|
|
|
|
if instance.username_same_with_user:
|
|
|
|
username = self.request.query_params.get("username")
|
|
|
|
asset_id = self.kwargs.get('aid')
|
|
|
|
asset = get_object_or_404(Asset, pk=asset_id)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
with tmp_to_org(asset.org_id):
|
|
|
|
instance.load_asset_special_auth(asset=asset, username=username)
|
|
|
|
return instance
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class SystemUserTaskApi(generics.CreateAPIView):
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2020-03-12 08:24:38 +00:00
|
|
|
serializer_class = serializers.SystemUserTaskSerializer
|
|
|
|
|
|
|
|
def do_push(self, system_user, asset=None):
|
|
|
|
if asset is None:
|
|
|
|
task = push_system_user_to_assets_manual.delay(system_user)
|
|
|
|
else:
|
|
|
|
username = self.request.query_params.get('username')
|
|
|
|
task = push_system_user_a_asset_manual.delay(
|
|
|
|
system_user, asset, username=username
|
|
|
|
)
|
|
|
|
return task
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def do_test(system_user, asset=None):
|
2018-12-18 09:28:45 +00:00
|
|
|
task = test_system_user_connectivity_manual.delay(system_user)
|
2020-03-12 08:24:38 +00:00
|
|
|
return task
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
pk = self.kwargs.get('pk')
|
|
|
|
return get_object_or_404(SystemUser, pk=pk)
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
action = serializer.validated_data["action"]
|
|
|
|
asset = serializer.validated_data.get('asset')
|
2018-09-03 03:24:25 +00:00
|
|
|
system_user = self.get_object()
|
2020-03-12 08:24:38 +00:00
|
|
|
if action == 'push':
|
|
|
|
task = self.do_push(system_user, asset)
|
|
|
|
else:
|
|
|
|
task = self.do_test(system_user, asset)
|
|
|
|
data = getattr(serializer, '_data', {})
|
|
|
|
data["task"] = task.id
|
|
|
|
setattr(serializer, '_data', data)
|
2018-10-10 07:37:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SystemUserCommandFilterRuleListApi(generics.ListAPIView):
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
from ..serializers import CommandFilterRuleSerializer
|
|
|
|
return CommandFilterRuleSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
pk = self.kwargs.get('pk', None)
|
|
|
|
system_user = get_object_or_404(SystemUser, pk=pk)
|
|
|
|
return system_user.cmd_filter_rules
|