jumpserver/apps/assets/api.py

111 lines
3.7 KiB
Python
Raw Normal View History

2016-08-09 09:27:37 +00:00
# ~*~ coding: utf-8 ~*~
2016-09-11 01:50:42 +00:00
2016-11-06 13:29:04 +00:00
from rest_framework import viewsets, generics, mixins
2016-11-01 11:31:35 +00:00
from rest_framework.response import Response
2016-11-01 09:21:16 +00:00
from rest_framework.views import APIView
from rest_framework_bulk import BulkListSerializer, BulkSerializerMixin, ListBulkCreateUpdateDestroyAPIView
2016-11-06 13:29:04 +00:00
from django.shortcuts import get_object_or_404
2016-11-01 09:21:16 +00:00
2016-10-21 13:14:49 +00:00
from common.mixins import BulkDeleteApiMixin
2016-11-01 11:31:35 +00:00
from common.utils import get_object_or_none, signer
from .hands import IsSuperUserOrTerminalUser, IsSuperUser
2016-11-06 13:29:04 +00:00
from .models import AssetGroup, Asset, IDC, SystemUser, AdminUser
from . import serializers
2016-09-04 09:43:03 +00:00
2016-09-03 11:05:50 +00:00
class AssetViewSet(viewsets.ModelViewSet):
2016-10-28 13:19:37 +00:00
"""API endpoint that allows Asset to be viewed or edited."""
2016-09-03 11:05:50 +00:00
queryset = Asset.objects.all()
2016-11-06 13:29:04 +00:00
serializer_class = serializers.AssetSerializer
2016-09-03 11:05:50 +00:00
2016-11-06 14:45:26 +00:00
def get_queryset(self):
queryset = super(AssetViewSet, self).get_queryset()
2016-11-17 11:28:45 +00:00
idc_id = self.request.query_params.get('idc_id', '')
asset_group_id = self.request.query_params.get('asset_group_id', '')
if idc_id:
queryset = queryset.filter(idc__id=idc_id)
if asset_group_id:
queryset = queryset.filter(groups__id=asset_group_id)
2016-11-06 14:45:26 +00:00
return queryset
2016-09-04 09:43:03 +00:00
2016-11-09 16:18:57 +00:00
class AssetGroupViewSet(viewsets.ModelViewSet):
""" API endpoint that allows AssetGroup to be viewed or edited.
some other comment
"""
queryset = AssetGroup.objects.all()
serializer_class = serializers.AssetGroupSerializer
2016-11-06 13:29:04 +00:00
class IDCViewSet(viewsets.ModelViewSet):
2016-10-28 13:19:37 +00:00
"""API endpoint that allows IDC to be viewed or edited."""
2016-09-03 11:05:50 +00:00
queryset = IDC.objects.all()
2016-11-06 13:29:04 +00:00
serializer_class = serializers.IDCSerializer
2016-11-01 11:31:35 +00:00
permission_classes = (IsSuperUser,)
2016-09-22 10:31:04 +00:00
2016-11-06 13:29:04 +00:00
class AdminUserViewSet(viewsets.ModelViewSet):
queryset = AdminUser.objects.all()
serializer_class = serializers.AdminUserSerializer
permission_classes = (IsSuperUser,)
class SystemUserViewSet(viewsets.ModelViewSet):
queryset = SystemUser.objects.all()
serializer_class = serializers.SystemUserSerializer
permission_classes = (IsSuperUser,)
2016-11-06 14:45:26 +00:00
# class IDCAssetsApi(generics.ListAPIView):
# model = IDC
# serializer_class = serializers.AssetSerializer
#
# def get(self, request, *args, **kwargs):
# filter_kwargs = {self.lookup_field: self.kwargs[self.lookup_field]}
# self.object = get_object_or_404(self.model, **filter_kwargs)
# return super(IDCAssetsApi, self).get(request, *args, **kwargs)
#
# def get_queryset(self):
# return self.object.assets.all()
2016-11-06 13:29:04 +00:00
2016-10-28 13:19:37 +00:00
2016-10-21 13:14:49 +00:00
class AssetListUpdateApi(BulkDeleteApiMixin, ListBulkCreateUpdateDestroyAPIView):
queryset = Asset.objects.all()
2016-11-09 16:18:57 +00:00
serializer_class = serializers.AssetSerializer
2016-11-01 11:31:35 +00:00
permission_classes = (IsSuperUser,)
2016-11-01 09:21:16 +00:00
2016-11-01 11:31:35 +00:00
class SystemUserAuthApi(APIView):
permission_classes = (IsSuperUserOrTerminalUser,)
2016-11-01 09:21:16 +00:00
def get(self, request, *args, **kwargs):
2016-11-01 11:31:35 +00:00
system_user_id = request.query_params.get('system_user_id', -1)
system_user_username = request.query_params.get('system_user_username', '')
2016-11-01 09:21:16 +00:00
2016-11-01 11:31:35 +00:00
system_user = get_object_or_none(SystemUser, id=system_user_id, username=system_user_username)
2016-11-01 09:21:16 +00:00
if system_user:
2016-11-02 11:44:11 +00:00
if system_user.password:
password = signer.sign(system_user.password)
else:
password = signer.sign('')
if system_user.private_key:
private_key = signer.sign(system_user.private_key)
else:
private_key = signer.sign(None)
2016-11-01 11:31:35 +00:00
response = {
'id': system_user.id,
'password': password,
'private_key': private_key,
}
return Response(response)
else:
return Response({'msg': 'error system user id or username'}, status=401)
2016-10-28 13:19:37 +00:00