mirror of
https://github.com/moodle/moodle.git
synced 2025-04-25 10:26:17 +02:00
MDL-54551 core: AJAX call redirects to login page when session expired
This commit is contained in:
parent
8c51626841
commit
acf94de6d5
@ -186,6 +186,7 @@ $string['service'] = 'Service';
|
||||
$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';
|
||||
|
2
lib/amd/build/ajax.min.js
vendored
2
lib/amd/build/ajax.min.js
vendored
@ -1 +1 @@
|
||||
define(["jquery","core/config","core/log"],function(a,b,c){var d=!1,e=function(a){var b,c,d=this,e=null,f=0;if(a.error)for(;f<d.length;f++)b=d[f],b.deferred.reject(a);else{for(f=0;f<d.length;f++){if(b=d[f],c=a[f],"undefined"==typeof c){e=new Error("missing response");break}if(c.error!==!1){e=c.exception;break}b.deferred.resolve(c.data)}if(null!==e)for(;f<d.length;f++)b=d[f],b.deferred.reject(e)}},f=function(a,b,e){var f=this,g=0;for(g=0;g<f.length;g++){var h=f[g];d?(c.error("Page unloaded."),c.error(e)):h.deferred.reject(e)}};return{call:function(c,g,h){a(window).bind("beforeunload",function(){d=!0});var i,j=[],k=[],l=[],m="";for("undefined"==typeof h&&(h=!0),"undefined"==typeof g&&(g=!0),i=0;i<c.length;i++){var n=c[i];j.push({index:i,methodname:n.methodname,args:n.args}),n.deferred=a.Deferred(),k.push(n.deferred.promise()),"undefined"!=typeof n.done&&n.deferred.done(n.done),"undefined"!=typeof n.fail&&n.deferred.fail(n.fail),n.index=i,l.push(n.methodname)}m=l.length<=5?l.sort().join():l.length+"-method-calls",j=JSON.stringify(j);var o={type:"POST",data:j,context:c,dataType:"json",processData:!1,async:g,contentType:"application/json"},p="service.php";h||(p="service-nologin.php");var q=b.wwwroot+"/lib/ajax/"+p+"?sesskey="+b.sesskey+"&info="+m;return g?a.ajax(q,o).done(e).fail(f):(o.success=e,o.error=f,a.ajax(q,o)),k}}});
|
||||
define(["jquery","core/config","core/log","core/yui","core/url"],function(a,b,c,d,e){var f=!1,g=function(a){var b,c,f=this,g=null,h=0;if(a.error)for(;h<f.length;h++)b=f[h],b.deferred.reject(a);else{for(h=0;h<f.length;h++){if(b=f[h],c=a[h],"undefined"==typeof c){g=new Error("missing response");break}if(c.error!==!1){g=c.exception;break}b.deferred.resolve(c.data)}if(null!==g)if("servicerequireslogin"===g.errorcode)d.use("moodle-core-formchangechecker",function(){if(!M.core_formchangechecker.get_form_dirty_state()){var a=e.relativeUrl("/login/index.php");window.location.replace(a)}});else for(;h<f.length;h++)b=f[h],b.deferred.reject(g)}},h=function(a,b,d){var e=this,g=0;for(g=0;g<e.length;g++){var h=e[g];f?(c.error("Page unloaded."),c.error(d)):h.deferred.reject(d)}};return{call:function(c,d,e){a(window).bind("beforeunload",function(){f=!0});var i,j=[],k=[],l=[],m="";for("undefined"==typeof e&&(e=!0),"undefined"==typeof d&&(d=!0),i=0;i<c.length;i++){var n=c[i];j.push({index:i,methodname:n.methodname,args:n.args}),n.deferred=a.Deferred(),k.push(n.deferred.promise()),"undefined"!=typeof n.done&&n.deferred.done(n.done),"undefined"!=typeof n.fail&&n.deferred.fail(n.fail),n.index=i,l.push(n.methodname)}m=l.length<=5?l.sort().join():l.length+"-method-calls",j=JSON.stringify(j);var o={type:"POST",data:j,context:c,dataType:"json",processData:!1,async:d,contentType:"application/json"},p="service.php";e||(p="service-nologin.php");var q=b.wwwroot+"/lib/ajax/"+p+"?sesskey="+b.sesskey+"&info="+m;return d?a.ajax(q,o).done(g).fail(h):(o.success=g,o.error=h,a.ajax(q,o)),k}}});
|
@ -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;
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -208,10 +208,10 @@ class external_api {
|
||||
// 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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user