Skip to content

Commit

Permalink
MDL-54551 core: AJAX call redirects to login page when session expired
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom Rawson authored and mdjnelson committed Jul 18, 2018
1 parent 8c51626 commit acf94de
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions lang/en/webservice.php
Expand Up @@ -186,6 +186,7 @@
$string['servicehelpexplanation'] = 'A service is a set of functions. A service can be accessed by all users or just specified users.';
$string['servicename'] = 'Service name';
$string['servicenotavailable'] = 'Web service is not available (it doesn\'t exist or might be disabled)';
$string['servicerequireslogin'] = 'Web service requires login (the session has been logged out or has expired. Please save any work on the current page before continuing)';
$string['servicesbuiltin'] = 'Built-in services';
$string['servicescustom'] = 'Custom services';
$string['serviceusers'] = 'Authorised users';
Expand Down
2 changes: 1 addition & 1 deletion lib/amd/build/ajax.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions lib/amd/src/ajax.js
Expand Up @@ -25,7 +25,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
define(['jquery', 'core/config', 'core/log'], function($, config, Log) {
define(['jquery', 'core/config', 'core/log', 'core/yui', 'core/url'], function($, config, Log, Y, URL) {

// Keeps track of when the user leaves the page so we know not to show an error.
var unloading = false;
Expand Down Expand Up @@ -79,9 +79,20 @@ define(['jquery', 'core/config', 'core/log'], function($, config, Log) {
}
// Something failed, reject the remaining promises.
if (exception !== null) {
for (; i < requests.length; i++) {
request = requests[i];
request.deferred.reject(exception);
// If the user isn't doing anything too important, redirect to the login page.
if (exception.errorcode === "servicerequireslogin") {
Y.use('moodle-core-formchangechecker', function() {
if (!M.core_formchangechecker.get_form_dirty_state()) {
// If we reach here, the user isn't editing anything on the page.
var loginUrl = URL.relativeUrl("/login/index.php");
window.location.replace(loginUrl);
}
});
} else {
for (; i < requests.length; i++) {
request = requests[i];
request.deferred.reject(exception);
}
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/externallib.php
Expand Up @@ -208,10 +208,10 @@ public static function call_external_function($function, $args, $ajaxonly=false)
// Do not allow access to write or delete webservices as a public user.
if ($externalfunctioninfo->loginrequired) {
if (defined('NO_MOODLE_COOKIES') && NO_MOODLE_COOKIES && !PHPUNIT_TEST) {
throw new moodle_exception('servicenotavailable', 'webservice');
throw new moodle_exception('servicerequireslogin', 'webservice');
}
if (!isloggedin()) {
throw new moodle_exception('servicenotavailable', 'webservice');
throw new moodle_exception('servicerequireslogin', 'webservice');
} else {
require_sesskey();
}
Expand Down

0 comments on commit acf94de

Please sign in to comment.