From c785409e37091511f4e969cae5752bc49eb5e916 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Thu, 28 Sep 2023 11:57:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20viewset=E4=B8=AD=E9=BB=98=E8=AE=A4perms?= =?UTF-8?q?=5Fmap=E4=B8=BA{'get':=20'*'}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/utils/permission.py | 16 +++++++++++----- apps/utils/viewsets.py | 4 ++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/apps/utils/permission.py b/apps/utils/permission.py index f9ada62d..efe05610 100755 --- a/apps/utils/permission.py +++ b/apps/utils/permission.py @@ -31,7 +31,8 @@ def get_user_perms_map(user): if perm.codes: for code in perm.codes: if code in user_perms_map: - data_range = user_perms_map[code].get(dept_id, -1) + data_range = user_perms_map[code].get( + dept_id, -1) if pr.data_range < data_range: user_perms_map[code][dept_id] = pr.data_range else: @@ -77,6 +78,7 @@ class RbacDataMixin: 带性能优化 此处对性能有较大影响,根据业务需求进行修改或取舍 """ + def get_queryset(self): assert self.queryset is not None, ( "'%s' should either include a `queryset` attribute, " @@ -103,7 +105,8 @@ class RbacDataMixin: if isinstance(user_perms_map, dict): if hasattr(self.view, 'perms_map'): perms_map = self.view.perms_map - action_str = perms_map.get(self.request._request.method.lower(), None) + action_str = perms_map.get( + self.request._request.method.lower(), None) if '*' in perms_map: return queryset elif action_str == '*': @@ -116,13 +119,16 @@ class RbacDataMixin: return queryset elif data_range == DataFilter.SAMELEVE_AND_BELOW: if dept.parent: - belong_depts = get_child_queryset2(dept.parent) + belong_depts = get_child_queryset2( + dept.parent) else: belong_depts = get_child_queryset2(dept) - queryset = queryset.filter(belong_dept__in=belong_depts) + queryset = queryset.filter( + belong_dept__in=belong_depts) elif data_range == DataFilter.THISLEVEL_AND_BELOW: belong_depts = get_child_queryset2(dept) - queryset = queryset.filter(belong_dept__in=belong_depts) + queryset = queryset.filter( + belong_dept__in=belong_depts) elif data_range == DataFilter.THISLEVEL: queryset = queryset.filter(belong_dept=dept) elif data_range == DataFilter.MYSELF: diff --git a/apps/utils/viewsets.py b/apps/utils/viewsets.py index 87d7e156..66acb496 100755 --- a/apps/utils/viewsets.py +++ b/apps/utils/viewsets.py @@ -26,7 +26,7 @@ class CustomGenericViewSet(MyLoggingMixin, GenericViewSet): """ 增强的GenericViewSet """ - perms_map = {'get': '*'} # 权限标识 + perms_map = {} # 权限标识 throttle_classes = [UserRateThrottle] logging_methods = ['POST', 'PUT', 'PATCH', 'DELETE'] ordering_fields = '__all__' @@ -170,7 +170,7 @@ class CustomModelViewSet(BulkCreateModelMixin, BulkUpdateModelMixin, ListModelMi def __init__(self, **kwargs) -> None: super().__init__(**kwargs) # 增加默认权限标识 - if not self.perms_map: + if not self.perms_map or self.perms_map == {'get': '*'}: basename = self.basename self.perms_map = {'get': '*', 'post': '{}.create'.format(basename), 'put': '{}.update'.format( basename), 'patch': '{}.update'.format(basename), 'delete': '{}.delete'.format(basename)}