Commit 477d23ea authored by ibuler's avatar ibuler

fix some bug

parent 61014c74
......@@ -70,7 +70,6 @@ class AssetBulkUpdateForm(forms.ModelForm):
required=True,
help_text='* required',
label=_('Select assets'),
# choices=[(asset.id, asset.hostname) for asset in Asset.objects.all()],
widget=forms.SelectMultiple(
attrs={
'class': 'select2',
......@@ -182,7 +181,7 @@ class AdminUserForm(forms.ModelForm):
# Form field name can not start with `_`, so redefine it,
password = forms.CharField(
widget=forms.PasswordInput, max_length=100,
min_length=8, strip=True, required=False,
strip=True, required=False,
help_text=_('If also set private key, use that first'),
)
# Need use upload private key file except paste private key content
......@@ -239,7 +238,7 @@ class SystemUserForm(forms.ModelForm):
auto_generate_key = forms.BooleanField(initial=True, required=False)
# Form field name can not start with `_`, so redefine it,
password = forms.CharField(widget=forms.PasswordInput, required=False,
max_length=100, min_length=8, strip=True)
max_length=100, strip=True)
# Need use upload private key file except paste private key content
private_key_file = forms.FileField(required=False)
......
......@@ -125,7 +125,7 @@ class Asset(models.Model):
'method': self.admin_user.become_method,
'user': self.admin_user.become_user,
'pass': self.admin_user.become_pass,
} if self.admin_user.become else {},
} if self.admin_user and self.admin_user.become else {},
}
class Meta:
......
......@@ -27,11 +27,6 @@
</div>
</div>
<div class="ibox-content">
{% if form.no_field_errors %}
<div class="alert alert-danger">
{{ form.non_field_errors }}
</div>
{% endif %}
<form enctype="multipart/form-data" method="post" class="form-horizontal" action="" >
{% csrf_token %}
{% if form.non_field_errors %}
......
......@@ -13,6 +13,8 @@ from django.views.generic.detail import DetailView, SingleObjectMixin
from .. import forms
from ..models import Asset, AssetGroup, AdminUser, IDC, SystemUser
from ..hands import AdminUserRequiredMixin
from perms.utils import associate_system_users_and_assets
__all__ = ['SystemUserCreateView', 'SystemUserUpdateView',
'SystemUserDetailView', 'SystemUserDeleteView',
......@@ -74,6 +76,14 @@ class SystemUserUpdateView(AdminUserRequiredMixin, UpdateView):
kwargs.update(context)
return super(SystemUserUpdateView, self).get_context_data(**kwargs)
def form_valid(self, form):
response = super(SystemUserUpdateView, self).form_valid(form)
system_user = self.object
assets = system_user.assets.all()
asset_groups = system_user.asset_groups.all()
associate_system_users_and_assets([system_user], assets, asset_groups, force=True)
return response
def get_success_url(self):
success_url = reverse_lazy('assets:system-user-detail',
kwargs={'pk': self.object.pk})
......
......@@ -239,7 +239,11 @@ def ssh_key_gen(length=2048, type='rsa', password=None, username='jumpserver', h
def validate_ssh_private_key(text):
if isinstance(text, bytes):
try:
text = text.decode("utf-8")
except UnicodeDecodeError:
return False
key = ssh_key_string_to_obj(text)
if key is None:
return False
......
......@@ -235,7 +235,7 @@ def push_system_user(assets, system_user):
return task.id
def associate_system_users_and_assets(system_users, assets, asset_groups):
def associate_system_users_and_assets(system_users, assets, asset_groups, force=False):
"""关联系统用户和资产, 目的是保存它们的关系, 然后新加入的资产或系统
用户时,推送系统用户到资产
......@@ -250,6 +250,9 @@ def associate_system_users_and_assets(system_users, assets, asset_groups):
for system_user in system_users:
assets_need_push = []
if system_user.auto_push:
if force:
assets_need_push = assets_all
else:
assets_need_push.extend(
[asset for asset in assets_all
if asset not in system_user.assets.all()
......@@ -258,4 +261,3 @@ def associate_system_users_and_assets(system_users, assets, asset_groups):
system_user.assets.add(*(tuple(assets_all)))
push_system_user(assets_need_push, system_user)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment