mirror of https://github.com/jumpserver/jumpserver
fix: 修复账号批量添加模版账号时name没同步过来,资产创建时使用模版账号没有切换自,资产克隆时生成的账号没有切换自 (#11877)
Co-authored-by: feng <1304903146@qq.com>pull/11878/head
parent
578c2af57c
commit
b313598227
|
@ -78,7 +78,8 @@ class AccountCreateUpdateSerializerMixin(serializers.Serializer):
|
||||||
def get_template_attr_for_account(template):
|
def get_template_attr_for_account(template):
|
||||||
# Set initial data from template
|
# Set initial data from template
|
||||||
field_names = [
|
field_names = [
|
||||||
'username', 'secret', 'secret_type', 'privileged', 'is_active'
|
'name', 'username', 'secret',
|
||||||
|
'secret_type', 'privileged', 'is_active'
|
||||||
]
|
]
|
||||||
|
|
||||||
attrs = {}
|
attrs = {}
|
||||||
|
|
|
@ -281,14 +281,48 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali
|
||||||
return protocols_data_map.values()
|
return protocols_data_map.values()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def accounts_create(accounts_data, asset):
|
def update_account_su_from(accounts, include_su_from_accounts):
|
||||||
|
if not include_su_from_accounts:
|
||||||
|
return
|
||||||
|
name_map = {account.name: account for account in accounts}
|
||||||
|
username_secret_type_map = {
|
||||||
|
(account.username, account.secret_type): account for account in accounts
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, username_secret_type in include_su_from_accounts.items():
|
||||||
|
account = name_map.get(name)
|
||||||
|
if not account:
|
||||||
|
continue
|
||||||
|
su_from_account = username_secret_type_map.get(username_secret_type)
|
||||||
|
if su_from_account:
|
||||||
|
account.su_from = su_from_account
|
||||||
|
account.save()
|
||||||
|
|
||||||
|
def accounts_create(self, accounts_data, asset):
|
||||||
|
from accounts.models import AccountTemplate
|
||||||
if not accounts_data:
|
if not accounts_data:
|
||||||
return
|
return
|
||||||
|
su_from_name_username_secret_type_map = {}
|
||||||
for data in accounts_data:
|
for data in accounts_data:
|
||||||
data['asset'] = asset.id
|
data['asset'] = asset.id
|
||||||
|
name = data.get('name')
|
||||||
|
su_from = data.pop('su_from', None)
|
||||||
|
template_id = data.get('template', None)
|
||||||
|
if template_id:
|
||||||
|
template = AccountTemplate.objects.get(id=template_id)
|
||||||
|
if template and template.su_from:
|
||||||
|
su_from_name_username_secret_type_map[template.name] = (
|
||||||
|
template.su_from.username, template.su_from.secret_type
|
||||||
|
)
|
||||||
|
elif isinstance(su_from, dict):
|
||||||
|
su_from = Account.objects.get(id=su_from.get('id'))
|
||||||
|
su_from_name_username_secret_type_map[name] = (
|
||||||
|
su_from.username, su_from.secret_type
|
||||||
|
)
|
||||||
s = AssetAccountSerializer(data=accounts_data, many=True)
|
s = AssetAccountSerializer(data=accounts_data, many=True)
|
||||||
s.is_valid(raise_exception=True)
|
s.is_valid(raise_exception=True)
|
||||||
s.save()
|
accounts = s.save()
|
||||||
|
self.update_account_su_from(accounts, su_from_name_username_secret_type_map)
|
||||||
|
|
||||||
@atomic
|
@atomic
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
|
|
Loading…
Reference in New Issue