From 2c04ad64652fc174e9f4b9aa19c5188f3f5ea43d Mon Sep 17 00:00:00 2001 From: "Jiangjie.Bai" Date: Tue, 18 Oct 2022 16:04:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E8=B5=84?= =?UTF-8?q?=E4=BA=A7=E6=8E=88=E6=9D=83=E5=B7=A5=E5=85=B7=E3=80=81=E8=B5=84?= =?UTF-8?q?=E4=BA=A7=E6=8E=88=E6=9D=83=E8=B4=A6=E5=8F=B7=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/perms/utils/permission.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/perms/utils/permission.py b/apps/perms/utils/permission.py index 7906cf1d3..3096a87db 100644 --- a/apps/perms/utils/permission.py +++ b/apps/perms/utils/permission.py @@ -43,7 +43,8 @@ class AssetPermissionUtil(object): def get_permissions_for_user_groups(user_groups, flat=False): """ 获取用户组的授权规则 """ group_ids = user_groups.values_list('id', flat=True).distinct() - group_perm_ids = AssetPermission.user_groups.through.objects.filter(usergroup_id__in=group_ids) \ + group_perm_ids = AssetPermission.user_groups.through.objects\ + .filter(usergroup_id__in=group_ids)\ .values_list('assetpermission_id', flat=True).distinct() if flat: return group_perm_ids @@ -66,9 +67,16 @@ class AssetPermissionUtil(object): return perms @staticmethod - def get_permissions_for_nodes(nodes, flat=False): + def get_permissions_for_nodes(nodes, with_ancestor=False, flat=False): """ 获取节点的授权规则 """ - node_ids = nodes.values_list('id', flat=True).distinct() + if with_ancestor: + node_ids = set() + for node in nodes: + _nodes = node.get_ancestors(with_self=True) + _node_ids = _nodes.values_list('id', flat=True).distinct() + node_ids.update(_node_ids) + else: + node_ids = nodes.values_list('id', flat=True).distinct() node_perm_ids = AssetPermission.nodes.through.objects.filter(node_id__in=node_ids) \ .values_list('assetpermission_id', flat=True).distinct() if flat: