diff --git a/auth/cas/auth.php b/auth/cas/auth.php index ff06a3a3732..6615b755931 100644 --- a/auth/cas/auth.php +++ b/auth/cas/auth.php @@ -162,19 +162,6 @@ class auth_plugin_cas extends auth_plugin_ldap { } } - /** - * Logout from the CAS - * - */ - function prelogout_hook() { - global $CFG; - - if (!empty($this->config->logoutcas)) { - $backurl = $CFG->wwwroot; - $this->connectCAS(); - phpCAS::logoutWithURL($backurl); - } - } /** * Connect to the CAS (clientcas connection or proxycas connection) @@ -510,4 +497,22 @@ class auth_plugin_cas extends auth_plugin_ldap { } } } + + /** + * Post logout hook. + * + * Note: this method replace the prelogout_hook method to avoid redirect to CAS logout + * before the event userlogout being triggered. + * + * @param stdClass $user clone of USER object object before the user session was terminated + */ + public function postlogout_hook($user) { + global $CFG; + // Only redirect to CAS logout if the user is logged as a CAS user. + if (!empty($this->config->logoutcas) && $user->auth == $this->authtype) { + $backurl = $CFG->wwwroot; + $this->connectCAS(); + phpCAS::logoutWithRedirectService($backurl); + } + } } diff --git a/lib/authlib.php b/lib/authlib.php index 2be318eaf69..6740fe22831 100644 --- a/lib/authlib.php +++ b/lib/authlib.php @@ -566,6 +566,16 @@ class auth_plugin_base { return $this->customfields; } + + /** + * Post logout hook. + * + * This method is used after moodle logout by auth classes to execute server logout. + * + * @param stdClass $user clone of USER object before the user session was terminated + */ + public function postlogout_hook($user) { + } } /** diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 018dd908404..eef42c9ba09 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3162,10 +3162,11 @@ function require_logout() { } // Execute hooks before action. + $authplugins = array(); $authsequence = get_enabled_auth_plugins(); foreach ($authsequence as $authname) { - $authplugin = get_auth_plugin($authname); - $authplugin->prelogout_hook(); + $authplugins[$authname] = get_auth_plugin($authname); + $authplugins[$authname]->prelogout_hook(); } // Store info that gets removed during logout. @@ -3181,11 +3182,19 @@ function require_logout() { $event->add_record_snapshot('sessions', $session); } + // Clone of $USER object to be used by auth plugins. + $user = fullclone($USER); + // Delete session record and drop $_SESSION content. \core\session\manager::terminate_current(); // Trigger event AFTER action. $event->trigger(); + + // Hook to execute auth plugins redirection after event trigger. + foreach ($authplugins as $authplugin) { + $authplugin->postlogout_hook($user); + } } /**