support standard not email login and username mapping

This commit is contained in:
Paweł Suwiński
2020-11-18 19:03:30 +01:00
parent 15450e8eec
commit 792a9e1c7e
2 changed files with 44 additions and 29 deletions

View File

@@ -63,9 +63,9 @@ class auth_plugin_emailotp extends auth_plugin_base {
} }
/** /**
* Matches only valid and allowed email as username. Validates credentials * Matches only valid email from allowed domains. Validates credentials and
* and password if exists in current session or generates ones for session * password if exists in current session or generates ones for session time
* time on empty password treated as one-time password request. * on empty password treated as one-time password request.
* *
* @param string $username The username * @param string $username The username
* @param string $password The password * @param string $password The password
@@ -73,7 +73,10 @@ class auth_plugin_emailotp extends auth_plugin_base {
*/ */
public function user_login($username, $password) { public function user_login($username, $password) {
global $CFG, $DB; global $CFG, $DB;
if (!validate_email($username) || email_is_not_allowed($username)) { $email = validate_email($username)
? $username // Email as username or signup on first login.
: $this->get_user_field($username, 'email'); // Standard login, existing user.
if (empty($email) || email_is_not_allowed($email)) {
return false; return false;
} }
// OTP already generated and base credentials matches. // OTP already generated and base credentials matches.
@@ -87,17 +90,12 @@ class auth_plugin_emailotp extends auth_plugin_base {
} }
// OTP request - do not proceed on preventaccountcreation when user not exits. // OTP request - do not proceed on preventaccountcreation when user not exits.
if (!isset($_SESSION[self::COMPONENT_NAME]) && empty($password) && ( if (!isset($_SESSION[self::COMPONENT_NAME]) && empty($password) && (
empty($CFG->authpreventaccountcreation) || $DB->get_field('user', 'id', [ empty($CFG->authpreventaccountcreation) || $this->get_user_field($username, 'id'))) {
'username' => $username, if (!$this->min_request_period_fulfilled($email)) {
'mnethostid' => $CFG->mnet_localhost_id,
'auth' => $this->authtype,
'deleted' => 0,
]))) {
if (!$this->min_request_period_fulfilled($username)) {
$this->redirect($username, 'otpperiod', notification::NOTIFY_WARNING); $this->redirect($username, 'otpperiod', notification::NOTIFY_WARNING);
} else if ($this->gen_otp($username)) { } else if ($this->gen_otp($username, $email)) {
\auth_emailotp\event\otp_generated::create(array( \auth_emailotp\event\otp_generated::create(array(
'other' => array('email' => $username), 'other' => array('email' => $email),
))->trigger(); ))->trigger();
$this->redirect($username, 'otpsent', notification::NOTIFY_SUCCESS); $this->redirect($username, 'otpsent', notification::NOTIFY_SUCCESS);
} else { } else {
@@ -114,7 +112,7 @@ class auth_plugin_emailotp extends auth_plugin_base {
notification::NOTIFY_WARNING notification::NOTIFY_WARNING
); );
\auth_emailotp\event\otp_revoked::create(array( \auth_emailotp\event\otp_revoked::create(array(
'other' => array('email' => $username), 'other' => array('email' => $email),
))->trigger(); ))->trigger();
} }
} }
@@ -157,6 +155,7 @@ class auth_plugin_emailotp extends auth_plugin_base {
*/ */
public function get_userinfo($username) { public function get_userinfo($username) {
$this->get_custom_user_profile_fields(); $this->get_custom_user_profile_fields();
// Signup - username is an email address.
$fields = array('email' => $username); $fields = array('email' => $username);
if ($this->config->fieldsmapping_pattern && if ($this->config->fieldsmapping_pattern &&
$this->config->fieldsmapping_mapping) { $this->config->fieldsmapping_mapping) {
@@ -168,7 +167,8 @@ class auth_plugin_emailotp extends auth_plugin_base {
return trim($mapping); return trim($mapping);
}, explode(PHP_EOL, $this->config->fieldsmapping_mapping))), }, explode(PHP_EOL, $this->config->fieldsmapping_mapping))),
function($key) { function($key) {
return in_array($key, $this->userfields) || return $key == 'username' ||
in_array($key, $this->userfields) ||
in_array($key, $this->customfields); in_array($key, $this->customfields);
}, },
ARRAY_FILTER_USE_KEY ARRAY_FILTER_USE_KEY
@@ -195,9 +195,10 @@ class auth_plugin_emailotp extends auth_plugin_base {
* gen_otp * gen_otp
* *
* @param string $username * @param string $username
* @param string $email
* @return bool * @return bool
*/ */
protected function gen_otp(string $username) { protected function gen_otp(string $username, string $email) {
global $CFG; global $CFG;
$newpassword = generate_password(); $newpassword = generate_password();
$_SESSION[self::COMPONENT_NAME] = array( $_SESSION[self::COMPONENT_NAME] = array(
@@ -205,21 +206,17 @@ class auth_plugin_emailotp extends auth_plugin_base {
'password' => password_hash($newpassword, PASSWORD_DEFAULT), 'password' => password_hash($newpassword, PASSWORD_DEFAULT),
'login_failed_count' => 0, 'login_failed_count' => 0,
); );
$a = (object)array( $user = (object)array(
'id' => -1, // Fake due email_to_user() requirements.
'auth' => $this->authtype,
'username' => $username, 'username' => $username,
'email' => $email,
'password' => $newpassword, 'password' => $newpassword,
); );
return email_to_user( return email_to_user($user, core_user::get_support_user(),
(object)array( format_string(get_site()->fullname).': '.
'id' => -1, get_string('otpgeneratedsubj', self::COMPONENT_NAME, $user),
'auth' => $this->authtype, get_string('otpgeneratedtext', self::COMPONENT_NAME, $user)
'username ' => $username,
'email' => $username,
),
core_user::get_support_user(),
sprintf('%s: %s', format_string(get_site()->fullname),
get_string('otpgeneratedsubj', self::COMPONENT_NAME, $a)),
get_string('otpgeneratedtext', self::COMPONENT_NAME, $a)
); );
} }
@@ -261,4 +258,22 @@ class auth_plugin_emailotp extends auth_plugin_base {
) )
) === 0; ) === 0;
} }
/**
* get_user_field
*
* @see moodle_database::get_field()
* @param string $username
* @param string $field
* @return mixed
*/
private function get_user_field(string $username, string $field) {
global $CFG, $DB;
return $DB->get_field('user', $field, array(
'username' => $username,
'mnethostid' => $CFG->mnet_localhost_id,
'auth' => $this->authtype,
'deleted' => 0,
));
}
} }

View File

@@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$plugin->version = 2020111800; // The current plugin version (Date: YYYYMMDDXX). $plugin->version = 2020111801; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2018120304; // Requires this Moodle version. $plugin->requires = 2018120304; // Requires this Moodle version.
$plugin->component = 'auth_emailotp'; // Full name of the plugin (used for diagnostics). $plugin->component = 'auth_emailotp'; // Full name of the plugin (used for diagnostics).
$plugin->maturity = MATURITY_ALPHA; $plugin->maturity = MATURITY_ALPHA;