From 813320fbb69ff301fefd4e137e29f5233ccb7094 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 23 Nov 2016 19:32:48 +0000 Subject: [PATCH] MDL-57102 auth: Add new signup_is_enabled function And apply the function in: - login/signup.php - blocks/login/block_login.php --- auth/upgrade.txt | 1 + blocks/login/block_login.php | 9 ++++----- lib/authlib.php | 18 ++++++++++++++++++ login/signup.php | 8 ++------ 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/auth/upgrade.txt b/auth/upgrade.txt index 8a1dc21d94b..6bd5ef3d84a 100644 --- a/auth/upgrade.txt +++ b/auth/upgrade.txt @@ -9,6 +9,7 @@ information provided here is intended especially for developers. * The authentication plugin auth_radius has been moved to https://github.com/moodlehq/moodle-auth_radius * New auth_email::user_signup_with_confirmation() method has a new optional parameter $confirmationurl to provide a different confirmation URL. +* New signup_is_enabled() function available in lib/authlib.php to safely check if sign-up is enabled in the site. === 3.0 === diff --git a/blocks/login/block_login.php b/blocks/login/block_login.php index 26ca859b722..be906deb45f 100644 --- a/blocks/login/block_login.php +++ b/blocks/login/block_login.php @@ -33,6 +33,8 @@ class block_login extends block_base { function get_content () { global $USER, $CFG, $SESSION, $OUTPUT; + require_once($CFG->libdir . '/authlib.php'); + $wwwroot = ''; $signup = ''; @@ -48,11 +50,8 @@ class block_login extends block_base { $wwwroot = str_replace("http://", "https://", $CFG->wwwroot); } - if (!empty($CFG->registerauth)) { - $authplugin = get_auth_plugin($CFG->registerauth); - if ($authplugin->can_signup()) { - $signup = $wwwroot . '/login/signup.php'; - } + if (signup_is_enabled()) { + $signup = $wwwroot . '/login/signup.php'; } // TODO: now that we have multiauth it is hard to find out if there is a way to change password $forgot = $wwwroot . '/login/forgot_password.php'; diff --git a/lib/authlib.php b/lib/authlib.php index 2d7405eb60d..09aa59d0584 100644 --- a/lib/authlib.php +++ b/lib/authlib.php @@ -916,3 +916,21 @@ function signup_get_user_confirmation_authplugin() { } return $authplugin; } + +/** + * Check if sign-up is enabled in the site. If is enabled, the function will return the authplugin instance. + * + * @return mixed false if sign-up is not enabled, the authplugin instance otherwise. + * @since Moodle 3.2 + */ +function signup_is_enabled() { + global $CFG; + + if (!empty($CFG->registerauth)) { + $authplugin = get_auth_plugin($CFG->registerauth); + if ($authplugin->can_signup()) { + return $authplugin; + } + } + return false; +} diff --git a/login/signup.php b/login/signup.php index abe977e0e0b..ad7d9454246 100644 --- a/login/signup.php +++ b/login/signup.php @@ -26,6 +26,7 @@ require('../config.php'); require_once($CFG->dirroot . '/user/editlib.php'); +require_once($CFG->libdir . '/authlib.php'); // Try to prevent searching for sites that allow sign-up. if (!isset($CFG->additionalhtmlhead)) { @@ -33,12 +34,7 @@ if (!isset($CFG->additionalhtmlhead)) { } $CFG->additionalhtmlhead .= ''; -if (empty($CFG->registerauth)) { - print_error('notlocalisederrormessage', 'error', '', 'Sorry, you may not use this page.'); -} -$authplugin = get_auth_plugin($CFG->registerauth); - -if (!$authplugin->can_signup()) { +if (!$authplugin = signup_is_enabled()) { print_error('notlocalisederrormessage', 'error', '', 'Sorry, you may not use this page.'); }