1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* factor_sms upgrade library.
|
|
|
19 |
*
|
|
|
20 |
* @package factor_sms
|
|
|
21 |
* @copyright 2024 Safat Shahin <safat.shahin@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Factor sms upgrade helper.
|
|
|
27 |
*
|
|
|
28 |
* @param int $oldversion Previous version of the plugin.
|
|
|
29 |
* @return bool
|
|
|
30 |
*/
|
|
|
31 |
function xmldb_factor_sms_upgrade(int $oldversion): bool {
|
|
|
32 |
if ($oldversion < 2024082200) {
|
|
|
33 |
$config = get_config('factor_sms');
|
|
|
34 |
// If the sms factor is enabled, then do the migration to the sms subsystem.
|
|
|
35 |
if ((int)$config->enabled === 1) {
|
|
|
36 |
// SMS gateway configs.
|
|
|
37 |
$smsconfig = new stdClass();
|
|
|
38 |
$smsconfig->countrycode = $config->countrycode;
|
|
|
39 |
$smsconfig->gateway = $config->gateway;
|
|
|
40 |
$smsconfig->usecredchain = $config->usecredchain;
|
|
|
41 |
$smsconfig->api_key = $config->api_key;
|
|
|
42 |
$smsconfig->api_secret = $config->api_secret;
|
|
|
43 |
$smsconfig->api_region = $config->api_region;
|
|
|
44 |
// Now insert the record.
|
|
|
45 |
$manager = \core\di::get(\core_sms\manager::class);
|
|
|
46 |
$gateway = $manager->create_gateway_instance(
|
|
|
47 |
classname: \smsgateway_aws\gateway::class,
|
|
|
48 |
name: 'MFA AWS',
|
|
|
49 |
enabled: $config->enabled,
|
|
|
50 |
config: $smsconfig,
|
|
|
51 |
);
|
|
|
52 |
// Set the mfa config for the sms gateway.
|
|
|
53 |
set_config('smsgateway', $gateway->id, 'factor_sms');
|
|
|
54 |
|
|
|
55 |
// Now add the task to send notification to admins about this migration.
|
|
|
56 |
$task = new \factor_sms\task\sms_gateway_migration_notification();
|
|
|
57 |
\core\task\manager::queue_adhoc_task($task, true);
|
|
|
58 |
}
|
|
|
59 |
// MFA savepoint reached.
|
|
|
60 |
upgrade_plugin_savepoint(true, 2024082200, 'factor', 'sms');
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if ($oldversion < 2024082201) {
|
|
|
64 |
// Unset the removed admin settings.
|
|
|
65 |
unset_config('countrycode', 'factor_sms');
|
|
|
66 |
unset_config('gateway', 'factor_sms');
|
|
|
67 |
unset_config('usecredchain', 'factor_sms');
|
|
|
68 |
unset_config('api_key', 'factor_sms');
|
|
|
69 |
unset_config('api_secret', 'factor_sms');
|
|
|
70 |
unset_config('api_region', 'factor_sms');
|
|
|
71 |
|
|
|
72 |
// MFA savepoint reached.
|
|
|
73 |
upgrade_plugin_savepoint(true, 2024082201, 'factor', 'sms');
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// Automatically generated Moodle v4.5.0 release upgrade line.
|
|
|
77 |
// Put any upgrade step following this.
|
|
|
78 |
|
|
|
79 |
if ($oldversion < 2025040700) {
|
|
|
80 |
// Ensure default values are applied for the MFA SMS factor when upgrading.
|
|
|
81 |
$config = get_config('factor_sms');
|
|
|
82 |
|
|
|
83 |
// Set the weight to the default value (100) if it is misconfigured (e.g. set to 0).
|
|
|
84 |
$weight = $config->weight ?? null;
|
|
|
85 |
if (isset($weight) && (int)$weight <= 0) {
|
|
|
86 |
set_config('weight', 100, 'factor_sms');
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
// Set the duration to the default value (30 minutes) if it is misconfigured (e.g. set to 0).
|
|
|
90 |
$duration = $config->duration ?? null;
|
|
|
91 |
if (isset($duration) && (int)$duration <= 0) {
|
|
|
92 |
set_config('duration', 30 * MINSECS, 'factor_sms');
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
// MFA savepoint reached.
|
|
|
96 |
upgrade_plugin_savepoint(true, 2025040700, 'factor', 'sms');
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// Automatically generated Moodle v5.0.0 release upgrade line.
|
|
|
100 |
// Put any upgrade step following this.
|
|
|
101 |
|
|
|
102 |
return true;
|
|
|
103 |
}
|