Merge with master

pull/530/head
ibuler 2016-09-17 18:53:11 +08:00
commit dc4d388d9a
5 changed files with 69 additions and 45 deletions

View File

@ -6,7 +6,6 @@ import logging
from rest_framework import generics from rest_framework import generics
from .serializers import UserSerializer, UserGroupSerializer, UserAttributeSerializer, UserGroupEditSerializer from .serializers import UserSerializer, UserGroupSerializer, UserAttributeSerializer, UserGroupEditSerializer
from .serializers import UserPKUpdateSerializer
from .models import User, UserGroup from .models import User, UserGroup
@ -60,15 +59,20 @@ class UserResetPasswordApi(generics.UpdateAPIView):
# Note: we are not updating the user object here. # Note: we are not updating the user object here.
# We just do the reset-password staff. # We just do the reset-password staff.
user = self.get_object() user = self.get_object()
import uuid
user.password_raw = str(uuid.uuid4())
user.save()
from .utils import send_reset_password_mail from .utils import send_reset_password_mail
send_reset_password_mail(user) send_reset_password_mail(user)
class UserResetPKApi(generics.UpdateAPIView): class UserResetPKApi(generics.UpdateAPIView):
queryset = User.objects.all() queryset = User.objects.all()
serializer_class = UserPKUpdateSerializer serializer_class = UserGroupEditSerializer
def perform_update(self, serializer): def perform_update(self, serializer):
user = self.get_object() user = self.get_object()
user.private_key = serializer.validated_data['_private_key'] user._public_key = ''
user.save() user.save()
from .utils import send_reset_ssh_key_mail
send_reset_ssh_key_mail(user)

View File

@ -73,15 +73,25 @@ class UserInfoForm(forms.Form):
class UserKeyForm(forms.Form): class UserKeyForm(forms.Form):
private_key = forms.CharField(max_length=5000, widget=forms.Textarea, label=_('private key')) 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_private_key(self): def clean_public_key(self):
from users.utils import validate_ssh_pk from sshpubkeys import SSHKey
ssh_pk = self.cleaned_data['private_key'] from sshpubkeys.exceptions import InvalidKeyException
checked, reason = validate_ssh_pk(ssh_pk) public_key = self.cleaned_data['public_key']
if not checked: ssh = SSHKey(public_key)
raise forms.ValidationError(_('Not a valid ssh private key.')) try:
return ssh_pk 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
class UserPrivateAssetPermissionForm(forms.ModelForm): class UserPrivateAssetPermissionForm(forms.ModelForm):
@ -106,4 +116,3 @@ class UserPrivateAssetPermissionForm(forms.ModelForm):
'system_users': forms.SelectMultiple(attrs={'class': 'select2', 'system_users': forms.SelectMultiple(attrs={'class': 'select2',
'data-placeholder': _('Select system users')}), 'data-placeholder': _('Select system users')}),
} }

View File

@ -156,7 +156,7 @@
<td>{% trans 'Reset ssh key' %}:</td> <td>{% trans 'Reset ssh key' %}:</td>
<td> <td>
<span class="pull-right"> <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> </span>
</td> </td>
</tr> </tr>
@ -207,7 +207,6 @@
</div> </div>
</div> </div>
</div> </div>
{% include 'users/_user_reset_pk_modal.html' %}
{% endblock %} {% endblock %}
{% block custom_foot_js %} {% block custom_foot_js %}
<script> <script>
@ -308,34 +307,28 @@ $(document).ready(function () {
doReset(); doReset();
} }
); );
}).on('click', '#btn_user_reset_pk', function(){ }).on('click', '#btn_reset_pk', function(){
var $this = $(this); function doReset() {
var pk = $('#txt_pk').val(); var the_url = '{% url "users:user-reset-pk-api" pk=user_object.id %}';
var the_url = '{% url "users:user-reset-pk-api" pk=user_object.id %}'; var body = {};
var body = {'_private_key': pk}; var success = function() {
var success = function() { 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.' %}";
$('#txt_pk').val(''); swal("{% trans 'SSH-Public-Key Reset' %}", msg, "success");
$this.closest('.modal').modal('hide'); }
var msg = "{% trans 'Successfully updated the SSH private key.' %}"; APIUpdateAttr({ url: the_url, body: JSON.stringify(body), success: success});
swal("{% trans 'User SSH Private Key Reset' %}", msg, "success");
};
var fail = function() {
var msg = "{% trans 'Failed to update the user\'s SSH private key.' %}";
swal({
title: "{% trans 'User SSH Private Key Reset' %}",
text: msg,
type: "error",
showCancelButton: false,
confirmButtonColor: "#DD6B55",
confirmButtonText: "{% trans 'Confirm' %}",
closeOnConfirm: true
}, function () {
$('#txt_pk').focus();
}
);
} }
APIUpdateAttr({ url: the_url, body: JSON.stringify(body), success: success, error: fail}); swal({
title: "{% trans 'Are you sure?' %}",
text: "{% trans 'This will reset the user\'s public key.' %}",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "{% trans 'Confirm' %}",
closeOnConfirm: false
}, function () {
doReset();
}
);
}); });
</script> </script>
{% endblock %} {% endblock %}

View File

@ -128,6 +128,28 @@ def send_reset_password_mail(user):
send_mail_async.delay(subject, message, recipient_list, html_message=message) 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): def validate_ssh_pk(text):
""" """
Expects a SSH private key as string. Expects a SSH private key as string.

View File

@ -51,10 +51,6 @@ class UserLoginView(FormView):
auth_login(self.request, form.get_user()) auth_login(self.request, form.get_user())
return redirect(self.get_success_url()) 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): def get_success_url(self):
if self.request.user.is_first_login: if self.request.user.is_first_login:
return reverse('users:user-first-login') return reverse('users:user-first-login')