Skip to content

Commit

Permalink
MDL-80961 factor_sms: Implement sms subsystem
Browse files Browse the repository at this point in the history
  • Loading branch information
safatshahin authored and mickhawkins committed Aug 14, 2024
1 parent 1870fc2 commit 43ce223
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
21 changes: 10 additions & 11 deletions admin/tool/mfa/factor/sms/classes/factor.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,6 @@ public function get_all_user_factors(stdClass $user): array {
* @return bool
*/
public function is_enabled(): bool {
if (empty(get_config('factor_sms', 'gateway'))) {
return false;
}

$class = '\factor_sms\local\smsgateway\\' . get_config('factor_sms', 'gateway');
if (!call_user_func($class . '::is_gateway_enabled')) {
return false;
}
return parent::is_enabled();
}

Expand Down Expand Up @@ -412,9 +404,16 @@ private function sms_verification_code(int $secret, ?string $phonenumber): void
];
$message = get_string('smsstring', 'factor_sms', $content);

$class = '\factor_sms\local\smsgateway\\' . get_config('factor_sms', 'gateway');
$gateway = new $class();
$gateway->send_sms_message($message, $phonenumber);
$manager = \core\di::get(\core_sms\manager::class);
$manager->send(
recipientnumber: $phonenumber,
content: $message,
component: 'factor_sms',
messagetype: 'factor',
recipientuserid: null,
sensitive: true,
async: false,
);
}

/**
Expand Down
57 changes: 57 additions & 0 deletions admin/tool/mfa/factor/sms/db/upgrade.php
Original file line number Diff line number Diff line change
@@ -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_sms upgrade library.
*
* @package factor_sms
* @copyright 2024 Safat Shahin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Factor sms upgrade helper.
*
* @param int $oldversion Previous version of the plugin.
* @return bool
*/
function xmldb_factor_sms_upgrade(int $oldversion): bool {
if ($oldversion < 2024050300) {
$config = get_config('factor_sms');
// If the sms factor is enabled, then do the migration to the sms subsystem.
if ((int)$config->enabled === 1) {
// SMS gateway configs.
$smsconfig = new stdClass();
$smsconfig->countrycode = $config->countrycode;
$smsconfig->gateway = $config->gateway;
$smsconfig->usecredchain = $config->usecredchain;
$smsconfig->api_key = $config->api_key;
$smsconfig->api_secret = $config->api_secret;
$smsconfig->api_region = $config->api_region;
// Now insert the record.
$manager = \core\di::get(\core_sms\manager::class);
$manager->create_gateway_instance(
classname: \smsgateway_aws\gateway::class,
enabled: $config->enabled,
config: $smsconfig,
);
}
// MFA savepoint reached.
upgrade_plugin_savepoint(true, 2024050300, 'factor', 'sms');
}

return true;
}
2 changes: 1 addition & 1 deletion admin/tool/mfa/factor/sms/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2024050300; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'factor_sms'; // Full name of the plugin (used for diagnostics).
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 43ce223

Please sign in to comment.