jumpserver/apps/assets/api/system_user.py

85 lines
2.8 KiB
Python
Raw Normal View History

2018-02-06 10:32:02 +00:00
# ~*~ coding: utf-8 ~*~
# Copyright (C) 2014-2018 Beijing DuiZhan Technology Co.,Ltd. All Rights Reserved.
#
# Licensed under the GNU General Public License v2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.gnu.org/licenses/gpl-2.0.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from rest_framework import generics
from rest_framework.response import Response
from rest_framework_bulk import BulkModelViewSet
2018-07-23 04:55:13 +00:00
2018-02-06 10:32:02 +00:00
from common.utils import get_logger
2018-07-23 04:55:13 +00:00
from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser
2018-02-06 10:32:02 +00:00
from ..models import SystemUser
from .. import serializers
from ..tasks import push_system_user_to_assets_manual, \
2018-02-06 10:32:02 +00:00
test_system_user_connectability_manual
logger = get_logger(__file__)
__all__ = [
'SystemUserViewSet', 'SystemUserAuthInfoApi',
'SystemUserPushApi', 'SystemUserTestConnectiveApi'
]
class SystemUserViewSet(BulkModelViewSet):
"""
System user api set, for add,delete,update,list,retrieve resource
"""
2018-07-14 16:55:05 +00:00
queryset = SystemUser.objects.all()
2018-02-06 10:32:02 +00:00
serializer_class = serializers.SystemUserSerializer
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdminOrAppUser,)
2018-02-06 10:32:02 +00:00
class SystemUserAuthInfoApi(generics.RetrieveUpdateDestroyAPIView):
2018-02-06 10:32:02 +00:00
"""
Get system user auth info
"""
2018-07-14 16:55:05 +00:00
queryset = SystemUser.objects.all()
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdminOrAppUser,)
serializer_class = serializers.SystemUserAuthSerializer
2018-02-06 10:32:02 +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
class SystemUserPushApi(generics.RetrieveAPIView):
"""
Push system user to cluster assets api
"""
2018-07-14 16:55:05 +00:00
queryset = SystemUser.objects.all()
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdmin,)
2018-02-06 10:32:02 +00:00
def retrieve(self, request, *args, **kwargs):
system_user = self.get_object()
2018-05-09 09:35:46 +00:00
nodes = system_user.nodes.all()
for node in nodes:
system_user.assets.add(*tuple(node.get_all_assets()))
task = push_system_user_to_assets_manual.delay(system_user)
return Response({"task": task.id})
2018-02-06 10:32:02 +00:00
class SystemUserTestConnectiveApi(generics.RetrieveAPIView):
"""
Push system user to cluster assets api
"""
2018-07-14 16:55:05 +00:00
queryset = SystemUser.objects.all()
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdmin,)
2018-02-06 10:32:02 +00:00
def retrieve(self, request, *args, **kwargs):
system_user = self.get_object()
task = test_system_user_connectability_manual.delay(system_user)
return Response({"task": task.id})