From b1e6e37cf704d8af18657f44650a7cc84358173f Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Tue, 13 Feb 2018 16:14:13 +1100 Subject: [PATCH] Add a pre-login hook to check if user is already logged in, giving each auth plugin the chance to authenticate or redirect the user as needed. --- htdocs/auth/lib.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/htdocs/auth/lib.php b/htdocs/auth/lib.php index 99e2f3b..b54a8d5 100644 --- a/htdocs/auth/lib.php +++ b/htdocs/auth/lib.php @@ -523,6 +523,11 @@ function auth_setup () { return; } + // Allow auth plugins to try logging in the user. + if (auth_plugins_call_pre_loginpage_hook()) { + return; + } + // Check if the page is public or the site is configured to be public. if (defined('PUBLIC') && !param_exists('login')) { return; @@ -539,6 +544,39 @@ function auth_setup () { } /** + * Allow auth plugins a chance to authenticate or redirect as needed. + * + * It will call the 'pre_loginpage_hook' for each auth plugin available, + * if any of them completes the login it returns immediately. + * + * Similar to: https://tracker.moodle.org/browse/MDL-48887 + * + * @return bool If any plugin authenticated the user. + */ +function auth_plugins_call_pre_loginpage_hook() { + global $USER; + + $methodname = 'pre_loginpage_hook'; + $instances = auth_get_auth_instances(); + + foreach ($instances as $instance) { + $auth = AuthFactory::create($instance->id); + if ($auth === false) { + continue; + } + if (!method_exists($auth, $methodname)) { + continue; + } + $auth->$methodname(); + if ($USER->is_logged_in()) { + return true; + } + } + + return false; +} + +/** * * Returns all auth instances * -- 2.7.4