2019-09-17 04:34:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-09-27 08:02:44 +00:00
|
|
|
import abc
|
2020-11-17 10:27:09 +00:00
|
|
|
from django.conf import settings
|
2019-09-17 04:34:47 +00:00
|
|
|
from rest_framework.generics import (
|
2020-08-16 15:08:58 +00:00
|
|
|
ListAPIView
|
2019-09-17 04:34:47 +00:00
|
|
|
)
|
2020-08-16 15:08:58 +00:00
|
|
|
from rest_framework.response import Response
|
2020-09-27 08:02:44 +00:00
|
|
|
from rest_framework.request import Request
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
from assets.api.mixin import SerializeToTreeNodeMixin
|
2019-09-17 04:34:47 +00:00
|
|
|
from common.utils import get_logger
|
2020-09-27 08:02:44 +00:00
|
|
|
from .mixin import ForAdminMixin, ForUserMixin, UserNodeGrantStatusDispatchMixin
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms.hands import Node, User
|
|
|
|
from perms import serializers
|
|
|
|
from perms.utils.asset.user_permission import (
|
2020-09-27 08:02:44 +00:00
|
|
|
get_indirect_granted_node_children,
|
|
|
|
get_user_granted_nodes_list_via_mapping_node,
|
|
|
|
get_top_level_granted_nodes,
|
2020-11-17 10:27:09 +00:00
|
|
|
rebuild_user_tree_if_need, get_favorite_node,
|
|
|
|
get_ungrouped_node
|
2020-09-27 08:02:44 +00:00
|
|
|
)
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
__all__ = [
|
2020-08-16 15:08:58 +00:00
|
|
|
'UserGrantedNodesForAdminApi',
|
|
|
|
'MyGrantedNodesApi',
|
|
|
|
'MyGrantedNodesAsTreeApi',
|
|
|
|
'UserGrantedNodeChildrenForAdminApi',
|
|
|
|
'MyGrantedNodeChildrenApi',
|
|
|
|
'UserGrantedNodeChildrenAsTreeForAdminApi',
|
|
|
|
'MyGrantedNodeChildrenAsTreeApi',
|
2020-09-27 08:02:44 +00:00
|
|
|
'BaseGrantedNodeAsTreeApi',
|
|
|
|
'UserGrantedNodesMixin',
|
2019-09-17 04:34:47 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class _GrantedNodeStructApi(ListAPIView, metaclass=abc.ABCMeta):
|
|
|
|
@property
|
2020-08-16 15:08:58 +00:00
|
|
|
def user(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_nodes(self):
|
|
|
|
# 不使用 `get_queryset` 单独定义 `get_nodes` 的原因是
|
|
|
|
# `get_nodes` 返回的不一定是 `queryset`
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class NodeChildrenMixin:
|
|
|
|
def get_children(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_nodes(self):
|
|
|
|
nodes = self.get_children()
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
|
|
|
|
class BaseGrantedNodeApi(_GrantedNodeStructApi, metaclass=abc.ABCMeta):
|
2020-08-16 15:08:58 +00:00
|
|
|
serializer_class = serializers.NodeGrantedSerializer
|
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
2020-09-27 11:47:13 +00:00
|
|
|
rebuild_user_tree_if_need(request, self.user)
|
2020-08-16 15:08:58 +00:00
|
|
|
nodes = self.get_nodes()
|
|
|
|
serializer = self.get_serializer(nodes, many=True)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class BaseNodeChildrenApi(NodeChildrenMixin, BaseGrantedNodeApi, metaclass=abc.ABCMeta):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class BaseGrantedNodeAsTreeApi(SerializeToTreeNodeMixin, _GrantedNodeStructApi, metaclass=abc.ABCMeta):
|
2020-09-27 11:47:13 +00:00
|
|
|
def list(self, request: Request, *args, **kwargs):
|
|
|
|
rebuild_user_tree_if_need(request, self.user)
|
2020-08-16 15:08:58 +00:00
|
|
|
nodes = self.get_nodes()
|
|
|
|
nodes = self.serialize_nodes(nodes, with_asset_amount=True)
|
|
|
|
return Response(data=nodes)
|
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class BaseNodeChildrenAsTreeApi(NodeChildrenMixin, BaseGrantedNodeAsTreeApi, metaclass=abc.ABCMeta):
|
|
|
|
pass
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
|
|
|
|
class UserGrantedNodeChildrenMixin(UserNodeGrantStatusDispatchMixin):
|
|
|
|
user: User
|
|
|
|
request: Request
|
|
|
|
|
|
|
|
def get_children(self):
|
2020-08-16 15:08:58 +00:00
|
|
|
user = self.user
|
|
|
|
key = self.request.query_params.get('key')
|
|
|
|
|
|
|
|
if not key:
|
2020-09-27 08:02:44 +00:00
|
|
|
nodes = list(get_top_level_granted_nodes(user))
|
2020-08-16 15:08:58 +00:00
|
|
|
else:
|
2020-09-27 08:02:44 +00:00
|
|
|
nodes = self.dispatch_get_data(key, user)
|
2020-08-16 15:08:58 +00:00
|
|
|
return nodes
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
def get_data_on_node_direct_granted(self, key):
|
2020-08-16 15:08:58 +00:00
|
|
|
return Node.objects.filter(parent_key=key)
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
def get_data_on_node_indirect_granted(self, key):
|
|
|
|
nodes = get_indirect_granted_node_children(self.user, key)
|
2020-08-16 15:08:58 +00:00
|
|
|
return nodes
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
def get_data_on_node_not_granted(self, key):
|
|
|
|
return Node.objects.none()
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
class UserGrantedNodesMixin:
|
2019-09-17 04:34:47 +00:00
|
|
|
"""
|
2020-08-16 15:08:58 +00:00
|
|
|
查询用户授权的所有节点 直接授权节点 + 授权资产关联的节点
|
2019-09-17 04:34:47 +00:00
|
|
|
"""
|
2020-09-27 08:02:44 +00:00
|
|
|
user: User
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
def get_nodes(self):
|
2020-11-17 10:27:09 +00:00
|
|
|
nodes = []
|
|
|
|
if settings.PERM_SINGLE_ASSET_TO_UNGROUP_NODE:
|
|
|
|
nodes.append(get_ungrouped_node(self.user))
|
|
|
|
nodes.append(get_favorite_node(self.user))
|
|
|
|
nodes.extend(get_user_granted_nodes_list_via_mapping_node(self.user))
|
|
|
|
return nodes
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
# ------------------------------------------
|
|
|
|
# 最终的 api
|
2020-09-27 08:02:44 +00:00
|
|
|
class UserGrantedNodeChildrenForAdminApi(ForAdminMixin, UserGrantedNodeChildrenMixin, BaseNodeChildrenApi):
|
2019-09-17 04:34:47 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class MyGrantedNodeChildrenApi(ForUserMixin, UserGrantedNodeChildrenMixin, BaseNodeChildrenApi):
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class UserGrantedNodeChildrenAsTreeForAdminApi(ForAdminMixin, UserGrantedNodeChildrenMixin, BaseNodeChildrenAsTreeApi):
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class MyGrantedNodeChildrenAsTreeApi(ForUserMixin, UserGrantedNodeChildrenMixin, BaseNodeChildrenAsTreeApi):
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class UserGrantedNodesForAdminApi(ForAdminMixin, UserGrantedNodesMixin, BaseGrantedNodeApi):
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class MyGrantedNodesApi(ForUserMixin, UserGrantedNodesMixin, BaseGrantedNodeApi):
|
2019-09-17 04:34:47 +00:00
|
|
|
pass
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
class MyGrantedNodesAsTreeApi(ForUserMixin, UserGrantedNodesMixin, BaseGrantedNodeAsTreeApi):
|
2020-08-16 15:08:58 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
# ------------------------------------------
|