def _get_indirect_granted_node_all_assets(self, node) -> QuerySet: """ 此算法依据 `UserAssetGrantedTreeNodeRelation` 的数据查询 1. 查询该节点下的直接授权节点 2. 查询该节点下授权资产关联的节点 """ user = self.user # 查询该节点下的授权节点 granted_nodes = UserAssetGrantedTreeNodeRelation.objects.filter( user=user, node_from=NodeFrom.granted).filter( Q(node_key__startswith=f'{node.key}:')).only( 'node_id', 'node_key') for n in granted_nodes: n.id = n.node_id node_assets = PermNode.get_nodes_all_assets(*granted_nodes) # 查询该节点下的资产授权节点 only_asset_granted_node_ids = UserAssetGrantedTreeNodeRelation.objects.filter( user=user, node_from=NodeFrom.asset).filter( Q(node_key__startswith=f'{node.key}:')).values_list('node_id', flat=True) only_asset_granted_node_ids = list(only_asset_granted_node_ids) if node.node_from == NodeFrom.asset: only_asset_granted_node_ids.append(node.id) assets = Asset.objects.filter( nodes__id__in=only_asset_granted_node_ids, granted_by_permissions__id__in=self.asset_perm_ids).distinct( ).order_by() granted_assets = UnionQuerySet(node_assets, assets) return granted_assets
def get_direct_granted_nodes_assets(self) -> AssetQuerySet: granted_node_ids = AssetPermission.nodes.through.objects.filter( assetpermission_id__in=self.asset_perm_ids ).values_list('node_id', flat=True).distinct() granted_node_ids = list(granted_node_ids) granted_nodes = PermNode.objects.filter(id__in=granted_node_ids).only('id', 'key') queryset = PermNode.get_nodes_all_assets(*granted_nodes) return queryset
def get_node_all_assets(self, id) -> Tuple[PermNode, QuerySet]: node = PermNode.objects.get(id=id) granted_status = node.get_granted_status(self.user) if granted_status == NodeFrom.granted: assets = PermNode.get_nodes_all_assets(node) return node, assets elif granted_status in (NodeFrom.asset, NodeFrom.child): node.use_granted_assets_amount() assets = self._get_indirect_granted_node_all_assets(node) return node, assets else: node.assets_amount = 0 return node, Asset.objects.none()
def get_favorite_node(self): assets_query_utils = UserGrantedAssetsQueryUtils( self.user, self.asset_perm_ids) assets_amount = assets_query_utils.get_favorite_assets().values_list( 'id').count() return PermNode.get_favorite_node(assets_amount)
def get_ungrouped_node(self): assets_util = UserGrantedAssetsQueryUtils(self.user, self.asset_perm_ids) assets_amount = assets_util.get_direct_granted_assets().count() return PermNode.get_ungrouped_node(assets_amount)
def _fill_direct_granted_node_asset_ids_from_mem(self, nodes_key, mapper): org_id = current_org.id for key in nodes_key: asset_ids = PermNode.get_all_asset_ids_by_node_key(org_id, key) mapper[key].update(asset_ids)
def _has_ancestor_granted(node: PermNode): """ 判断一个节点是否有授权过的祖先节点 """ ancestor_keys = set(node.get_ancestor_keys()) return ancestor_keys & granted_key_set