redirect to login on unauthorized ajax calls [fixes #29]

This commit is contained in:
Gert
2015-04-04 22:34:34 +02:00
parent 2ec9946a70
commit b06211b641
3 changed files with 133 additions and 101 deletions

View File

@@ -49,28 +49,53 @@ $(function () {
}); });
} }
var ajaxRequest = function (url, settings) {
settings = typeof settings === 'undefined' ? typeof url === 'string' ? {} : url : settings;
settings.url = typeof settings.url === 'undefined' && typeof url === 'string' ? url : settings.url;
var successHandler = typeof settings.success !== 'undefined' ? typeof settings.success === 'function' ? [ settings.success ] : settings.success : [];
successHandler.unshift(ajaxRequest.logoutHandler);
settings.success = successHandler;
return $.ajax(settings);
};
ajaxRequest.logoutHandler = function (response, status, xhr) {
if (response.status && (response.status === "unauthorized" || response.status === "forbidden")) {
document.location.href = GravAdmin.config.base_url_relative;
throw "Logged out";
}
};
// Cache Clear // Cache Clear
$('[data-clear-cache]').on('click', function(e) { $('[data-clear-cache]').on('click', function(e) {
$(this).attr('disabled','disabled').find('> .fa').removeClass('fa-trash').addClass('fa-refresh fa-spin'); $(this).attr('disabled','disabled').find('> .fa').removeClass('fa-trash').addClass('fa-refresh fa-spin');
var url = $(this).data('clearCache'); var url = $(this).data('clearCache');
var jqxhr = $.getJSON(url, function(result, status) {
ajaxRequest({
dataType: "json",
url: url,
success: function(result, status) {
if (result.status == 'success') { if (result.status == 'success') {
toastr.success(result.message); toastr.success(result.message);
} else { } else {
toastr.error(result.message); toastr.error(result.message);
} }
}); }
jqxhr.complete(function() { }).complete(function() {
$('[data-clear-cache]').removeAttr('disabled').find('> .fa').removeClass('fa-refresh fa-spin').addClass('fa-trash'); $('[data-clear-cache]').removeAttr('disabled').find('> .fa').removeClass('fa-refresh fa-spin').addClass('fa-trash');
}); });
}); });
// GPM ajaxRequest({
$.post(window.location.href, { dataType: "JSON",
url: window.location.href,
method: "POST",
data: {
task: 'GPM', task: 'GPM',
action: 'getUpdates' action: 'getUpdates'
}, function (response) { },
success: function (response) {
if (!response.success) { if (!response.success) {
throw new Error(response.message); throw new Error(response.message);
} }
@@ -170,6 +195,6 @@ $(function () {
$(details).html('<p>' + icon + content + button + '</p>'); $(details).html('<p>' + icon + content + button + '</p>');
} }
} }
}
}, 'json'); });
}); });

View File

@@ -29,6 +29,7 @@
{{ assets.css() }} {{ assets.css() }}
{% endblock %} {% endblock %}
{% include 'partials/javascript-config.html.twig' %}
{% block javascripts %} {% block javascripts %}
{% do assets.add('jquery',101) %} {% do assets.add('jquery',101) %}
{% do assets.addJs(theme_url~'/js/modernizr.custom.71422.js') %} {% do assets.addJs(theme_url~'/js/modernizr.custom.71422.js') %}

View File

@@ -0,0 +1,6 @@
<script type="text/javascript">
window.GravAdmin = window.GravAdmin || {};
window.GravAdmin.config = {
base_url_relative: '{{ base_url_relative }}'
};
</script>