2019-03-04 12:45:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-08-16 15:08:58 +00:00
|
|
|
from itertools import chain
|
2019-03-04 12:45:57 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
from rest_framework.generics import ListAPIView
|
|
|
|
from rest_framework.response import Response
|
2019-03-04 12:45:57 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
from common.utils import lazyproperty
|
|
|
|
from perms.models import AssetPermission
|
|
|
|
from assets.models import Asset, Node
|
2022-08-22 10:32:33 +00:00
|
|
|
from . import user_permission as uapi
|
2020-08-16 15:08:58 +00:00
|
|
|
from perms import serializers
|
2022-10-18 08:42:32 +00:00
|
|
|
from perms.utils import PermAccountUtil
|
2020-08-16 15:08:58 +00:00
|
|
|
from assets.api.mixin import SerializeToTreeNodeMixin
|
|
|
|
from users.models import UserGroup
|
2019-03-04 12:45:57 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'UserGroupGrantedAssetsApi', 'UserGroupGrantedNodesApi',
|
2020-08-16 15:08:58 +00:00
|
|
|
'UserGroupGrantedNodeAssetsApi',
|
2019-08-31 04:07:03 +00:00
|
|
|
'UserGroupGrantedNodeChildrenAsTreeApi',
|
2019-03-04 12:45:57 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
class UserGroupGrantedAssetsApi(ListAPIView):
|
|
|
|
serializer_class = serializers.AssetGrantedSerializer
|
|
|
|
only_fields = serializers.AssetGrantedSerializer.Meta.only_fields
|
2022-09-21 03:17:14 +00:00
|
|
|
filterset_fields = ['name', 'address', 'id', 'comment']
|
|
|
|
search_fields = ['name', 'address', 'comment']
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2022-02-28 11:28:58 +00:00
|
|
|
'list': 'perms.view_usergroupassets',
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-02-21 10:51:11 +00:00
|
|
|
user_group_id = self.kwargs.get('pk')
|
|
|
|
if not user_group_id:
|
|
|
|
return Asset.objects.none()
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
asset_perm_ids = list(AssetPermission.objects.valid().filter(
|
2020-11-22 06:09:38 +00:00
|
|
|
user_groups__id=user_group_id
|
|
|
|
).distinct().values_list('id', flat=True))
|
|
|
|
|
|
|
|
granted_node_keys = Node.objects.filter(
|
2021-03-08 02:08:51 +00:00
|
|
|
granted_by_permissions__id__in=asset_perm_ids,
|
2020-11-22 06:09:38 +00:00
|
|
|
).distinct().values_list('key', flat=True)
|
|
|
|
|
|
|
|
granted_q = Q()
|
|
|
|
for _key in granted_node_keys:
|
|
|
|
granted_q |= Q(nodes__key__startswith=f'{_key}:')
|
|
|
|
granted_q |= Q(nodes__key=_key)
|
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
granted_q |= Q(granted_by_permissions__id__in=asset_perm_ids)
|
2020-11-22 06:09:38 +00:00
|
|
|
|
|
|
|
assets = Asset.objects.filter(
|
|
|
|
granted_q
|
2020-08-16 15:08:58 +00:00
|
|
|
).distinct().only(
|
|
|
|
*self.only_fields
|
|
|
|
)
|
2020-11-22 06:09:38 +00:00
|
|
|
return assets
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserGroupGrantedNodeAssetsApi(ListAPIView):
|
|
|
|
serializer_class = serializers.AssetGrantedSerializer
|
|
|
|
only_fields = serializers.AssetGrantedSerializer.Meta.only_fields
|
2022-09-21 03:17:14 +00:00
|
|
|
filterset_fields = ['name', 'address', 'id', 'comment']
|
|
|
|
search_fields = ['name', 'address', 'comment']
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2022-02-28 11:28:58 +00:00
|
|
|
'list': 'perms.view_usergroupassets',
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-12-11 09:21:52 +00:00
|
|
|
if getattr(self, 'swagger_fake_view', False):
|
|
|
|
return Asset.objects.none()
|
2020-08-16 15:08:58 +00:00
|
|
|
user_group_id = self.kwargs.get('pk', '')
|
|
|
|
node_id = self.kwargs.get("node_id")
|
|
|
|
node = Node.objects.get(id=node_id)
|
|
|
|
|
|
|
|
granted = AssetPermission.objects.filter(
|
|
|
|
user_groups__id=user_group_id,
|
|
|
|
nodes__id=node_id
|
2020-11-22 06:09:38 +00:00
|
|
|
).valid().exists()
|
2020-08-16 15:08:58 +00:00
|
|
|
if granted:
|
|
|
|
assets = Asset.objects.filter(
|
|
|
|
Q(nodes__key__startswith=f'{node.key}:') |
|
|
|
|
Q(nodes__key=node.key)
|
|
|
|
)
|
|
|
|
return assets
|
|
|
|
else:
|
2021-03-08 02:08:51 +00:00
|
|
|
asset_perm_ids = list(AssetPermission.objects.valid().filter(
|
2020-11-22 06:09:38 +00:00
|
|
|
user_groups__id=user_group_id
|
|
|
|
).distinct().values_list('id', flat=True))
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
granted_node_keys = Node.objects.filter(
|
2021-03-08 02:08:51 +00:00
|
|
|
granted_by_permissions__id__in=asset_perm_ids,
|
2020-08-16 15:08:58 +00:00
|
|
|
key__startswith=f'{node.key}:'
|
|
|
|
).distinct().values_list('key', flat=True)
|
|
|
|
|
|
|
|
granted_node_q = Q()
|
|
|
|
for _key in granted_node_keys:
|
|
|
|
granted_node_q |= Q(nodes__key__startswith=f'{_key}:')
|
|
|
|
granted_node_q |= Q(nodes__key=_key)
|
|
|
|
|
|
|
|
granted_asset_q = (
|
2021-03-08 02:08:51 +00:00
|
|
|
Q(granted_by_permissions__id__in=asset_perm_ids) &
|
2020-08-16 15:08:58 +00:00
|
|
|
(
|
|
|
|
Q(nodes__key__startswith=f'{node.key}:') |
|
|
|
|
Q(nodes__key=node.key)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
assets = Asset.objects.filter(
|
|
|
|
granted_node_q | granted_asset_q
|
|
|
|
).distinct()
|
|
|
|
return assets
|
|
|
|
|
|
|
|
|
|
|
|
class UserGroupGrantedNodesApi(ListAPIView):
|
|
|
|
serializer_class = serializers.NodeGrantedSerializer
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2022-02-28 11:28:58 +00:00
|
|
|
'list': 'perms.view_usergroupassets',
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-02-21 10:51:11 +00:00
|
|
|
user_group_id = self.kwargs.get('pk')
|
|
|
|
if not user_group_id:
|
|
|
|
return Node.objects.none()
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
nodes = Node.objects.filter(
|
|
|
|
Q(granted_by_permissions__user_groups__id=user_group_id) |
|
|
|
|
Q(assets__granted_by_permissions__user_groups__id=user_group_id)
|
|
|
|
)
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
|
|
|
|
class UserGroupGrantedNodeChildrenAsTreeApi(SerializeToTreeNodeMixin, ListAPIView):
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2022-02-28 11:28:58 +00:00
|
|
|
'list': 'perms.view_usergroupassets',
|
|
|
|
'GET': 'perms.view_usergroupassets',
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
def get_children_nodes(self, parent_key):
|
|
|
|
return Node.objects.filter(parent_key=parent_key)
|
|
|
|
|
|
|
|
def add_children_key(self, node_key, key, key_set):
|
|
|
|
if key.startswith(f'{node_key}:'):
|
|
|
|
try:
|
|
|
|
end = key.index(':', len(node_key) + 1)
|
|
|
|
key_set.add(key[:end])
|
|
|
|
except ValueError:
|
|
|
|
key_set.add(key)
|
|
|
|
|
|
|
|
def get_nodes(self):
|
|
|
|
group_id = self.kwargs.get('pk')
|
|
|
|
node_key = self.request.query_params.get('key', None)
|
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
asset_perm_ids = list(AssetPermission.objects.valid().filter(
|
2020-11-22 06:09:38 +00:00
|
|
|
user_groups__id=group_id
|
|
|
|
).distinct().values_list('id', flat=True))
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
granted_keys = Node.objects.filter(
|
2021-03-08 02:08:51 +00:00
|
|
|
granted_by_permissions__id__in=asset_perm_ids
|
2020-08-16 15:08:58 +00:00
|
|
|
).values_list('key', flat=True)
|
|
|
|
|
|
|
|
asset_granted_keys = Node.objects.filter(
|
2021-03-08 02:08:51 +00:00
|
|
|
assets__granted_by_permissions__id__in=asset_perm_ids
|
2020-08-16 15:08:58 +00:00
|
|
|
).values_list('key', flat=True)
|
|
|
|
|
|
|
|
if node_key is None:
|
|
|
|
root_keys = set()
|
|
|
|
for _key in chain(granted_keys, asset_granted_keys):
|
|
|
|
root_keys.add(_key.split(':', 1)[0])
|
|
|
|
return Node.objects.filter(key__in=root_keys)
|
|
|
|
else:
|
|
|
|
children_keys = set()
|
|
|
|
for _key in granted_keys:
|
|
|
|
# 判断当前节点是否是授权节点
|
|
|
|
if node_key == _key:
|
|
|
|
return self.get_children_nodes(node_key)
|
|
|
|
# 判断当前节点有没有授权的父节点
|
|
|
|
if node_key.startswith(f'{_key}:'):
|
|
|
|
return self.get_children_nodes(node_key)
|
|
|
|
self.add_children_key(node_key, _key, children_keys)
|
|
|
|
|
|
|
|
for _key in asset_granted_keys:
|
|
|
|
self.add_children_key(node_key, _key, children_keys)
|
|
|
|
|
|
|
|
return Node.objects.filter(key__in=children_keys)
|
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
nodes = self.get_nodes()
|
|
|
|
nodes = self.serialize_nodes(nodes)
|
|
|
|
return Response(data=nodes)
|