mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-11-17 02:31:02 +01:00
redirect to login on unauthorized ajax calls [fixes #29]
This commit is contained in:
@@ -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
|
||||
$('[data-clear-cache]').on('click', function(e) {
|
||||
|
||||
$(this).attr('disabled','disabled').find('> .fa').removeClass('fa-trash').addClass('fa-refresh fa-spin');
|
||||
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') {
|
||||
toastr.success(result.message);
|
||||
} else {
|
||||
toastr.error(result.message);
|
||||
}
|
||||
});
|
||||
jqxhr.complete(function() {
|
||||
}
|
||||
}).complete(function() {
|
||||
$('[data-clear-cache]').removeAttr('disabled').find('> .fa').removeClass('fa-refresh fa-spin').addClass('fa-trash');
|
||||
});
|
||||
});
|
||||
|
||||
// GPM
|
||||
$.post(window.location.href, {
|
||||
ajaxRequest({
|
||||
dataType: "JSON",
|
||||
url: window.location.href,
|
||||
method: "POST",
|
||||
data: {
|
||||
task: 'GPM',
|
||||
action: 'getUpdates'
|
||||
}, function (response) {
|
||||
},
|
||||
success: function (response) {
|
||||
if (!response.success) {
|
||||
throw new Error(response.message);
|
||||
}
|
||||
@@ -170,6 +195,6 @@ $(function () {
|
||||
$(details).html('<p>' + icon + content + button + '</p>');
|
||||
}
|
||||
}
|
||||
|
||||
}, 'json');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
{{ assets.css() }}
|
||||
{% endblock %}
|
||||
|
||||
{% include 'partials/javascript-config.html.twig' %}
|
||||
{% block javascripts %}
|
||||
{% do assets.add('jquery',101) %}
|
||||
{% do assets.addJs(theme_url~'/js/modernizr.custom.71422.js') %}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<script type="text/javascript">
|
||||
window.GravAdmin = window.GravAdmin || {};
|
||||
window.GravAdmin.config = {
|
||||
base_url_relative: '{{ base_url_relative }}'
|
||||
};
|
||||
</script>
|
||||
Reference in New Issue
Block a user