MDL-79958 factor_email: Email factor is enabled by default

This commit is contained in:
David Woloszyn
2024-12-24 15:17:49 +07:00
committed by Huong Nguyen
parent 7d6f282aef
commit 8d7b8f99bd
11 changed files with 120 additions and 13 deletions
@@ -0,0 +1,31 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Installation code for factor_email.
*
* @package factor_email
* @copyright 2024 David Woloszyn <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Perform install procedures for factor_email.
*/
function xmldb_factor_email_install(): void {
// The factor_email should be enabled by default. Just ensure it is ordered too.
set_config('factor_order', 'email', 'tool_mfa');
}
@@ -0,0 +1,57 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* factor_email upgrade library.
*
* @package factor_email
* @copyright 2024 David Woloszyn <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* MFA upgrade helper function.
*
* @param int $oldversion
*/
function xmldb_factor_email_upgrade($oldversion): bool {
if ($oldversion < 2024122400) {
// Check for sites that don't have MFA enabled.
if (!get_config('tool_mfa', 'enabled')) {
// Enable email factor.
set_config('enabled', 1, 'factor_email');
// Check factor order config to ensure email is situated in there.
$factororderconfig = get_config('tool_mfa', 'factor_order');
if (!$factororderconfig) {
set_config('factor_order', 'email', 'tool_mfa');
} else {
$order = explode(',', $factororderconfig);
// Remove any empty entries (this happens with entries like ',sms,email').
$order = array_filter($order);
if (!in_array('email', $order)) {
array_unshift($order, 'email');
$orderstring = implode(',', $order);
set_config('factor_order', $orderstring, 'tool_mfa');
}
}
}
upgrade_plugin_savepoint(true, 2024122400, 'factor', 'email');
}
return true;
}
+1 -1
View File
@@ -27,7 +27,7 @@ defined('MOODLE_INTERNAL') || die();
$enabled = new admin_setting_configcheckbox('factor_email/enabled',
new lang_string('settings:enablefactor', 'tool_mfa'),
new lang_string('settings:enablefactor_help', 'tool_mfa'), 0);
new lang_string('settings:enablefactor_help', 'tool_mfa'), 1);
$enabled->set_updatedcallback(function () {
\tool_mfa\manager::do_factor_action('email', get_config('factor_email', 'enabled') ? 'enable' : 'disable');
});
+1 -1
View File
@@ -26,7 +26,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024121800; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2024122400; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024100100; // Requires this Moodle version.
$plugin->component = 'factor_email'; // Full name of the plugin (used for diagnostics).
$plugin->maturity = MATURITY_STABLE;
@@ -37,6 +37,9 @@ final class factor_test extends \advanced_testcase {
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Disable the email factor (enabled by default).
set_config('enabled', 0, 'factor_email');
$grace = \tool_mfa\plugininfo\factor::get_factor('grace');
$affecting = $grace->get_affecting_factors();
$this->assertEquals(0, count($affecting));
@@ -16,6 +16,8 @@ Feature: Login user with sms authentication factor
| enabled | 1 | factor_sms |
| weight | 100 | factor_sms |
| duration | 1800 | factor_sms |
And the following config values are set as admin:
| enabled | 0 | factor_email |
And I navigate to "Plugins > Admin tools > Multi-factor authentication" in site administration
And I follow "Edit settings for the SMS mobile phone factor"
And I set the field "SMS gateway" to "Dummy gateway (AWS)"
@@ -31,6 +31,16 @@ final class admin_setting_managemfa_test extends \advanced_testcase {
use \tool_mfa\tests\mfa_settings_trait;
/**
* Setup testcase.
*/
public function setUp(): void {
parent::setUp();
$this->resetAfterTest();
// Disable email factor (enabled by default).
$this->set_factor_state('email', 0);
}
/**
* Tests getting the factor combinations
*/
@@ -137,7 +147,6 @@ final class admin_setting_managemfa_test extends \advanced_testcase {
* @param int $combinationscount expected count of available combinations
*/
public function test_get_factor_combinations_with_data_provider(array $factorset, int $combinationscount): void {
$this->resetAfterTest();
$enabledcount = 0;
foreach ($factorset as $factor) {
@@ -168,7 +177,6 @@ final class admin_setting_managemfa_test extends \advanced_testcase {
* Tests checking the factor combinations
*/
public function test_factor_combination_checker(): void {
$this->resetAfterTest();
$managemfa = new \tool_mfa\table\admin_setting_managemfa();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
@@ -24,3 +24,7 @@ Feature: Manage factor plugins
And "Grace period" "table_row" should appear before "Trust this device" "table_row"
And I click on "Move down" "link" in the "Grace period" "table_row"
And "Grace period" "table_row" should appear after "Trust this device" "table_row"
Scenario: Email factor is enabled by default
Given I navigate to "Plugins > Admin tools > Multi-factor authentication" in site administration
And I should see "Disable Email" in the "Email" "table_row"
@@ -39,6 +39,8 @@ Feature: Set up and manage user factors
| enabled | 1 | factor_webauthn |
And the following config values are set as admin:
| enabled | 1 | factor_sms |
And the following config values are set as admin:
| enabled | 0 | factor_email |
And the following "tool_mfa > User factors" exist:
| username | factor | label |
| admin | sms | +409111222 |
+5
View File
@@ -86,6 +86,9 @@ final class manager_test extends \advanced_testcase {
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Disable the email factor (enabled by default).
set_config('enabled', 0, 'factor_email');
// Check for fail status with no factors.
$this->assertEquals(\tool_mfa\plugininfo\factor::STATE_FAIL, \tool_mfa\manager::get_status());
@@ -369,6 +372,8 @@ final class manager_test extends \advanced_testcase {
$this->setUser($user);
set_config('enabled', 1, 'factor_nosetup');
set_config('enabled', 1, 'tool_mfa');
// Disable the email factor (enabled by default).
set_config('enabled', 0, 'factor_email');
// Capability Check.
$this->assertTrue(\tool_mfa\manager::is_ready());
@@ -44,6 +44,9 @@ final class plugininfo_factor_test extends \advanced_testcase {
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Disable the email factor (enabled by default).
set_config('enabled', 0, 'factor_email');
// Test that with no enabled factors, fallback is returned.
$this->assertEquals('fallback', \tool_mfa\plugininfo\factor::get_next_user_login_factor()->name);
@@ -90,22 +93,14 @@ final class plugininfo_factor_test extends \advanced_testcase {
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Create two active user factors.
// Add another factor (email factor is enabled by default).
set_config('enabled', 1, 'factor_totp');
set_config('enabled', 1, 'factor_webauthn');
$data = new \stdClass();
$data->userid = $user->id;
$data->factor = 'totp';
$data->label = 'testtotp';
$data->revoked = 0;
$DB->insert_record('tool_mfa', $data);
$data = new \stdClass();
$data->userid = $user->id;
$data->factor = 'webauthn';
$data->label = 'testwebauthn';
$data->revoked = 0;
$factorid = $DB->insert_record('tool_mfa', $data);
// Test there is more than one active factor.