mirror of https://github.com/jumpserver/jumpserver
perf: add account details api
parent
d380a15490
commit
71abe93fa5
|
@ -77,10 +77,12 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
|
||||||
serializer_classes = {
|
serializer_classes = {
|
||||||
"default": serializers.GatheredAccountSerializer,
|
"default": serializers.GatheredAccountSerializer,
|
||||||
"status": serializers.GatheredAccountActionSerializer,
|
"status": serializers.GatheredAccountActionSerializer,
|
||||||
|
"details": serializers.GatheredAccountDetailsSerializer
|
||||||
}
|
}
|
||||||
rbac_perms = {
|
rbac_perms = {
|
||||||
"sync_accounts": "assets.add_gatheredaccount",
|
"sync_accounts": "assets.add_gatheredaccount",
|
||||||
"status": "assets.change_gatheredaccount",
|
"status": "assets.change_gatheredaccount",
|
||||||
|
"details": "assets.view_gatheredaccount"
|
||||||
}
|
}
|
||||||
|
|
||||||
@action(methods=["put"], detail=True, url_path="status")
|
@action(methods=["put"], detail=True, url_path="status")
|
||||||
|
@ -102,3 +104,10 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
|
||||||
handler = RiskHandler(asset, username, request=self.request)
|
handler = RiskHandler(asset, username, request=self.request)
|
||||||
handler.handle_delete_remote()
|
handler.handle_delete_remote()
|
||||||
return Response(status=status.HTTP_200_OK)
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
@action(methods=["get"], detail=True, url_path="details")
|
||||||
|
def details(self, request, *args, **kwargs):
|
||||||
|
pk = kwargs.get('pk')
|
||||||
|
account = get_object_or_404(GatheredAccount, pk=pk)
|
||||||
|
serializer = self.get_serializer(account.detail)
|
||||||
|
return Response(data=serializer.data)
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
from accounts.const import AutomationTypes
|
from accounts.const import AutomationTypes
|
||||||
from accounts.models import GatherAccountsAutomation
|
from accounts.models import GatherAccountsAutomation
|
||||||
|
@ -12,6 +14,7 @@ __all__ = [
|
||||||
'GatheredAccountSerializer',
|
'GatheredAccountSerializer',
|
||||||
'GatheredAccountActionSerializer',
|
'GatheredAccountActionSerializer',
|
||||||
'GatherAccountAutomationSerializer',
|
'GatherAccountAutomationSerializer',
|
||||||
|
'GatheredAccountDetailsSerializer'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,8 +23,8 @@ class GatherAccountAutomationSerializer(BaseAutomationSerializer):
|
||||||
model = GatherAccountsAutomation
|
model = GatherAccountsAutomation
|
||||||
read_only_fields = BaseAutomationSerializer.Meta.read_only_fields
|
read_only_fields = BaseAutomationSerializer.Meta.read_only_fields
|
||||||
fields = (BaseAutomationSerializer.Meta.fields
|
fields = (BaseAutomationSerializer.Meta.fields
|
||||||
+ ['is_sync_account', 'check_risk', 'recipients']
|
+ ['is_sync_account', 'check_risk', 'recipients']
|
||||||
+ read_only_fields)
|
+ read_only_fields)
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
'check_risk': {
|
'check_risk': {
|
||||||
'help_text': _('Whether to check the risk of the gathered accounts.'),
|
'help_text': _('Whether to check the risk of the gathered accounts.'),
|
||||||
|
@ -36,6 +39,7 @@ class GatherAccountAutomationSerializer(BaseAutomationSerializer):
|
||||||
|
|
||||||
class AccountAssetSerializer(_AccountAssetSerializer):
|
class AccountAssetSerializer(_AccountAssetSerializer):
|
||||||
class Meta(_AccountAssetSerializer.Meta):
|
class Meta(_AccountAssetSerializer.Meta):
|
||||||
|
ref_name = "GatheredAccountAssetSerializer"
|
||||||
fields = [f for f in _AccountAssetSerializer.Meta.fields if f != 'auto_config']
|
fields = [f for f in _AccountAssetSerializer.Meta.fields if f != 'auto_config']
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,7 +52,7 @@ class GatheredAccountSerializer(BulkOrgResourceModelSerializer):
|
||||||
'id', 'asset', 'username',
|
'id', 'asset', 'username',
|
||||||
'date_last_login', 'address_last_login',
|
'date_last_login', 'address_last_login',
|
||||||
'remote_present', 'present',
|
'remote_present', 'present',
|
||||||
'date_updated', 'status',
|
'date_updated', 'status', 'detail'
|
||||||
]
|
]
|
||||||
read_only_fields = fields
|
read_only_fields = fields
|
||||||
|
|
||||||
|
@ -62,3 +66,23 @@ class GatheredAccountSerializer(BulkOrgResourceModelSerializer):
|
||||||
class GatheredAccountActionSerializer(GatheredAccountSerializer):
|
class GatheredAccountActionSerializer(GatheredAccountSerializer):
|
||||||
class Meta(GatheredAccountSerializer.Meta):
|
class Meta(GatheredAccountSerializer.Meta):
|
||||||
read_only_fields = list(set(GatheredAccountSerializer.Meta.read_only_fields) - {'status'})
|
read_only_fields = list(set(GatheredAccountSerializer.Meta.read_only_fields) - {'status'})
|
||||||
|
|
||||||
|
|
||||||
|
class GatheredAccountDetailsSerializer(serializers.Serializer):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
request = self.context.get('request')
|
||||||
|
if not request:
|
||||||
|
return
|
||||||
|
|
||||||
|
params = request.query_params
|
||||||
|
if params.get('format') == 'openapi':
|
||||||
|
return
|
||||||
|
pk = request.parser_context['kwargs'].get('pk')
|
||||||
|
obj = get_object_or_404(GatheredAccount, pk=pk)
|
||||||
|
details = obj.detail
|
||||||
|
for key, value in details.items():
|
||||||
|
if isinstance(value, bool):
|
||||||
|
self.fields[key] = serializers.BooleanField(label=key, read_only=True)
|
||||||
|
else:
|
||||||
|
self.fields[key] = serializers.CharField(label=key, read_only=True)
|
||||||
|
|
Loading…
Reference in New Issue