mirror of https://github.com/jumpserver/jumpserver
perf: 优化 account 添加 name
parent
c1ad072736
commit
4fcbdfa3f4
|
@ -1,6 +1,6 @@
|
|||
from orgs.mixins.api import OrgBulkModelViewSet
|
||||
from ..models import AccountTemplate
|
||||
from .. import serializers
|
||||
from assets.models import AccountTemplate
|
||||
from assets import serializers
|
||||
|
||||
|
||||
class AccountTemplateViewSet(OrgBulkModelViewSet):
|
||||
|
|
|
@ -21,6 +21,7 @@ class Migration(migrations.Migration):
|
|||
fields=[
|
||||
('org_id', models.CharField(blank=True, db_index=True, default='', max_length=36, verbose_name='Organization')),
|
||||
('id', models.UUIDField(db_index=True, default=uuid.uuid4)),
|
||||
('name', models.CharField(max_length=128, verbose_name='Name')),
|
||||
('username', models.CharField(blank=True, db_index=True, max_length=128, verbose_name='Username')),
|
||||
('password', common.db.fields.EncryptCharField(blank=True, max_length=256, null=True, verbose_name='Password')),
|
||||
('private_key', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='SSH private key')),
|
||||
|
@ -52,6 +53,7 @@ class Migration(migrations.Migration):
|
|||
fields=[
|
||||
('org_id', models.CharField(blank=True, db_index=True, default='', max_length=36, verbose_name='Organization')),
|
||||
('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)),
|
||||
('name', models.CharField(max_length=128, verbose_name='Name')),
|
||||
('username', models.CharField(blank=True, db_index=True, max_length=128, verbose_name='Username')),
|
||||
('password', common.db.fields.EncryptCharField(blank=True, max_length=256, null=True, verbose_name='Password')),
|
||||
('private_key', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='SSH private key')),
|
||||
|
@ -68,7 +70,7 @@ class Migration(migrations.Migration):
|
|||
options={
|
||||
'verbose_name': 'Account',
|
||||
'permissions': [('view_accountsecret', 'Can view asset account secret'), ('change_accountsecret', 'Can change asset account secret'), ('view_historyaccount', 'Can view asset history account'), ('view_historyaccountsecret', 'Can view asset history account secret')],
|
||||
'unique_together': {('username', 'asset')},
|
||||
'unique_together': {('username', 'asset'), ('name', 'asset')},
|
||||
},
|
||||
),
|
||||
]
|
||||
|
|
|
@ -40,7 +40,7 @@ def migrate_accounts(apps, schema_editor):
|
|||
values['version'] = 1
|
||||
|
||||
system_user = auth_book.systemuser
|
||||
if auth_book.systemuser:
|
||||
if system_user:
|
||||
values.update({attr: getattr(system_user, attr) for attr in auth_attrs})
|
||||
values['created_by'] = str(system_user.id)
|
||||
values['privileged'] = system_user.type == 'admin'
|
||||
|
@ -48,6 +48,7 @@ def migrate_accounts(apps, schema_editor):
|
|||
auth_book_auth = {attr: getattr(auth_book, attr) for attr in auth_attrs}
|
||||
auth_book_auth = {attr: value for attr, value in auth_book_auth.items() if value}
|
||||
values.update(auth_book_auth)
|
||||
values['name'] = values['username']
|
||||
|
||||
account = account_model(**values)
|
||||
accounts.append(account)
|
||||
|
|
|
@ -23,7 +23,10 @@ class Account(BaseAccount):
|
|||
|
||||
class Meta:
|
||||
verbose_name = _('Account')
|
||||
unique_together = [('username', 'asset')]
|
||||
unique_together = [
|
||||
('username', 'asset'),
|
||||
('name', 'asset'),
|
||||
]
|
||||
permissions = [
|
||||
('view_accountsecret', _('Can view asset account secret')),
|
||||
('change_accountsecret', _('Can change asset account secret')),
|
||||
|
@ -31,10 +34,6 @@ class Account(BaseAccount):
|
|||
('view_historyaccountsecret', _('Can view asset history account secret')),
|
||||
]
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return "{}({})_{}".format(self.asset_name, self.ip, self.username)
|
||||
|
||||
@lazyproperty
|
||||
def ip(self):
|
||||
return self.asset.ip
|
||||
|
|
|
@ -14,7 +14,9 @@ class AccountSerializerCreateMixin(serializers.ModelSerializer):
|
|||
required=False, allow_null=True, write_only=True,
|
||||
label=_('Account template')
|
||||
)
|
||||
push_to_asset = serializers.BooleanField(default=False, label=_("Push to asset"), write_only=True)
|
||||
push_now = serializers.BooleanField(
|
||||
default=False, label=_("Push now"), write_only=True
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def validate_template(value):
|
||||
|
@ -39,9 +41,17 @@ class AccountSerializerCreateMixin(serializers.ModelSerializer):
|
|||
account_template = attrs.pop('template', None)
|
||||
if account_template:
|
||||
self.replace_attrs(account_template, attrs)
|
||||
push_to_asset = attrs.pop('push_to_asset', False)
|
||||
self.push_now = attrs.pop('push_now', False)
|
||||
return super().validate(attrs)
|
||||
|
||||
def create(self, validated_data):
|
||||
instance = super().create(validated_data)
|
||||
if self.push_now:
|
||||
print("Start push account to asset")
|
||||
# Todo: push it
|
||||
pass
|
||||
return instance
|
||||
|
||||
|
||||
class AccountSerializer(AuthValidateMixin,
|
||||
AccountSerializerCreateMixin,
|
||||
|
@ -55,7 +65,7 @@ class AccountSerializer(AuthValidateMixin,
|
|||
class Meta(AccountFieldsSerializerMixin.Meta):
|
||||
model = Account
|
||||
fields = AccountFieldsSerializerMixin.Meta.fields \
|
||||
+ ['template', 'push_to_asset']
|
||||
+ ['template', 'push_now']
|
||||
|
||||
@classmethod
|
||||
def setup_eager_loading(cls, queryset):
|
||||
|
|
|
@ -54,9 +54,12 @@ class AssetAccountSerializer(AccountSerializer):
|
|||
|
||||
class Meta(AccountSerializer.Meta):
|
||||
fields_mini = [
|
||||
'id', 'name', 'username', 'privileged', 'version'
|
||||
'id', 'name', 'username', 'privileged', 'version',
|
||||
]
|
||||
fields_write_only = [
|
||||
'password', 'private_key', 'public_key',
|
||||
'passphrase', 'token', 'push_now'
|
||||
]
|
||||
fields_write_only = ['password', 'private_key', 'public_key', 'passphrase', 'token']
|
||||
fields = fields_mini + fields_write_only
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue