1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-25 23:02:26 +00:00

[lib decrypt] bugfix, improvement

This commit is contained in:
llj
2016-03-21 17:16:57 +08:00
parent 69cbd56064
commit 05f3d353bf
4 changed files with 72 additions and 62 deletions

View File

@@ -39,7 +39,7 @@ class RepoSetPassword(APIView):
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
elif e.msg == 'Incorrect password':
error_msg = _(u'Wrong password')
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
elif e.msg == 'Internal server error':
error_msg = _(u'Internal server error')
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

View File

@@ -5,14 +5,11 @@
<div class="wide-panel">
<h2>{{repo.name}}</h2>
<p class="access-notice">{% trans "This library is encrypted. Please input the password if you want to browse it online. And the password will be kept on the server for only 1 hour." %}</p>
<form action="" method="post" id="repo-decrypt-form">{% csrf_token %}
<form action="" method="post" id="repo-decrypt-form">
<label>{% trans "Password: " %}</label>
<input type="password" name="password" autofocus />
<input type="submit" value="{% trans "Submit" %}" />
<p class="error hide"></p>
{% for error in form.errors.values %}
{{ error|escape }}
{% endfor %}
{% if not force_server_crypto %}
{% url 'edit_profile' as profile_edit_url %}
@@ -24,32 +21,38 @@
{% block extra_script %}
<script type="text/javascript">
$('#repo-decrypt-form').submit(function() {
var form = $(this),
form_id = $(this).attr('id');
password = $('[name="password"]', form).val(),
err = $('.error',form),
repo_id = "{{ repo.id }}",
username = "{{ request.user.username }}",
redirect_url = "{{ next }}";
var $form = $(this),
password = $.trim($('[name="password"]', $form).val()),
$error = $('.error', $form);
if (!$.trim(password)) {
err.html("{% trans "Please enter the password." %}").removeClass('hide');
return false;
if (!password) {
$error.html("{% trans "Please enter the password." %}").removeClass('hide');
return false;
}
var $submitBtn = $('[type="submit"]', $form);
disable($submitBtn);
$.ajax({
type: "POST",
url: "{% url 'api-v2.1-repo-set-password' repo.id %}",
type: "POST",
dataType: 'json',
cache: false,
beforeSend: prepareCSRFToken,
data: {password: password},
success: function(data) {
location.href = redirect_url;
data: {
password: password
},
error: function(jqXHR, textStatus, errorThrown) {
var error_msg = $.parseJSON(jqXHR.responseText).error_msg;
apply_form_error(form_id, error_msg);
success: function() {
location.href = "{{ next|escapejs }}";
},
error: function(xhr) {
var error;
if (xhr.responseText) {
error = $.parseJSON(xhr.responseText).error_msg;
} else {
error = "{% trans "Failed. Please check the network." %}";
}
$error.html(error).removeClass('hide');
enable($submitBtn);
}
});

View File

@@ -125,14 +125,13 @@
</div>
{% if not err and repo.encrypted %}
<form id="repo-decrypt-form" class="hide">
<form id="repo-decrypt-form" class="hide" action="" method="post">
<h3>{% trans 'Library' %} <span class="op-target">{{repo.name}}</span> {% trans 'is encrypted' %}</h3>
<label>{% trans 'Password' %}</label><br />
<input type="password" name="password" class="input" />
<p class="tip">{% trans "The password will be kept in the server for only 1 hour." %}</p>
<p class="error"></p>
<p class="error hide"></p>
<input type="submit" class="submit" value="{% trans 'Submit' %}" />
<button class="simplemodal-close">{% trans 'Cancel' %}</button>
</form>
{% endif %}
@@ -405,44 +404,49 @@ $('#simplemodal-container').css({'height':'auto'});
{% endif %}
$('#repo-decrypt-form').submit(function() {
var pwd_input = $(this).find('input[name="password"]');
var pwd = $.trim(pwd_input.val());
var err = $(this).find('.error');
var $form = $(this);
var $pwd_input = $('input[name="password"]', $form);
var pwd = $.trim($pwd_input.val());
var $error = $('.error', $form);
if (!pwd) {
err.html("{% trans "Password is required." %}");
} else {
$.ajax({
url: '{% url 'api-v2.1-repo-set-password' repo.id %}',
type: 'POST',
dataType: 'json',
cache: 'false',
beforeSend: prepareCSRFToken,
data: {
password: pwd
},
success: function(data) {
if (data['success']) {
$.modal.close();
{% if file_content != None %}
editSubmit();
{% endif %}
{% if op == 'decrypt' %}
location.reload(true);
{% endif %}
}
},
error: function(jqXHR, textStatus, errorThrown) {
var err_str = '';
if (jqXHR.responseText) {
err_str = $.parseJSON(jqXHR.responseText).error_msg;
} else {
err_str = "{% trans "Failed. Please check the network." %}";
}
err.html(err_str);
pwd_input.val('');
}
});
$error.html("{% trans "Password is required." %}").removeClass('hide');
return false;
}
var $submitBtn = $('[type="submit"]', $form);
disable($submitBtn);
$.ajax({
url: '{% url 'api-v2.1-repo-set-password' repo.id %}',
type: 'POST',
dataType: 'json',
cache: false,
beforeSend: prepareCSRFToken,
data: {
password: pwd
},
success: function(data) {
if (data['success']) {
$.modal.close();
{% if file_content != None %}
editSubmit();
{% endif %}
{% if op == 'decrypt' %}
location.reload(true);
{% endif %}
}
},
error: function(xhr) {
var err = '';
if (xhr.responseText) {
err = $.parseJSON(xhr.responseText).error_msg;
} else {
err = "{% trans "Failed. Please check the network." %}";
}
$error.html(err).removeClass('hide');
enable($submitBtn);
}
});
return false;
});

View File

@@ -299,7 +299,10 @@ define([
Common.ajaxPost({
form: form,
form_id: form.attr('id'),
post_url: Common.getUrl({'name':'api_v2.1_repo_set_password', repo_id: _this.dir.repo_id}),
post_url: Common.getUrl({
'name': 'api_v2.1_repo_set_password',
'repo_id': _this.dir.repo_id
}),
post_data: {
password: passwd
},