mirror of https://github.com/jumpserver/jumpserver
[Bugfix] 修改添加系统用户view
parent
8916221bba
commit
87bbb6afde
|
@ -214,23 +214,9 @@ class AdminUserForm(forms.ModelForm):
|
|||
|
||||
class SystemUserForm(forms.ModelForm):
|
||||
# Admin user assets define, let user select, save it in form not in view
|
||||
assets = forms.ModelMultipleChoiceField(queryset=Asset.objects.all(),
|
||||
label=_('Asset'),
|
||||
required=False,
|
||||
widget=forms.SelectMultiple(
|
||||
attrs={'class': 'select2', 'data-placeholder': _('Select assets')})
|
||||
)
|
||||
asset_groups = forms.ModelMultipleChoiceField(queryset=AssetGroup.objects.all(),
|
||||
label=_('Asset group'),
|
||||
required=False,
|
||||
widget=forms.SelectMultiple(
|
||||
attrs={'class': 'select2',
|
||||
'data-placeholder': _('Select asset groups')})
|
||||
)
|
||||
auto_generate_key = forms.BooleanField(initial=True)
|
||||
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, max_length=100, min_length=8, strip=True,
|
||||
help_text=_('If also set private key, use that first'), required=False)
|
||||
password = forms.CharField(widget=forms.PasswordInput, max_length=100, min_length=8, strip=True)
|
||||
# Need use upload private key file except paste private key content
|
||||
private_key_file = forms.FileField(required=False)
|
||||
|
||||
|
@ -242,24 +228,15 @@ class SystemUserForm(forms.ModelForm):
|
|||
initial['asset_groups'] = kwargs['instance'].asset_groups.all()
|
||||
super(SystemUserForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def _save_m2m(self):
|
||||
# Save assets relation with admin user
|
||||
super(SystemUserForm, self)._save_m2m()
|
||||
assets = self.cleaned_data['assets']
|
||||
asset_groups = self.cleaned_data['asset_groups']
|
||||
self.instance.assets.clear()
|
||||
self.instance.assets.add(*tuple(assets))
|
||||
self.instance.asset_groups.clear()
|
||||
self.instance.asset_groups.add(*tuple(asset_groups))
|
||||
|
||||
def save(self, commit=True):
|
||||
# Because we define custom field, so we need rewrite :method: `save`
|
||||
system_user = super(SystemUserForm, self).save(commit=commit)
|
||||
password = self.cleaned_data['password']
|
||||
private_key_file = self.cleaned_data['private_key_file']
|
||||
|
||||
if password:
|
||||
system_user.password = password
|
||||
if system_user.auth_method == 'P':
|
||||
if password:
|
||||
system_user.password = password
|
||||
print(password)
|
||||
# Todo: Validate private key file, and generate public key
|
||||
# Todo: Auto generate private key and public key
|
||||
|
@ -268,11 +245,30 @@ class SystemUserForm(forms.ModelForm):
|
|||
system_user.save()
|
||||
return self.instance
|
||||
|
||||
# Todo: check valid
|
||||
# def clean_private_key_file(self):
|
||||
# if not self.cleaned_data['auto_generate_key']:
|
||||
# if not self.cleaned_data['private_key_file']:
|
||||
# raise forms.ValidationError(_('Private key required'))
|
||||
|
||||
# def clean_password(self):
|
||||
# if self.cleaned_data['auth_method'] == 'P':
|
||||
# if not self.cleaned_data['password']:
|
||||
# raise forms.ValidationError(_('Password required'))
|
||||
# return self.cleaned_data['password']
|
||||
|
||||
# def clean(self):
|
||||
# password = self.cleaned_data['password']
|
||||
# private_key_file = self.cleaned_data.get('private_key_file', '')
|
||||
#
|
||||
# if not (password or private_key_file):
|
||||
# raise forms.ValidationError(_('Password and private key file must be input one'))
|
||||
|
||||
class Meta:
|
||||
model = SystemUser
|
||||
fields = [
|
||||
'name', 'username', 'protocol', 'auto_generate_key', 'password', 'private_key_file', 'auth_method',
|
||||
'auto_push', 'auto_update', 'sudo', 'comment', 'shell', 'home', 'uid',
|
||||
'auto_push', 'sudo', 'comment', 'shell', 'home', 'uid',
|
||||
]
|
||||
widgets = {
|
||||
'name': forms.TextInput(attrs={'placeholder': _('Name')}),
|
||||
|
@ -282,7 +278,6 @@ class SystemUserForm(forms.ModelForm):
|
|||
'name': '* required',
|
||||
'username': '* required',
|
||||
'auto_push': 'Auto push system user to asset',
|
||||
'auto_update': 'Auto update system user ssh key',
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -27,23 +27,31 @@
|
|||
</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 %}
|
||||
<h3>{% trans 'Basic' %}</h3>
|
||||
{{ form.name|bootstrap_horizontal }}
|
||||
{{ form.username|bootstrap_horizontal }}
|
||||
{{ form.protocol|bootstrap_horizontal }}
|
||||
<div class="form-group">
|
||||
<label for="{{ form.auto_generate_key.id_for_label }}" class="col-sm-2 control-label">{% trans 'Auto generate key' %}</label>
|
||||
<div class="col-sm-8">
|
||||
{{ form.auto_generate_key}}
|
||||
</div>
|
||||
<h3>{% trans 'Auth' %}</h3>
|
||||
{{ form.auth_method|bootstrap_horizontal }}
|
||||
<div class="password-auth hidden">
|
||||
{{ form.password|bootstrap_horizontal }}
|
||||
</div>
|
||||
{{ form.password|bootstrap_horizontal }}
|
||||
{{ form.private_key_file|bootstrap_horizontal }}
|
||||
<div class="form-group">
|
||||
<label for="{{ form.as_default.id_for_label }}" class="col-sm-2 control-label">{% trans 'As default' %}</label>
|
||||
<div class="col-sm-8">
|
||||
{{ form.as_default}}
|
||||
<div class="public-key-auth">
|
||||
<div class="form-group">
|
||||
<label for="{{ form.auto_generate_key.id_for_label }}" class="col-sm-2 control-label">{% trans 'Auto generate key' %}</label>
|
||||
<div class="col-sm-8">
|
||||
{{ form.auto_generate_key}}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
{{ form.private_key_file|bootstrap_horizontal }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
|
@ -52,20 +60,12 @@
|
|||
{{ form.auto_push}}
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="{{ form.as_update.id_for_label }}" class="col-sm-2 control-label">{% trans 'Auto update' %}</label>
|
||||
<div class="col-sm-8">
|
||||
{{ form.auto_update}}
|
||||
</div>
|
||||
</div>
|
||||
{{ form.assets|bootstrap_horizontal }}
|
||||
{{ form.asset_groups|bootstrap_horizontal }}
|
||||
<h3>{% trans 'Other' %}</h3>
|
||||
{{ form.sudo|bootstrap_horizontal }}
|
||||
{{ form.comment|bootstrap_horizontal }}
|
||||
{{ form.home|bootstrap_horizontal }}
|
||||
{{ form.shell|bootstrap_horizontal }}
|
||||
{{ form.uid|bootstrap_horizontal }}
|
||||
|
||||
{{ form.comment|bootstrap_horizontal }}
|
||||
<div class="form-group">
|
||||
<div class="col-sm-4 col-sm-offset-2">
|
||||
<button class="btn btn-white" type="reset">{% trans 'Reset' %}</button>
|
||||
|
@ -81,8 +81,36 @@
|
|||
{% endblock %}
|
||||
{% block custom_foot_js %}
|
||||
<script>
|
||||
var auth_method = '#'+'{{ form.auth_method.id_for_label }}';
|
||||
var auto_generate_key = '#'+'{{ form.auto_generate_key.id_for_label }}';
|
||||
function authMethodDisplay() {
|
||||
if ($(auth_method).val() == 'P') {
|
||||
$('.password-auth').removeClass('hidden');
|
||||
$('.public-key-auth').addClass('hidden');
|
||||
$('#'+'{{ form.password.id_for_label }}').attr('required', 'required');
|
||||
|
||||
} else if ($(auth_method).val() == 'K') {
|
||||
$('.password-auth').addClass('hidden');
|
||||
$('.public-key-auth').removeClass('hidden');
|
||||
|
||||
if ($(auto_generate_key).prop('checked')){
|
||||
$('#'+'{{ form.private_key_file.id_for_label }}').closest('.form-group').addClass('hidden');
|
||||
} else {
|
||||
$('#'+'{{ form.private_key_file.id_for_label }}').closest('.form-group').removeClass('hidden');
|
||||
{# $('#'+'{{ form.private_key_file.id_for_label }}').attr('required', 'required');#}
|
||||
}
|
||||
}
|
||||
}
|
||||
$(document).ready(function () {
|
||||
$('.select2').select2();
|
||||
authMethodDisplay();
|
||||
$(auth_method).change(function () {
|
||||
authMethodDisplay();
|
||||
});
|
||||
$(auto_generate_key).change(function () {
|
||||
authMethodDisplay();
|
||||
});
|
||||
|
||||
|
||||
if ($('#'+'{{ form.protocol.id_for_label }}').val() == 'telnet') {
|
||||
$('#'+'{{ form.auto_generate_key.id_for_label }}').closest('.form-group').remove();
|
||||
|
|
|
@ -547,6 +547,10 @@ class SystemUserCreateView(AdminUserRequiredMixin, SuccessMessageMixin, CreateVi
|
|||
kwargs.update(context)
|
||||
return super(SystemUserCreateView, self).get_context_data(**kwargs)
|
||||
|
||||
def form_invalid(self, form):
|
||||
print(form.errors)
|
||||
return super(SystemUserCreateView, self).form_invalid(form)
|
||||
|
||||
def get_success_message(self, cleaned_data):
|
||||
success_message = _('Create system user <a href="%s">%s</a> successfully.' %
|
||||
(
|
||||
|
|
Loading…
Reference in New Issue