jumpserver/apps/assets/api.py

97 lines
2.9 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-09-03 11:05:50 +00:00
from rest_framework import serializers
2016-11-01 09:21:16 +00:00
from rest_framework import viewsets, serializers, generics
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-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
from .models import AssetGroup, Asset, IDC, SystemUser
2016-11-01 09:21:16 +00:00
from .serializers import AssetBulkUpdateSerializer
2016-09-03 11:05:50 +00:00
class AssetGroupSerializer(serializers.ModelSerializer):
class Meta:
model = AssetGroup
2016-09-11 01:50:42 +00:00
2016-09-03 11:05:50 +00:00
class AssetSerializer(serializers.ModelSerializer):
class Meta:
model = Asset
2016-09-11 01:50:42 +00:00
# fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
2016-09-03 11:05:50 +00:00
class IDCSerializer(serializers.ModelSerializer):
class Meta:
model = IDC
2016-09-11 01:50:42 +00:00
# fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
2016-09-04 09:43:03 +00:00
2016-09-03 11:05:50 +00:00
class AssetGroupViewSet(viewsets.ModelViewSet):
2016-09-11 01:50:42 +00:00
""" API endpoint that allows AssetGroup to be viewed or edited.
some other comment
2016-09-03 11:05:50 +00:00
"""
queryset = AssetGroup.objects.all()
serializer_class = AssetGroupSerializer
2016-09-04 09:43:03 +00:00
2016-09-03 11:05:50 +00:00
class AssetViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows Asset to be viewed or edited.
"""
queryset = Asset.objects.all()
serializer_class = AssetSerializer
2016-09-04 09:43:03 +00:00
2016-09-22 10:31:04 +00:00
class IDCViewSet(viewsets.ReadOnlyModelViewSet):
2016-09-03 11:05:50 +00:00
"""
API endpoint that allows IDC to be viewed or edited.
"""
queryset = IDC.objects.all()
2016-09-22 10:31:04 +00:00
serializer_class = IDCSerializer
2016-11-01 11:31:35 +00:00
permission_classes = (IsSuperUser,)
2016-09-22 10:31:04 +00:00
2016-11-01 09:21:16 +00:00
2016-10-21 13:14:49 +00:00
class AssetListUpdateApi(BulkDeleteApiMixin, ListBulkCreateUpdateDestroyAPIView):
queryset = Asset.objects.all()
2016-11-01 09:21:16 +00:00
serializer_class = AssetBulkUpdateSerializer
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-11-01 09:21:16 +00:00