-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-81924 core_sms: Add SMS gateway management UI
Originally implemented as MDL-81732. Co-authored by: Michael Hawkins <michaelh@moodle.com>
- Loading branch information
1 parent
3348832
commit b5ac325
Showing
29 changed files
with
1,510 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
admin/tool/mfa/factor/sms/tests/behat/factor_sms_setup_gateway.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@tool @tool_mfa @factor_sms | ||
Feature: Set up SMS factor when relevant gateway is not configured | ||
In order configure the SMS factor | ||
As an admin | ||
I want to be directed to the SMS gateway setup when necessary | ||
|
||
Scenario: Configuring gateways from the SMS factor settings | ||
Given I log in as "admin" | ||
And the following config values are set as admin: | ||
| enabled | 1 | tool_mfa | | ||
| lockout | 3 | tool_mfa | | ||
And the following config values are set as admin: | ||
| enabled | 1 | factor_sms | | ||
| weight | 100 | factor_sms | | ||
| duration | 1800 | factor_sms | | ||
When I navigate to "Plugins > Admin tools > Multi-factor authentication" in site administration | ||
And I follow "Edit settings for the SMS mobile phone factor" | ||
Then I should see "To use SMS as an authentication factor, you first need to set up an SMS gateway." | ||
And I should see "set up an SMS gateway" | ||
And I follow "set up an SMS gateway" | ||
And I should see "Configure SMS gateway" | ||
And I set the following fields to these values: | ||
| SMS gateway provider | AWS | | ||
| Gateway name | First AWS gateway | | ||
| Country code | 61 | | ||
| Access key | key123 | | ||
| Secret access key | secret456 | | ||
| Amazon API gateway region | ap-southeast-2 | | ||
And I press "Save changes" | ||
And I should see "SMS mobile phone" | ||
And the "SMS gateway" select box should contain "First AWS gateway (AWS)" | ||
And I follow "create a new gateway" | ||
And I should see "Configure SMS gateway" | ||
And I set the following fields to these values: | ||
| SMS gateway provider | AWS | | ||
| Gateway name | Second one | | ||
| Country code | 1 | | ||
| Access key | key1234 | | ||
| Secret access key | secret4567 | | ||
| Amazon API gateway region | ap-southeast-2 | | ||
And I press "Save changes" | ||
And I should see "SMS mobile phone" | ||
And the "SMS gateway" select box should contain "First AWS gateway (AWS)" | ||
And the "SMS gateway" select box should contain "Second one (AWS)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
// 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/>. | ||
|
||
/** | ||
* SMS gateway selection handler. | ||
* | ||
* @module core_sms/smsgatewaychooser | ||
* @copyright 2024 Safat Shahin <safat.shahin@moodle.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
const Selectors = { | ||
fields: { | ||
selector: '[data-smsgatewaychooser-field="selector"]', | ||
updateButton: '[data-smsgatewaychooser-field="updateButton"]', | ||
}, | ||
}; | ||
|
||
/** | ||
* Initialise the sms gateway chooser. | ||
*/ | ||
export const init = () => { | ||
document.querySelector(Selectors.fields.selector)?.addEventListener('change', e => { | ||
const form = e.target.closest('form'); | ||
const updateButton = form.querySelector(Selectors.fields.updateButton); | ||
const url = new URL(form.action); | ||
|
||
form.action = url.toString(); | ||
updateButton.click(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?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/>. | ||
|
||
namespace core_sms\external; | ||
|
||
use context_system; | ||
use core_external\external_api; | ||
use core_external\external_function_parameters; | ||
use core_external\external_single_structure; | ||
use core_external\external_value; | ||
|
||
/** | ||
* Webservice to enable or disable sms gateway. | ||
* | ||
* @package core_sms | ||
* @copyright 2024 Safat Shahin <safat.shahin@moodle.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class sms_gateway_status extends external_api { | ||
|
||
public static function execute_parameters(): external_function_parameters { | ||
return new external_function_parameters([ | ||
'plugin' => new external_value(PARAM_INT, 'Gateway ID', VALUE_REQUIRED), | ||
'state' => new external_value(PARAM_INT, 'Enabled or disabled', VALUE_REQUIRED), | ||
]); | ||
} | ||
|
||
public static function execute(int $plugin, int $state): array { | ||
// Parameter validation. | ||
[ | ||
'plugin' => $gatewayid, | ||
'state' => $state, | ||
] = self::validate_parameters(self::execute_parameters(), [ | ||
'plugin' => $plugin, | ||
'state' => $state, | ||
]); | ||
|
||
$context = context_system::instance(); | ||
self::validate_context($context); | ||
require_capability('moodle/site:config', $context); | ||
|
||
$result = [ | ||
'result' => true, | ||
'message' => '', | ||
'messagetype' => '', | ||
]; | ||
$manager = \core\di::get(\core_sms\manager::class); | ||
$gatewaymanagers = $manager->get_gateway_instances(['id' => $gatewayid]); | ||
$gatewaymanager = reset($gatewaymanagers); | ||
|
||
if (!$gatewaymanager) { | ||
$result = [ | ||
'result' => false, | ||
'message' => 'sms_gateway_not_found', | ||
'messagetype' => 'error' | ||
]; | ||
return $result; | ||
} | ||
|
||
if (!empty($state)) { | ||
$manager->enable_gateway(gateway: $gatewaymanager); | ||
$message = get_string('plugin_enabled', 'core_admin', $gatewaymanager->name); | ||
$messagetype = \core\notification::SUCCESS; | ||
} else { | ||
$gatewayresult = $manager->disable_gateway(gateway: $gatewaymanager); | ||
if ($gatewayresult->enabled) { | ||
$result = [ | ||
'result' => false, | ||
'message' => 'sms_gateway_disable_failed', | ||
'messagetype' => 'error' | ||
]; | ||
$message = get_string('sms_gateway_disable_failed', 'core_sms'); | ||
$messagetype = \core\notification::ERROR; | ||
} else { | ||
$message = get_string('plugin_disabled', 'core_admin', $gatewaymanager->name); | ||
$messagetype = \core\notification::SUCCESS; | ||
} | ||
} | ||
|
||
\core\notification::add($message, $messagetype); | ||
|
||
return $result; | ||
} | ||
|
||
public static function execute_returns(): external_single_structure { | ||
return new external_single_structure( | ||
[ | ||
'result' => new external_value(PARAM_BOOL, 'Whether the status was changed, true or false'), | ||
'message' => new external_value(PARAM_TEXT, 'Messages'), | ||
'messagetype' => new external_value(PARAM_TEXT, 'Message type'), | ||
] | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.