1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Poll the server to keep the session alive.
|
|
|
18 |
*
|
|
|
19 |
* @module core/network
|
|
|
20 |
* @copyright 2019 Damyon Wiese
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define(['jquery', 'core/ajax', 'core/config', 'core/notification', 'core/str'],
|
|
|
24 |
function($, Ajax, Config, Notification, Str) {
|
|
|
25 |
|
|
|
26 |
var started = false;
|
|
|
27 |
var warningDisplayed = false;
|
|
|
28 |
var keepAliveFrequency = 0;
|
|
|
29 |
var requestTimeout = 0;
|
|
|
30 |
var keepAliveMessage = false;
|
|
|
31 |
var sessionTimeout = false;
|
|
|
32 |
// 1/10 of session timeout, max of 10 minutes.
|
|
|
33 |
var checkFrequency = Math.min((Config.sessiontimeout / 10), 600) * 1000;
|
|
|
34 |
// Check if sessiontimeoutwarning is set or double the checkFrequency.
|
|
|
35 |
var warningLimit = (Config.sessiontimeoutwarning > 0) ? (Config.sessiontimeoutwarning * 1000) : (checkFrequency * 2);
|
|
|
36 |
// First wait is minimum of remaining time or half of the session timeout.
|
|
|
37 |
var firstWait = (Config.sessiontimeoutwarning > 0) ?
|
|
|
38 |
Math.min((Config.sessiontimeout - Config.sessiontimeoutwarning) * 1000, checkFrequency * 5) : checkFrequency * 5;
|
|
|
39 |
/**
|
|
|
40 |
* The session time has expired - we can't extend it now.
|
|
|
41 |
* @param {Modal} modal
|
|
|
42 |
*/
|
|
|
43 |
var timeoutSessionExpired = function(modal) {
|
|
|
44 |
sessionTimeout = true;
|
|
|
45 |
warningDisplayed = false;
|
|
|
46 |
closeModal(modal);
|
|
|
47 |
displaySessionExpired();
|
|
|
48 |
};
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Close modal - this relies on modal object passed from Notification.confirm.
|
|
|
52 |
*
|
|
|
53 |
* @param {Modal} modal
|
|
|
54 |
*/
|
|
|
55 |
var closeModal = function(modal) {
|
|
|
56 |
modal.destroy();
|
|
|
57 |
};
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* The session time has expired - we can't extend it now.
|
|
|
61 |
* @return {Promise}
|
|
|
62 |
*/
|
|
|
63 |
var displaySessionExpired = function() {
|
|
|
64 |
// Check again if its already extended before displaying session expired popup in case multiple tabs are open.
|
|
|
65 |
var request = {
|
|
|
66 |
methodname: 'core_session_time_remaining',
|
|
|
67 |
args: { }
|
|
|
68 |
};
|
|
|
69 |
|
|
|
70 |
return Ajax.call([request], true, true, true)[0].then(function(args) {
|
|
|
71 |
if (args.timeremaining * 1000 > warningLimit) {
|
|
|
72 |
return false;
|
|
|
73 |
} else {
|
|
|
74 |
return Str.get_strings([
|
|
|
75 |
{key: 'sessionexpired', component: 'error'},
|
|
|
76 |
{key: 'sessionerroruser', component: 'error'},
|
|
|
77 |
{key: 'loginagain', component: 'moodle'},
|
|
|
78 |
{key: 'cancel', component: 'moodle'}
|
|
|
79 |
]).then(function(strings) {
|
|
|
80 |
Notification.confirm(
|
|
|
81 |
strings[0], // Title.
|
|
|
82 |
strings[1], // Message.
|
|
|
83 |
strings[2], // Login Again.
|
|
|
84 |
strings[3], // Cancel.
|
|
|
85 |
function() {
|
|
|
86 |
location.reload();
|
|
|
87 |
return true;
|
|
|
88 |
}
|
|
|
89 |
);
|
|
|
90 |
return true;
|
|
|
91 |
}).catch(Notification.exception);
|
|
|
92 |
}
|
|
|
93 |
});
|
|
|
94 |
};
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Ping the server to keep the session alive.
|
|
|
98 |
*
|
|
|
99 |
* @return {Promise}
|
|
|
100 |
*/
|
|
|
101 |
var touchSession = function() {
|
|
|
102 |
var request = {
|
|
|
103 |
methodname: 'core_session_touch',
|
|
|
104 |
args: { }
|
|
|
105 |
};
|
|
|
106 |
|
|
|
107 |
if (sessionTimeout) {
|
|
|
108 |
// We timed out before we extended the session.
|
|
|
109 |
return displaySessionExpired();
|
|
|
110 |
} else {
|
|
|
111 |
return Ajax.call([request], true, true, false, requestTimeout)[0].then(function() {
|
|
|
112 |
if (keepAliveFrequency > 0) {
|
|
|
113 |
setTimeout(touchSession, keepAliveFrequency);
|
|
|
114 |
}
|
|
|
115 |
return true;
|
|
|
116 |
}).catch(function() {
|
|
|
117 |
Notification.alert('', keepAliveMessage);
|
|
|
118 |
});
|
|
|
119 |
}
|
|
|
120 |
};
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Ask the server how much time is remaining in this session and
|
|
|
124 |
* show confirm/cancel notifications if the session is about to run out.
|
|
|
125 |
*
|
|
|
126 |
* @return {Promise}
|
|
|
127 |
*/
|
|
|
128 |
var checkSession = function() {
|
|
|
129 |
var request = {
|
|
|
130 |
methodname: 'core_session_time_remaining',
|
|
|
131 |
args: { }
|
|
|
132 |
};
|
|
|
133 |
sessionTimeout = false;
|
|
|
134 |
return Ajax.call([request], true, true, true)[0].then(function(args) {
|
|
|
135 |
if (args.userid <= 0) {
|
|
|
136 |
return false;
|
|
|
137 |
}
|
|
|
138 |
if (args.timeremaining <= 0) {
|
|
|
139 |
return displaySessionExpired();
|
|
|
140 |
} else if (args.timeremaining * 1000 <= warningLimit && !warningDisplayed) {
|
|
|
141 |
warningDisplayed = true;
|
|
|
142 |
Str.get_strings([
|
|
|
143 |
{key: 'norecentactivity', component: 'moodle'},
|
|
|
144 |
{key: 'sessiontimeoutsoon', component: 'moodle'},
|
|
|
145 |
{key: 'extendsession', component: 'moodle'},
|
|
|
146 |
{key: 'cancel', component: 'moodle'}
|
|
|
147 |
]).then(function(strings) {
|
|
|
148 |
return Notification.confirm(
|
|
|
149 |
strings[0], // Title.
|
|
|
150 |
strings[1], // Message.
|
|
|
151 |
strings[2], // Extend session.
|
|
|
152 |
strings[3], // Cancel.
|
|
|
153 |
function() {
|
|
|
154 |
touchSession();
|
|
|
155 |
warningDisplayed = false;
|
|
|
156 |
// First wait is minimum of remaining time or half of the session timeout.
|
|
|
157 |
setTimeout(checkSession, firstWait);
|
|
|
158 |
return true;
|
|
|
159 |
},
|
|
|
160 |
function() {
|
|
|
161 |
// User has cancelled notification.
|
|
|
162 |
setTimeout(checkSession, checkFrequency);
|
|
|
163 |
}
|
|
|
164 |
);
|
|
|
165 |
}).then(modal => {
|
|
|
166 |
// If we don't extend the session before the timeout - warn.
|
|
|
167 |
setTimeout(timeoutSessionExpired, args.timeremaining * 1000, modal);
|
|
|
168 |
return;
|
|
|
169 |
}).catch(Notification.exception);
|
|
|
170 |
} else {
|
|
|
171 |
setTimeout(checkSession, checkFrequency);
|
|
|
172 |
}
|
|
|
173 |
return true;
|
|
|
174 |
});
|
|
|
175 |
// We do not catch the fails from the above ajax call because they will fail when
|
|
|
176 |
// we are not logged in - we don't need to take any action then.
|
|
|
177 |
};
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* Start calling a function to check if the session is still alive.
|
|
|
181 |
*/
|
|
|
182 |
var start = function() {
|
|
|
183 |
if (keepAliveFrequency > 0) {
|
|
|
184 |
setTimeout(touchSession, keepAliveFrequency);
|
|
|
185 |
} else {
|
|
|
186 |
// First wait is minimum of remaining time or half of the session timeout.
|
|
|
187 |
setTimeout(checkSession, firstWait);
|
|
|
188 |
}
|
|
|
189 |
};
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Are we in an iframe and the parent page is from the same Moodle site?
|
|
|
193 |
*
|
|
|
194 |
* @return {boolean} true if we are in an iframe in a page from this Moodle site.
|
|
|
195 |
*/
|
|
|
196 |
const isMoodleIframe = function() {
|
|
|
197 |
if (window.parent === window) {
|
|
|
198 |
// Not in an iframe.
|
|
|
199 |
return false;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
// We are in an iframe. Is the parent from the same Moodle site?
|
|
|
203 |
let parentUrl;
|
|
|
204 |
try {
|
|
|
205 |
parentUrl = window.parent.location.href;
|
|
|
206 |
} catch (e) {
|
|
|
207 |
// If we cannot access the URL of the parent page, it must be another site.
|
|
|
208 |
return false;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
return parentUrl.startsWith(M.cfg.wwwroot);
|
|
|
212 |
};
|
|
|
213 |
|
|
|
214 |
/**
|
|
|
215 |
* Don't allow more than one of these polling loops in a single page.
|
|
|
216 |
*/
|
|
|
217 |
var init = function() {
|
|
|
218 |
// We only allow one concurrent instance of this checker.
|
|
|
219 |
if (started) {
|
|
|
220 |
return;
|
|
|
221 |
}
|
|
|
222 |
started = true;
|
|
|
223 |
|
|
|
224 |
if (isMoodleIframe()) {
|
|
|
225 |
window.console.log('Not starting Moodle session timeout warning in this iframe.');
|
|
|
226 |
return;
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
window.console.log('Starting Moodle session timeout warning.');
|
|
|
230 |
|
|
|
231 |
start();
|
|
|
232 |
};
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* Start polling with more specific values for the frequency, timeout and message.
|
|
|
236 |
*
|
|
|
237 |
* @param {number} freq How ofter to poll the server.
|
|
|
238 |
* @param {number} timeout The time to wait for each request to the server.
|
|
|
239 |
* @param {string} identifier The string identifier for the message to show if session is going to time out.
|
|
|
240 |
* @param {string} component The string component for the message to show if session is going to time out.
|
|
|
241 |
*/
|
|
|
242 |
var keepalive = async function(freq, timeout, identifier, component) {
|
|
|
243 |
// We only allow one concurrent instance of this checker.
|
|
|
244 |
if (started) {
|
|
|
245 |
window.console.warn('Ignoring session keep-alive. The core/network module was already initialised.');
|
|
|
246 |
return;
|
|
|
247 |
}
|
|
|
248 |
started = true;
|
|
|
249 |
|
|
|
250 |
if (isMoodleIframe()) {
|
|
|
251 |
window.console.warn('Ignoring session keep-alive in this iframe inside another Moodle page.');
|
|
|
252 |
return;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
window.console.log('Starting Moodle session keep-alive.');
|
|
|
256 |
|
|
|
257 |
keepAliveFrequency = freq * 1000;
|
|
|
258 |
keepAliveMessage = await Str.get_string(identifier, component);
|
|
|
259 |
requestTimeout = timeout * 1000;
|
|
|
260 |
start();
|
|
|
261 |
};
|
|
|
262 |
|
|
|
263 |
return {
|
|
|
264 |
keepalive: keepalive,
|
|
|
265 |
init: init
|
|
|
266 |
};
|
|
|
267 |
});
|