mirror of https://github.com/jumpserver/jumpserver
perf: account filter risk
parent
106062ce81
commit
ac2151bc4b
|
@ -37,6 +37,8 @@ class AccountViewSet(OrgBulkModelViewSet):
|
||||||
'partial_update': ['accounts.change_account'],
|
'partial_update': ['accounts.change_account'],
|
||||||
'su_from_accounts': 'accounts.view_account',
|
'su_from_accounts': 'accounts.view_account',
|
||||||
'clear_secret': 'accounts.change_account',
|
'clear_secret': 'accounts.change_account',
|
||||||
|
'move_to_assets': 'accounts.create_account',
|
||||||
|
'copy_to_assets': 'accounts.create_account',
|
||||||
}
|
}
|
||||||
export_as_zip = True
|
export_as_zip = True
|
||||||
|
|
||||||
|
@ -112,18 +114,18 @@ class AccountViewSet(OrgBulkModelViewSet):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
creation_results[asset] = {'error': str(e), 'state': 'error'}
|
creation_results[asset] = {'error': str(e), 'state': 'error'}
|
||||||
|
|
||||||
results = [{'asset': asset, **res} for asset, res in creation_results.items()]
|
results = [{'asset': str(asset), **res} for asset, res in creation_results.items()]
|
||||||
|
|
||||||
if move and success_count > 0:
|
if move and success_count > 0:
|
||||||
account.delete()
|
account.delete()
|
||||||
|
|
||||||
return Response(data=results, status=HTTP_200_OK)
|
return Response(results, status=HTTP_200_OK)
|
||||||
|
|
||||||
@action(methods=['patch'], detail=True, url_path='move-to-assets')
|
@action(methods=['post'], detail=True, url_path='move-to-assets')
|
||||||
def move_to_assets(self, request, *args, **kwargs):
|
def move_to_assets(self, request, *args, **kwargs):
|
||||||
return self._copy_or_move_to_assets(request, move=True)
|
return self._copy_or_move_to_assets(request, move=True)
|
||||||
|
|
||||||
@action(methods=['patch'], detail=True, url_path='copy-to-assets')
|
@action(methods=['post'], detail=True, url_path='copy-to-assets')
|
||||||
def copy_to_assets(self, request, *args, **kwargs):
|
def copy_to_assets(self, request, *args, **kwargs):
|
||||||
return self._copy_or_move_to_assets(request, move=False)
|
return self._copy_or_move_to_assets(request, move=False)
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
from django.db.models import Q, F
|
from django.db.models import Q, F, Value, CharField
|
||||||
|
from django.db.models.functions import Concat
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django_filters import rest_framework as drf_filters
|
from django_filters import rest_framework as drf_filters
|
||||||
|
|
||||||
from assets.models import Node
|
from assets.models import Node
|
||||||
from common.drf.filters import BaseFilterSet
|
from common.drf.filters import BaseFilterSet
|
||||||
from common.utils.timezone import local_zero_hour, local_now
|
from common.utils.timezone import local_zero_hour, local_now
|
||||||
from .models import Account, GatheredAccount, ChangeSecretRecord
|
from .models import Account, GatheredAccount, ChangeSecretRecord, AccountRisk
|
||||||
|
|
||||||
|
|
||||||
class AccountFilterSet(BaseFilterSet):
|
class AccountFilterSet(BaseFilterSet):
|
||||||
|
@ -62,9 +63,20 @@ class AccountFilterSet(BaseFilterSet):
|
||||||
if not value:
|
if not value:
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
queryset = queryset.prefetch_related('risks') \
|
asset_usernames = AccountRisk.objects.filter(risk=value). \
|
||||||
.annotate(risk=F('risks__risk'), confirmed=F('risks__confirmed')) \
|
values_list(
|
||||||
.filter(risk=value, confirmed=False)
|
Concat(
|
||||||
|
F('asset_id'), Value('-'), F('username'),
|
||||||
|
output_field=CharField()
|
||||||
|
), flat=True
|
||||||
|
)
|
||||||
|
|
||||||
|
queryset = queryset.annotate(
|
||||||
|
asset_username=Concat(
|
||||||
|
F('asset_id'), Value('-'), F('username'),
|
||||||
|
output_field=CharField()
|
||||||
|
)
|
||||||
|
).filter(asset_username__in=asset_usernames)
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
Loading…
Reference in New Issue