perf: 优化资产授权规则过滤支持 accounts

pull/9426/head
Bai 2023-02-03 15:01:31 +08:00 committed by Jiangjie.Bai
parent d2ae6642eb
commit ce8ad5f9cc
3 changed files with 16 additions and 2 deletions

View File

@ -5,7 +5,7 @@ from common.drf.filters import BaseFilterSet
from common.utils import get_object_or_none, is_uuid
from users.models import User, UserGroup
from assets.models import Node, Asset
from perms.models import AssetPermission
from perms.models import AssetPermission, AssetPermissionQuerySet
class PermissionBaseFilter(BaseFilterSet):
@ -94,6 +94,7 @@ class AssetPermissionFilter(PermissionBaseFilter):
node_name = filters.CharFilter(method='do_nothing')
asset_id = filters.UUIDFilter(method='do_nothing')
asset_name = filters.CharFilter(method='do_nothing')
accounts = filters.CharFilter(method='do_nothing')
ip = filters.CharFilter(method='do_nothing')
class Meta:
@ -111,9 +112,18 @@ class AssetPermissionFilter(PermissionBaseFilter):
qs = self.filter_effective(qs)
qs = self.filter_asset(qs)
qs = self.filter_node(qs)
qs = self.filter_accounts(qs)
qs = qs.distinct()
return qs
def filter_accounts(self, queryset: AssetPermissionQuerySet):
accounts = self.get_query_param('accounts')
if not accounts:
return queryset
accounts = accounts.split(',')
queryset = queryset.filter_by_accounts(accounts)
return queryset
def filter_node(self, queryset: QuerySet):
is_query_all = self.get_query_param('all', True)
node_id = self.get_query_param('node_id')

View File

@ -15,7 +15,7 @@ from common.utils.timezone import local_now
from perms.const import ActionChoices
from accounts.const import AliasAccount
__all__ = ['AssetPermission', 'ActionChoices']
__all__ = ['AssetPermission', 'ActionChoices', 'AssetPermissionQuerySet']
# 使用场景
logger = logging.getLogger(__name__)

View File

@ -72,6 +72,10 @@ class AssetPermissionSerializer(BulkOrgResourceModelSerializer):
return
actions.default = list(actions.choices.keys())
@staticmethod
def validate_accounts(accounts):
return list(set(accounts))
@classmethod
def setup_eager_loading(cls, queryset):
"""Perform necessary eager loading of data."""