mirror of https://github.com/jumpserver/jumpserver
parent
d3170e4815
commit
f6c5c35a2c
|
@ -6,10 +6,9 @@ from rest_framework.decorators import action
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from accounts import serializers
|
from accounts import serializers
|
||||||
from accounts.const import AutomationTypes
|
from accounts.const import Source, AutomationTypes
|
||||||
from accounts.const import Source
|
|
||||||
from accounts.filters import GatheredAccountFilterSet
|
from accounts.filters import GatheredAccountFilterSet
|
||||||
from accounts.models import GatherAccountsAutomation
|
from accounts.models import GatherAccountsAutomation, Account
|
||||||
from accounts.models import GatheredAccount
|
from accounts.models import GatheredAccount
|
||||||
from orgs.mixins.api import OrgBulkModelViewSet
|
from orgs.mixins.api import OrgBulkModelViewSet
|
||||||
from .base import AutomationExecutionViewSet
|
from .base import AutomationExecutionViewSet
|
||||||
|
@ -50,22 +49,28 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
|
||||||
'default': serializers.GatheredAccountSerializer,
|
'default': serializers.GatheredAccountSerializer,
|
||||||
}
|
}
|
||||||
rbac_perms = {
|
rbac_perms = {
|
||||||
'sync_account': 'assets.add_gatheredaccount',
|
'sync_accounts': 'assets.add_gatheredaccount',
|
||||||
}
|
}
|
||||||
|
|
||||||
@action(methods=['post'], detail=True, url_path='sync')
|
@action(methods=['post'], detail=False, url_path='sync-accounts')
|
||||||
def sync_account(self, request, *args, **kwargs):
|
def sync_accounts(self, request, *args, **kwargs):
|
||||||
gathered_account = super().get_object()
|
gathered_account_ids = request.data.get('gathered_account_ids')
|
||||||
asset = gathered_account.asset
|
gathered_accounts = self.model.objects.filter(id__in=gathered_account_ids)
|
||||||
|
account_objs = []
|
||||||
|
exists_accounts = Account.objects.none()
|
||||||
|
for gathered_account in gathered_accounts:
|
||||||
|
asset_id = gathered_account.asset_id
|
||||||
username = gathered_account.username
|
username = gathered_account.username
|
||||||
accounts = asset.accounts.filter(username=username)
|
accounts = Account.objects.filter(asset_id=asset_id, username=username)
|
||||||
|
|
||||||
if accounts.exists():
|
if accounts.exists():
|
||||||
accounts.update(source=Source.COLLECTED)
|
exists_accounts |= accounts
|
||||||
else:
|
else:
|
||||||
asset.accounts.model.objects.create(
|
account_objs.append(
|
||||||
asset=asset, username=username,
|
Account(
|
||||||
|
asset_id=asset_id, username=username,
|
||||||
name=f'{username}-{_("Collected")}',
|
name=f'{username}-{_("Collected")}',
|
||||||
source=Source.COLLECTED
|
source=Source.COLLECTED
|
||||||
)
|
))
|
||||||
|
exists_accounts.update(source=Source.COLLECTED)
|
||||||
|
Account.objects.bulk_create(account_objs)
|
||||||
return Response(status=status.HTTP_201_CREATED)
|
return Response(status=status.HTTP_201_CREATED)
|
||||||
|
|
|
@ -109,7 +109,7 @@ class AssetPermissionSerializer(BulkOrgResourceModelSerializer):
|
||||||
if condition in username_secret_type_dict:
|
if condition in username_secret_type_dict:
|
||||||
continue
|
continue
|
||||||
account_data = {key: getattr(template, key) for key in account_attribute}
|
account_data = {key: getattr(template, key) for key in account_attribute}
|
||||||
account_data['name'] = f"{account_data['name']}-clone"
|
account_data['name'] = f"{account_data['name']}-{_('Account template')}"
|
||||||
need_create_accounts.append(Account(**{'asset_id': asset.id, **account_data}))
|
need_create_accounts.append(Account(**{'asset_id': asset.id, **account_data}))
|
||||||
return Account.objects.bulk_create(need_create_accounts)
|
return Account.objects.bulk_create(need_create_accounts)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue