From cffd0fa138345e8143919e261ba693dac646f576 Mon Sep 17 00:00:00 2001 From: Jakob Date: Fri, 4 Mar 2016 11:02:34 -0800 Subject: [PATCH] MDL-53306 auth: Add hook for auth plugins to access user object. Add a hook for auth plugins to be able to modify or check a user, before raising any authentication errors. The auth plugin needs to add a public function like this: /** * Pre user_login hook. * This method is called from authenticate_user_login() right after the user * object is generated. This gives the auth plugins an option to make adjustments * before the verification process starts. * * @param object $user user object, later used for $USER */ public function pre_user_login_hook(&$user) { // Override if needed. } --- auth/upgrade.txt | 5 +++++ lib/authlib.php | 12 ++++++++++++ lib/moodlelib.php | 6 ++++++ 3 files changed, 23 insertions(+) diff --git a/auth/upgrade.txt b/auth/upgrade.txt index 0024b81aad9..d6fa887fc4d 100644 --- a/auth/upgrade.txt +++ b/auth/upgrade.txt @@ -1,6 +1,11 @@ This files describes API changes in /auth/* - plugins, information provided here is intended especially for developers. +=== 3.2 === + +* New auth hook - pre_user_login_hook() - available, triggered right after the user object is created. + This can be used to modify the user object before any authentication errors are raised. + === 3.0 === * login_signup_form::signup_captcha_enabled() now calls is_captcha_enabled() from the current auth plugin instead of from auth_email diff --git a/lib/authlib.php b/lib/authlib.php index 9c17d2784ad..15dc7d471d5 100644 --- a/lib/authlib.php +++ b/lib/authlib.php @@ -464,6 +464,18 @@ class auth_plugin_base { // complete_user_login($user); } + /** + * Pre user_login hook. + * This method is called from authenticate_user_login() right after the user + * object is generated. This gives the auth plugins an option to make adjustments + * before the verification process starts. + * + * @param object $user user object, later used for $USER + */ + public function pre_user_login_hook(&$user) { + // Override if needed. + } + /** * Post authentication hook. * This method is called from authenticate_user_login() for all enabled auth plugins. diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 8160f89c542..6e35cced7ca 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -4123,6 +4123,12 @@ function authenticate_user_login($username, $password, $ignorelockout=false, &$f if ($user) { // Use manual if auth not set. $auth = empty($user->auth) ? 'manual' : $user->auth; + + if (in_array($user->auth, $authsenabled)) { + $authplugin = get_auth_plugin($user->auth); + $authplugin->pre_user_login_hook($user); + } + if (!empty($user->suspended)) { $failurereason = AUTH_LOGIN_SUSPENDED;