jumpserver/apps/perms/api/user_permission/nodes.py

52 lines
1.2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#
import abc
2022-12-15 08:02:34 +00:00
from rest_framework.generics import ListAPIView
from assets.models import Node
2022-12-15 08:02:34 +00:00
from common.utils import get_logger, lazyproperty
from perms import serializers
from perms.utils import UserPermNodeUtil
from .mixin import SelfOrPKUserMixin
logger = get_logger(__name__)
__all__ = [
'UserAllPermedNodesApi',
2022-12-06 11:53:36 +00:00
'UserPermedNodeChildrenApi',
]
class BaseUserPermedNodesApi(SelfOrPKUserMixin, ListAPIView):
2022-12-15 08:02:34 +00:00
serializer_class = serializers.NodePermedSerializer
def get_queryset(self):
if getattr(self, 'swagger_fake_view', False):
return Node.objects.none()
return self.get_nodes()
@abc.abstractmethod
def get_nodes(self):
return []
@lazyproperty
def query_node_util(self):
return UserPermNodeUtil(self.user)
class UserAllPermedNodesApi(BaseUserPermedNodesApi):
""" 用户授权的节点 """
2022-12-15 08:02:34 +00:00
def get_nodes(self):
return self.query_node_util.get_whole_tree_nodes()
class UserPermedNodeChildrenApi(BaseUserPermedNodesApi):
""" 用户授权的节点下的子节点 """
2022-12-15 08:02:34 +00:00
def get_nodes(self):
key = self.request.query_params.get('key')
nodes = self.query_node_util.get_node_children(key)
return nodes