From 2f8042141c206821f55df35f96795829b7b4817a Mon Sep 17 00:00:00 2001 From: xinwen Date: Tue, 23 Mar 2021 18:36:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8E=88=E6=9D=83=E6=A0=91=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/perms/utils/asset/user_permission.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/apps/perms/utils/asset/user_permission.py b/apps/perms/utils/asset/user_permission.py index 739a4191e..7f3a0941f 100644 --- a/apps/perms/utils/asset/user_permission.py +++ b/apps/perms/utils/asset/user_permission.py @@ -488,11 +488,12 @@ class UserGrantedAssetsQueryUtils(UserGrantedUtilsBase): if granted_status == NodeFrom.granted: assets = Asset.objects.order_by().filter(nodes__id=node.id) - return assets elif granted_status == NodeFrom.asset: - return self._get_indirect_granted_node_assets(node.id) + assets = self._get_indirect_granted_node_assets(node.id) else: - return Asset.objects.none() + assets = Asset.objects.none() + assets = assets.order_by('hostname') + return assets def _get_indirect_granted_node_assets(self, id) -> AssetQuerySet: assets = Asset.objects.order_by().filter(nodes__id=id).distinct() & self.get_direct_granted_assets() @@ -538,6 +539,10 @@ class UserGrantedAssetsQueryUtils(UserGrantedUtilsBase): class UserGrantedNodesQueryUtils(UserGrantedUtilsBase): + def sort(self, nodes): + nodes = sorted(nodes, key=lambda x: x.value) + return nodes + def get_node_children(self, key): if not key: return self.get_top_level_nodes() @@ -545,11 +550,13 @@ class UserGrantedNodesQueryUtils(UserGrantedUtilsBase): node = PermNode.objects.get(key=key) granted_status = node.get_granted_status(self.user) if granted_status == NodeFrom.granted: - return PermNode.objects.filter(parent_key=key) + nodes = PermNode.objects.filter(parent_key=key) elif granted_status in (NodeFrom.asset, NodeFrom.child): - return self.get_indirect_granted_node_children(key) + nodes = self.get_indirect_granted_node_children(key) else: - return PermNode.objects.none() + nodes = PermNode.objects.none() + nodes = self.sort(nodes) + return nodes def get_indirect_granted_node_children(self, key): """ @@ -571,7 +578,8 @@ class UserGrantedNodesQueryUtils(UserGrantedUtilsBase): def get_top_level_nodes(self): nodes = self.get_special_nodes() - nodes.extend(self.get_indirect_granted_node_children('')) + real_nodes = self.get_indirect_granted_node_children('') + nodes.extend(self.sort(real_nodes)) return nodes def get_ungrouped_node(self):