From b06211b6410d8012380a594779e26bcf6c36a9d0 Mon Sep 17 00:00:00 2001 From: Gert Date: Sat, 4 Apr 2015 22:34:34 +0200 Subject: [PATCH] redirect to login on unauthorized ajax calls [fixes #29] --- themes/grav/js/admin-all.js | 227 ++++++++++-------- themes/grav/templates/partials/base.html.twig | 1 + .../partials/javascript-config.html.twig | 6 + 3 files changed, 133 insertions(+), 101 deletions(-) create mode 100644 themes/grav/templates/partials/javascript-config.html.twig diff --git a/themes/grav/js/admin-all.js b/themes/grav/js/admin-all.js index 0e92e640..9addce9e 100644 --- a/themes/grav/js/admin-all.js +++ b/themes/grav/js/admin-all.js @@ -49,127 +49,152 @@ $(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) { - if (result.status == 'success') { - toastr.success(result.message); - } else { - toastr.error(result.message); + + 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, { - task: 'GPM', - action: 'getUpdates' - }, function (response) { - if (!response.success) { - throw new Error(response.message); - } + ajaxRequest({ + dataType: "JSON", + url: window.location.href, + method: "POST", + data: { + task: 'GPM', + action: 'getUpdates' + }, + success: function (response) { + if (!response.success) { + throw new Error(response.message); + } - var grav = response.payload.grav, - installed = response.payload.installed, - resources = response.payload.resources; + var grav = response.payload.grav, + installed = response.payload.installed, + resources = response.payload.resources; - //console.log(grav, resources); + //console.log(grav, resources); - // grav updatable - if (grav.isUpdatable) { - var icon = ' '; - content = 'Grav v{available} is now available! (Current: v{version}) ', - button = ''; + // grav updatable + if (grav.isUpdatable) { + var icon = ' '; + content = 'Grav v{available} is now available! (Current: v{version}) ', + button = ''; - content = jQuery.substitute(content, {available: grav.available, version: grav.version}); - $('[data-gpm-grav]').addClass('grav').html('

' + icon + content + button + '

'); - } + content = jQuery.substitute(content, {available: grav.available, version: grav.version}); + $('[data-gpm-grav]').addClass('grav').html('

' + icon + content + button + '

'); + } - // dashboard - if ($('.updates-chart').length) { - var missing = (resources.total + (grav.isUpdatable ? 1 : 0)) * 100 / (installed + (grav.isUpdatable ? 1 : 0)), - updated = 100 - missing; - UpdatesChart.update({series: [updated, missing]}); - } + // dashboard + if ($('.updates-chart').length) { + var missing = (resources.total + (grav.isUpdatable ? 1 : 0)) * 100 / (installed + (grav.isUpdatable ? 1 : 0)), + updated = 100 - missing; + UpdatesChart.update({series: [updated, missing]}); + } - if (resources.total > 0) { - var length, - icon = '', - content = '{updates} of your {type} have an update available', - button = '', - plugins = $('.grav-update.plugins'), - themes = $('.grav-update.themes'), - sidebar = {plugins: $('#admin-menu a[href$="/plugins"]'), themes: $('#admin-menu a[href$="/themes"]')}; + if (resources.total > 0) { + var length, + icon = '', + content = '{updates} of your {type} have an update available', + button = '', + plugins = $('.grav-update.plugins'), + themes = $('.grav-update.themes'), + sidebar = {plugins: $('#admin-menu a[href$="/plugins"]'), themes: $('#admin-menu a[href$="/themes"]')}; - // sidebar - if (sidebar.plugins.length || sidebar.themes.length) { - var length, badges; - if (sidebar.plugins.length && (length = Object.keys(resources.plugins).length)) { - badges = sidebar.plugins.find('.badges'); - badges.addClass('with-updates'); - badges.find('.badge.updates').text(length); + // sidebar + if (sidebar.plugins.length || sidebar.themes.length) { + var length, badges; + if (sidebar.plugins.length && (length = Object.keys(resources.plugins).length)) { + badges = sidebar.plugins.find('.badges'); + badges.addClass('with-updates'); + badges.find('.badge.updates').text(length); + } + + if (sidebar.themes.length && (length = Object.keys(resources.themes).length)) { + badges = sidebar.themes.find('.badges'); + badges.addClass('with-updates'); + badges.find('.badge.updates').text(length); + } } - if (sidebar.themes.length && (length = Object.keys(resources.themes).length)) { - badges = sidebar.themes.find('.badges'); - badges.addClass('with-updates'); - badges.find('.badge.updates').text(length); + // list page + if (plugins[0] && (length = Object.keys(resources.plugins).length)) { + content = jQuery.substitute(content, {updates: length, type: 'plugins'}); + button = jQuery.substitute(button, {Type: 'All Plugins'}); + plugins.html('

' + icon + content + button + '

'); + + var plugin, url; + $.each(resources.plugins, function (key, value) { + plugin = $('[data-gpm-plugin="' + key + '"] .gpm-name'); + url = plugin.find('a'); + plugin.append('Update available!'); + + }); + } + + if (themes[0] && (length = Object.keys(resources.themes).length)) { + content = jQuery.substitute(content, {updates: length, type: 'themes'}); + button = jQuery.substitute(button, {Type: 'All Themes'}); + themes.html('

' + icon + content + button + '

'); + + var theme, url; + $.each(resources.themes, function (key, value) { + theme = $('[data-gpm-theme="' + key + '"]'); + url = theme.find('.gpm-name a'); + theme.append('
UPDATE
'); + }); + } + + // details page + var type = 'plugin', + details = $('.grav-update.plugin')[0]; + + if (!details) { + details = $('.grav-update.theme')[0]; + type = 'theme'; + } + + if (details){ + var slug = $('[data-gpm-' + type + ']').data('gpm-' + type), + Type = type.charAt(0).toUpperCase() + type.substring(1); + + content = 'v{available} of this ' + type + ' is now available!'; + content = jQuery.substitute(content, {available: resources[type + 's'][slug].available}); + button = jQuery.substitute(button, {Type: Type}); + $(details).html('

' + icon + content + button + '

'); } } - - // list page - if (plugins[0] && (length = Object.keys(resources.plugins).length)) { - content = jQuery.substitute(content, {updates: length, type: 'plugins'}); - button = jQuery.substitute(button, {Type: 'All Plugins'}); - plugins.html('

' + icon + content + button + '

'); - - var plugin, url; - $.each(resources.plugins, function (key, value) { - plugin = $('[data-gpm-plugin="' + key + '"] .gpm-name'); - url = plugin.find('a'); - plugin.append('Update available!'); - - }); - } - - if (themes[0] && (length = Object.keys(resources.themes).length)) { - content = jQuery.substitute(content, {updates: length, type: 'themes'}); - button = jQuery.substitute(button, {Type: 'All Themes'}); - themes.html('

' + icon + content + button + '

'); - - var theme, url; - $.each(resources.themes, function (key, value) { - theme = $('[data-gpm-theme="' + key + '"]'); - url = theme.find('.gpm-name a'); - theme.append('
UPDATE
'); - }); - } - - // details page - var type = 'plugin', - details = $('.grav-update.plugin')[0]; - - if (!details) { - details = $('.grav-update.theme')[0]; - type = 'theme'; - } - - if (details){ - var slug = $('[data-gpm-' + type + ']').data('gpm-' + type), - Type = type.charAt(0).toUpperCase() + type.substring(1); - - content = 'v{available} of this ' + type + ' is now available!'; - content = jQuery.substitute(content, {available: resources[type + 's'][slug].available}); - button = jQuery.substitute(button, {Type: Type}); - $(details).html('

' + icon + content + button + '

'); - } } - - }, 'json'); + }); }); diff --git a/themes/grav/templates/partials/base.html.twig b/themes/grav/templates/partials/base.html.twig index 414ed4fe..f730f585 100644 --- a/themes/grav/templates/partials/base.html.twig +++ b/themes/grav/templates/partials/base.html.twig @@ -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') %} diff --git a/themes/grav/templates/partials/javascript-config.html.twig b/themes/grav/templates/partials/javascript-config.html.twig new file mode 100644 index 00000000..c5014e97 --- /dev/null +++ b/themes/grav/templates/partials/javascript-config.html.twig @@ -0,0 +1,6 @@ + \ No newline at end of file