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
7984806b
Commit
7984806b
authored
Sep 15, 2016
by
xiaokong1937@gmail.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change user ssh reset type from private key to public key
parent
766bd3b7
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
63 additions
and
40 deletions
+63
-40
api.py
apps/users/api.py
+7
-3
forms.py
apps/users/forms.py
+19
-9
user_detail.html
apps/users/templates/users/user_detail.html
+15
-22
utils.py
apps/users/utils.py
+22
-0
views.py
apps/users/views.py
+0
-6
No files found.
apps/users/api.py
View file @
7984806b
...
...
@@ -6,7 +6,6 @@ import logging
from
rest_framework
import
generics
from
.serializers
import
UserSerializer
,
UserGroupSerializer
,
UserAttributeSerializer
,
UserGroupEditSerializer
from
.serializers
import
UserPKUpdateSerializer
from
.models
import
User
,
UserGroup
...
...
@@ -60,15 +59,20 @@ class UserResetPasswordApi(generics.UpdateAPIView):
# Note: we are not updating the user object here.
# We just do the reset-password staff.
user
=
self
.
get_object
()
import
uuid
user
.
password_raw
=
str
(
uuid
.
uuid4
())
user
.
save
()
from
.utils
import
send_reset_password_mail
send_reset_password_mail
(
user
)
class
UserResetPKApi
(
generics
.
UpdateAPIView
):
queryset
=
User
.
objects
.
all
()
serializer_class
=
User
PKUpdate
Serializer
serializer_class
=
User
GroupEdit
Serializer
def
perform_update
(
self
,
serializer
):
user
=
self
.
get_object
()
user
.
private_key
=
serializer
.
validated_data
[
'_private_key'
]
user
.
_public_key
=
''
user
.
save
()
from
.utils
import
send_reset_ssh_key_mail
send_reset_ssh_key_mail
(
user
)
apps/users/forms.py
View file @
7984806b
...
...
@@ -79,12 +79,22 @@ class UserInfoForm(forms.Form):
class
UserKeyForm
(
forms
.
Form
):
private_key
=
forms
.
CharField
(
max_length
=
5000
,
widget
=
forms
.
Textarea
,
label
=
_
(
'private key'
))
def
clean_private_key
(
self
):
from
users.utils
import
validate_ssh_pk
ssh_pk
=
self
.
cleaned_data
[
'private_key'
]
checked
,
reason
=
validate_ssh_pk
(
ssh_pk
)
if
not
checked
:
raise
forms
.
ValidationError
(
_
(
'Not a valid ssh private key.'
))
return
ssh_pk
public_key
=
forms
.
CharField
(
label
=
_
(
'ssh public key'
),
max_length
=
5000
,
widget
=
forms
.
Textarea
(
attrs
=
{
'placeholder'
:
_
(
'ssh-rsa AAAA...'
)}),
help_text
=
_
(
'Paste your id_ras.pub here.'
))
def
clean_public_key
(
self
):
from
sshpubkeys
import
SSHKey
from
sshpubkeys.exceptions
import
InvalidKeyException
public_key
=
self
.
cleaned_data
[
'public_key'
]
ssh
=
SSHKey
(
public_key
)
try
:
ssh
.
parse
()
except
InvalidKeyException
as
e
:
print
e
raise
forms
.
ValidationError
(
_
(
'Not a valid ssh public key'
))
except
NotImplementedError
as
e
:
print
e
raise
forms
.
ValidationError
(
_
(
'Not a valid ssh public key'
))
return
public_key
apps/users/templates/users/user_detail.html
View file @
7984806b
...
...
@@ -152,7 +152,7 @@
<td>
{% trans 'Reset ssh key' %}:
</td>
<td>
<span
class=
"pull-right"
>
<button
type=
"button"
class=
"btn btn-primary btn-xs"
id=
"btn_reset_pk"
style=
"width: 54px;"
data-toggle=
"modal"
data-target=
"#user_reset_pk_modal"
>
{% trans 'Reset' %}
</button>
<button
type=
"button"
class=
"btn btn-primary btn-xs"
id=
"btn_reset_pk"
style=
"width: 54px;"
>
{% trans 'Reset' %}
</button>
</span>
</td>
</tr>
...
...
@@ -203,7 +203,6 @@
</div>
</div>
</div>
{% include 'users/_user_reset_pk_modal.html' %}
{% endblock %}
{% block custom_foot_js %}
<script>
...
...
@@ -304,34 +303,28 @@ $(document).ready(function () {
doReset
();
}
);
}).
on
(
'click'
,
'#btn_user_reset_pk'
,
function
(){
var
$this
=
$
(
this
);
var
pk
=
$
(
'#txt_pk'
).
val
();
}).
on
(
'click'
,
'#btn_reset_pk'
,
function
(){
function
doReset
()
{
var
the_url
=
'{% url "users:user-reset-pk-api" pk=user_object.id %}'
;
var
body
=
{
'_private_key'
:
pk
};
var
body
=
{
};
var
success
=
function
()
{
$
(
'#txt_pk'
).
val
(
''
);
$this
.
closest
(
'.modal'
).
modal
(
'hide'
);
var
msg
=
"{% trans 'Successfully updated the SSH private key.' %}"
;
swal
(
"{% trans 'User SSH Private Key Reset' %}"
,
msg
,
"success"
);
};
var
fail
=
function
()
{
var
msg
=
"{% trans 'Failed to update the user
\
's SSH private key.' %}"
;
var
msg
=
"{% trans 'The reset-ssh-public-key E-mail has been sent successfully. Please inform the user to update his new ssh public key.' %}"
;
swal
(
"{% trans 'SSH-Public-Key Reset' %}"
,
msg
,
"success"
);
}
APIUpdateAttr
({
url
:
the_url
,
body
:
JSON
.
stringify
(
body
),
success
:
success
});
}
swal
({
title
:
"{% trans 'User SSH Private Key Reset
' %}"
,
text
:
msg
,
type
:
"error
"
,
showCancelButton
:
fals
e
,
title
:
"{% trans 'Are you sure?
' %}"
,
text
:
"{% trans 'This will reset the user
\
's public key.' %}"
,
type
:
"warning
"
,
showCancelButton
:
tru
e
,
confirmButtonColor
:
"#DD6B55"
,
confirmButtonText
:
"{% trans 'Confirm' %}"
,
closeOnConfirm
:
tru
e
closeOnConfirm
:
fals
e
},
function
()
{
$
(
'#txt_pk'
).
focus
();
doReset
();
}
);
}
APIUpdateAttr
({
url
:
the_url
,
body
:
JSON
.
stringify
(
body
),
success
:
success
,
error
:
fail
});
});
</script>
{% endblock %}
apps/users/utils.py
View file @
7984806b
...
...
@@ -128,6 +128,28 @@ def send_reset_password_mail(user):
send_mail_async
.
delay
(
subject
,
message
,
recipient_list
,
html_message
=
message
)
def
send_reset_ssh_key_mail
(
user
):
subject
=
_
(
'SSH Key Reset'
)
recipient_list
=
[
user
.
email
]
message
=
_
(
"""
Hello
%(name)
s:
</br>
Your ssh public key has been reset by site administrator.
Please login and reset your ssh public key.
</br>
<a href="
%(login_url)
s">Login direct</a>
</br>
"""
)
%
{
'name'
:
user
.
name
,
'login_url'
:
reverse
(
'users:login'
,
external
=
True
),
}
if
settings
.
DEBUG
:
logger
.
debug
(
message
)
send_mail_async
.
delay
(
subject
,
message
,
recipient_list
,
html_message
=
message
)
def
validate_ssh_pk
(
text
):
"""
Expects a SSH private key as string.
...
...
apps/users/views.py
View file @
7984806b
...
...
@@ -2,8 +2,6 @@
from
__future__
import
unicode_literals
import
logging
from
django.conf
import
settings
from
django.contrib.auth
import
login
as
auth_login
,
logout
as
auth_logout
from
django.contrib.auth.mixins
import
LoginRequiredMixin
...
...
@@ -52,10 +50,6 @@ class UserLoginView(FormView):
auth_login
(
self
.
request
,
form
.
get_user
())
return
redirect
(
self
.
get_success_url
())
def
form_invalid
(
self
,
form
):
logger
.
debug
(
form
.
errors
)
return
super
(
UserLoginView
,
self
)
.
form_invalid
(
form
)
def
get_success_url
(
self
):
if
self
.
request
.
user
.
is_first_login
:
return
reverse
(
'users:user-first-login'
)
...
...
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