Browse Source

fix: 授权树节点排序

pull/5953/head^2
xinwen 4 years ago
parent
commit
2f8042141c
  1. 22
      apps/perms/utils/asset/user_permission.py

22
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):

Loading…
Cancel
Save