From 0e2317032be167a0c9df164ab4b8327d270477a4 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Mon, 14 Sep 2015 15:58:34 +0200 Subject: [PATCH] Keepalive in admin. Fires a 'keepAlive' task 5s before the end of the admin session --- classes/controller.php | 10 ++++++++++ themes/grav/js/admin-all.js | 10 ++++++++++ .../templates/partials/javascript-config.html.twig | 3 ++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/classes/controller.php b/classes/controller.php index 4385ca7b..067e67df 100644 --- a/classes/controller.php +++ b/classes/controller.php @@ -193,6 +193,16 @@ class AdminController return true; } + /** + * Handle logout. + * + * @return bool True if the action was performed. + */ + protected function taskKeepAlive() + { + exit(); + } + /** * Handle the email password recovery procedure. * diff --git a/themes/grav/js/admin-all.js b/themes/grav/js/admin-all.js index 60c1b531..670246c8 100644 --- a/themes/grav/js/admin-all.js +++ b/themes/grav/js/admin-all.js @@ -20,6 +20,10 @@ var bytesToSize = function(bytes) { var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1; +var keepAlive = function keepAlive() { + $.post(GravAdmin.config.base_url_relative + '/task' + GravAdmin.config.param_sep + 'keepAlive'); +}; + $(function () { jQuery.substitute = function(str, sub) { return str.replace(/\{(.+?)\}/g, function($0, $1) { @@ -513,4 +517,10 @@ $(function () { remodal.find('strong').text(name); remodal.find('.button.continue').attr('href', $(e.target).attr('href')); }); + + // Keep-alive + setInterval(function() { + keepAlive(); + }, GravAdmin.config.admin_timeout*1000 - 5*1000); //Call keepAlive() 5s before the admin session timeout + }); diff --git a/themes/grav/templates/partials/javascript-config.html.twig b/themes/grav/templates/partials/javascript-config.html.twig index 36e6eb00..5ee1078a 100644 --- a/themes/grav/templates/partials/javascript-config.html.twig +++ b/themes/grav/templates/partials/javascript-config.html.twig @@ -3,6 +3,7 @@ window.GravAdmin.config = { base_url_relative: '{{ base_url_relative }}', param_sep: '{{ config.system.param_sep }}', - enable_auto_updates_check: '{{ config.plugins.admin.enable_auto_updates_check }}' + enable_auto_updates_check: '{{ config.plugins.admin.enable_auto_updates_check }}', + admin_timeout: '{{ config.plugins.admin.session.timeout ?: 1800 }}' };