Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
5ffd1f99
Commit
5ffd1f99
authored
Jul 24, 2017
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Bugfix] 修复更新bug
parent
cc1a3391
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
98 additions
and
4 deletions
+98
-4
forms.py
apps/assets/forms.py
+55
-0
system_user_update.html
apps/assets/templates/assets/system_user_update.html
+39
-0
system_user.py
apps/assets/views/system_user.py
+4
-4
No files found.
apps/assets/forms.py
View file @
5ffd1f99
...
...
@@ -302,5 +302,60 @@ class SystemUserForm(forms.ModelForm):
}
class
SystemUserUpdateForm
(
forms
.
ModelForm
):
# Admin user assets define, let user select, save it in form not in view
auto_generate_key
=
forms
.
BooleanField
(
initial
=
False
,
required
=
False
)
# Form field name can not start with `_`, so redefine it,
password
=
forms
.
CharField
(
widget
=
forms
.
PasswordInput
,
required
=
False
,
max_length
=
100
,
strip
=
True
)
# Need use upload private key file except paste private key content
private_key_file
=
forms
.
FileField
(
required
=
False
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
SystemUserUpdateForm
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
def
save
(
self
,
commit
=
True
):
# Because we define custom field, so we need rewrite :method: `save`
system_user
=
super
(
SystemUserUpdateForm
,
self
)
.
save
(
commit
=
commit
)
password
=
self
.
cleaned_data
[
'password'
]
private_key_file
=
self
.
cleaned_data
.
get
(
'private_key_file'
)
if
system_user
.
auth_method
==
'P'
and
password
:
system_user
.
password
=
password
elif
system_user
.
auth_method
==
'K'
and
private_key_file
:
private_key
=
private_key_file
.
read
()
.
strip
()
public_key
=
ssh_pubkey_gen
(
private_key
=
private_key
)
system_user
.
private_key
=
private_key
system_user
.
public_key
=
public_key
system_user
.
save
()
return
self
.
instance
def
clean_private_key_file
(
self
):
if
self
.
data
[
'auth_method'
]
==
'K'
and
self
.
cleaned_data
[
'private_key_file'
]:
key_string
=
self
.
cleaned_data
[
'private_key_file'
]
.
read
()
self
.
cleaned_data
[
'private_key_file'
]
.
seek
(
0
)
if
not
validate_ssh_private_key
(
key_string
):
raise
forms
.
ValidationError
(
_
(
'Invalid private key'
))
return
self
.
cleaned_data
[
'private_key_file'
]
class
Meta
:
model
=
SystemUser
fields
=
[
'name'
,
'username'
,
'protocol'
,
'auto_generate_key'
,
'password'
,
'private_key_file'
,
'auth_method'
,
'auto_push'
,
'sudo'
,
'comment'
,
'shell'
]
widgets
=
{
'name'
:
forms
.
TextInput
(
attrs
=
{
'placeholder'
:
_
(
'Name'
)}),
'username'
:
forms
.
TextInput
(
attrs
=
{
'placeholder'
:
_
(
'Username'
)}),
}
help_texts
=
{
'name'
:
'* required'
,
'username'
:
'* required'
,
'auto_push'
:
'Auto push system user to asset'
,
}
class
FileForm
(
forms
.
Form
):
file
=
forms
.
FileField
()
apps/assets/templates/assets/system_user_update.html
View file @
5ffd1f99
...
...
@@ -13,3 +13,41 @@
</div>
</div>
{% 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 }}'
).
removeAttr
(
'disabled'
);
}
else
if
(
$
(
auth_method
).
val
()
==
'K'
)
{
$
(
'.password-auth'
).
addClass
(
'hidden'
);
$
(
'.public-key-auth'
).
removeClass
(
'hidden'
);
$
(
'#'
+
'{{ form.password.id_for_label }}'
).
removeAttr
(
'required'
);
$
(
'#'
+
'{{ form.password.id_for_label }}'
).
attr
(
'disabled'
,
'disabled'
);
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'
);
}
}
}
$
(
document
).
ready
(
function
()
{
$
(
'.select2'
).
select2
();
authMethodDisplay
();
$
(
auth_method
).
change
(
function
()
{
authMethodDisplay
();
});
$
(
auto_generate_key
).
change
(
function
()
{
authMethodDisplay
();
});
})
</script>
{% endblock %}
\ No newline at end of file
apps/assets/views/system_user.py
View file @
5ffd1f99
...
...
@@ -4,14 +4,14 @@ from __future__ import absolute_import, unicode_literals
from
django.utils.translation
import
ugettext
as
_
from
django.conf
import
settings
from
django.db
import
transaction
from
django.views.generic
import
TemplateView
,
ListView
,
View
from
django.views.generic.edit
import
CreateView
,
DeleteView
,
FormView
,
UpdateView
from
django.views.generic
import
TemplateView
,
ListView
from
django.views.generic.edit
import
CreateView
,
DeleteView
,
UpdateView
from
django.urls
import
reverse_lazy
from
django.contrib.messages.views
import
SuccessMessageMixin
from
django.views.generic.detail
import
DetailView
,
SingleObjectMixin
from
..
import
forms
from
..models
import
Asset
,
AssetGroup
,
AdminUser
,
IDC
,
SystemUser
from
..models
import
Asset
,
AssetGroup
,
SystemUser
from
..hands
import
AdminUserRequiredMixin
from
perms.utils
import
associate_system_users_and_assets
...
...
@@ -65,7 +65,7 @@ class SystemUserCreateView(AdminUserRequiredMixin, SuccessMessageMixin, CreateVi
class
SystemUserUpdateView
(
AdminUserRequiredMixin
,
UpdateView
):
model
=
SystemUser
form_class
=
forms
.
SystemUserForm
form_class
=
forms
.
SystemUser
Update
Form
template_name
=
'assets/system_user_update.html'
def
get_context_data
(
self
,
**
kwargs
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment