MDL-34498 session: Do not extend expired session

After showing a warning, if the "extend session" button is clicked too late,
tell them their session is expired so they can collect any unsaved data in the page.
This commit is contained in:
Damyon Wiese 2019-05-06 14:14:45 +08:00
parent ceacef1bd8
commit 00fd749041
2 changed files with 38 additions and 11 deletions

View File

@ -1 +1 @@
define(["jquery","core/ajax","core/config","core/notification","core/str"],function(a,b,c,d,e){var f=!1,g=!1,h=0,i=0,j=!1,k=c.sessiontimeout/10*1e3,l=2*k,m=function(){var a={methodname:"core_session_touch",args:{}};return b.call([a],!0,!0,!1,i)[0].then(function(){return h>0&&setTimeout(m,h),!0}).fail(function(){d.alert("",j)})},n=function(){var a={methodname:"core_session_time_remaining",args:{}};return b.call([a],!0,!0,!0)[0].then(function(a){return!(a.userid<=0)&&(a.timeremaining<0?e.get_strings([{key:"sessionexpired",component:"error"},{key:"sessionerroruser",component:"error"}]).then(function(a){return d.alert(a[0],a[1]),!0}).fail(d.exception):1e3*a.timeremaining<l&&!g?(g=!0,e.get_strings([{key:"norecentactivity",component:"moodle"},{key:"sessiontimeoutsoon",component:"moodle"},{key:"extendsession",component:"moodle"},{key:"cancel",component:"moodle"}]).then(function(a){return d.confirm(a[0],a[1],a[2],a[3],function(){return m(),g=!1,setTimeout(n,5*k),!0},function(){g=!1,setTimeout(n,k)}),!0}).fail(d.exception)):setTimeout(n,k),!0)})},o=function(){h>0?setTimeout(m,h):setTimeout(n,5*k)},p=function(){f||(f=!0,o())},q=function(a,b,c){f||(f=!0,h=1e3*a,j=c,i=1e3*b,o())};return{keepalive:q,init:p}});
define(["jquery","core/ajax","core/config","core/notification","core/str"],function(a,b,c,d,e){var f=!1,g=!1,h=0,i=0,j=!1,k=!1,l=1e3*Math.min(c.sessiontimeout/10,600),m=2*l,n=function(){k=!0},o=function(){var a={methodname:"core_session_touch",args:{}};return k?e.get_strings([{key:"sessionexpired",component:"error"},{key:"sessionerroruser",component:"error"}]).then(function(a){return d.alert(a[0],a[1]),!0}).fail(d.exception):b.call([a],!0,!0,!1,i)[0].then(function(){return h>0&&setTimeout(o,h),!0}).fail(function(){d.alert("",j)})},p=function(){var a={methodname:"core_session_time_remaining",args:{}};return k=!1,b.call([a],!0,!0,!0)[0].then(function(a){return!(a.userid<=0)&&(a.timeremaining<0?e.get_strings([{key:"sessionexpired",component:"error"},{key:"sessionerroruser",component:"error"}]).then(function(a){return d.alert(a[0],a[1]),!0}).fail(d.exception):1e3*a.timeremaining<m&&!g?(setTimeout(n,1e3*a.timeremaining),g=!0,e.get_strings([{key:"norecentactivity",component:"moodle"},{key:"sessiontimeoutsoon",component:"moodle"},{key:"extendsession",component:"moodle"},{key:"cancel",component:"moodle"}]).then(function(a){return d.confirm(a[0],a[1],a[2],a[3],function(){return o(),g=!1,setTimeout(p,5*l),!0},function(){g=!1,setTimeout(p,l)}),!0}).fail(d.exception)):setTimeout(p,l),!0)})},q=function(){h>0?setTimeout(o,h):setTimeout(p,5*l)},r=function(){f||(f=!0,q())},s=function(a,b,c){f||(f=!0,h=1e3*a,j=c,i=1e3*b,q())};return{keepalive:s,init:r}});

View File

@ -29,8 +29,18 @@ define(['jquery', 'core/ajax', 'core/config', 'core/notification', 'core/str'],
var keepAliveFrequency = 0;
var requestTimeout = 0;
var keepAliveMessage = false;
var checkFrequency = (Config.sessiontimeout / 10) * 1000;
var warningLimit = checkFrequency * 2; // 1/5 of sessiontimeout.
var sessionTimeout = false;
// 1/10 of session timeout, max of 10 minutes.
var checkFrequency = Math.min((Config.sessiontimeout / 10), 600) * 1000;
// 1/5 of sessiontimeout.
var warningLimit = checkFrequency * 2;
/**
* The session time has expired - we can't extend it now.
*/
var timeoutSessionExpired = function() {
sessionTimeout = true;
};
/**
* Ping the server to keep the session alive.
@ -43,14 +53,28 @@ define(['jquery', 'core/ajax', 'core/config', 'core/notification', 'core/str'],
args: { }
};
return Ajax.call([request], true, true, false, requestTimeout)[0].then(function() {
if (keepAliveFrequency > 0) {
setTimeout(touchSession, keepAliveFrequency);
}
return true;
}).fail(function() {
Notification.alert('', keepAliveMessage);
});
if (sessionTimeout) {
// We timed out before we extended the session.
return Str.get_strings([
{key: 'sessionexpired', component: 'error'},
{key: 'sessionerroruser', component: 'error'}
]).then(function(strings) {
Notification.alert(
strings[0], // Title.
strings[1] // Message.
);
return true;
}).fail(Notification.exception);
} else {
return Ajax.call([request], true, true, false, requestTimeout)[0].then(function() {
if (keepAliveFrequency > 0) {
setTimeout(touchSession, keepAliveFrequency);
}
return true;
}).fail(function() {
Notification.alert('', keepAliveMessage);
});
}
};
/**
@ -65,6 +89,7 @@ define(['jquery', 'core/ajax', 'core/config', 'core/notification', 'core/str'],
args: { }
};
sessionTimeout = false;
return Ajax.call([request], true, true, true)[0].then(function(args) {
if (args.userid <= 0) {
return false;
@ -82,6 +107,8 @@ define(['jquery', 'core/ajax', 'core/config', 'core/notification', 'core/str'],
}).fail(Notification.exception);
} else if (args.timeremaining * 1000 < warningLimit && !warningDisplayed) {
// If we don't extend the session before the timeout - warn.
setTimeout(timeoutSessionExpired, args.timeremaining * 1000);
warningDisplayed = true;
Str.get_strings([
{key: 'norecentactivity', component: 'moodle'},