From 3348832b78a456ab12d5b83a2954af27b7487fe0 Mon Sep 17 00:00:00 2001 From: Safat Date: Sat, 29 Jun 2024 16:33:21 +1000 Subject: [PATCH] MDL-81924 smsgateway_aws: Add hooks for SMS gateway management Originally implemented as MDL-81732. Co-authored by: Michael Hawkins --- sms/gateway/aws/classes/hook_listener.php | 110 +++++++++++++++++++++ sms/gateway/aws/db/hooks.php | 32 ++++++ sms/gateway/aws/lang/en/smsgateway_aws.php | 22 ++--- sms/gateway/aws/tests/gateway_test.php | 21 +++- sms/gateway/aws/version.php | 2 +- 5 files changed, 167 insertions(+), 20 deletions(-) create mode 100644 sms/gateway/aws/classes/hook_listener.php create mode 100644 sms/gateway/aws/db/hooks.php diff --git a/sms/gateway/aws/classes/hook_listener.php b/sms/gateway/aws/classes/hook_listener.php new file mode 100644 index 0000000000000..8da4ff9db2c3d --- /dev/null +++ b/sms/gateway/aws/classes/hook_listener.php @@ -0,0 +1,110 @@ +. + +namespace smsgateway_aws; + +use core_sms\hook\after_sms_gateway_form_hook; + +/** + * Hook listener for aws sms gateway. + * + * @package smsgateway_aws + * @copyright 2024 Safat Shahin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class hook_listener { + + /** + * Hook listener for the sms gateway setup form. + * + * @param after_sms_gateway_form_hook $hook The hook to add to sms gateway setup. + */ + public static function set_form_definition_for_aws_sms_gateway(after_sms_gateway_form_hook $hook): void { + if ($hook->plugin !== 'smsgateway_aws') { + return; + } + + $mform = $hook->mform; + + $mform->addElement('static', 'information', get_string('aws_information', 'smsgateway_aws')); + + $gateways = [ + 'aws_sns' => get_string('aws_sns', 'smsgateway_aws'), + ]; + $mform->addElement( + 'select', + 'gateway', + get_string('gateway', 'smsgateway_aws'), + $gateways, + ); + $mform->setDefault( + elementName: 'gateway', + defaultValue: 'aws_sns', + ); + // Remove this if more aws gateway implemented, eg sqs. + $mform->hardFreeze('gateway'); + + $mform->addElement( + 'checkbox', + 'usecredchain', + get_string('usecredchain', 'smsgateway_aws'), + ' ', + ); + $mform->setDefault( + elementName: 'usecredchain', + defaultValue: 0, + ); + + $mform->addElement( + 'text', + 'api_key', + get_string('api_key', 'smsgateway_aws'), + 'maxlength="255" size="20"', + ); + $mform->setType('api_key', PARAM_TEXT); + $mform->addRule('api_key', get_string('maximumchars', '', 255), 'maxlength', 255); + $mform->setDefault( + elementName: 'api_key', + defaultValue: '', + ); + $mform->addElement( + 'passwordunmask', + 'api_secret', + get_string('api_secret', 'smsgateway_aws'), + 'maxlength="255" size="20"', + ); + $mform->setType('api_secret', PARAM_TEXT); + $mform->addRule('api_secret', get_string('maximumchars', '', 255), 'maxlength', 255); + $mform->setDefault( + elementName: 'api_secret', + defaultValue: '', + ); + + $mform->addElement( + 'text', + 'api_region', + get_string('api_region', 'smsgateway_aws'), + 'maxlength="255" size="20"', + ); + $mform->setType('api_region', PARAM_TEXT); + $mform->addRule('api_region', get_string('maximumchars', '', 255), 'maxlength', 255); + $mform->setDefault( + elementName: 'api_region', + defaultValue: 'ap-southeast-2', + ); + } + +} diff --git a/sms/gateway/aws/db/hooks.php b/sms/gateway/aws/db/hooks.php new file mode 100644 index 0000000000000..efcc88187ae2f --- /dev/null +++ b/sms/gateway/aws/db/hooks.php @@ -0,0 +1,32 @@ +. + +/** + * Hook listener callbacks for aws sms gateway. + * + * @package smsgateway_aws + * @copyright 2024 Safat Shahin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$callbacks = [ + [ + 'hook' => \core_sms\hook\after_sms_gateway_form_hook::class, + 'callback' => \smsgateway_aws\hook_listener::class . '::set_form_definition_for_aws_sms_gateway', + ], +]; diff --git a/sms/gateway/aws/lang/en/smsgateway_aws.php b/sms/gateway/aws/lang/en/smsgateway_aws.php index f0dcce3eaab0a..e19e079ef9ca4 100644 --- a/sms/gateway/aws/lang/en/smsgateway_aws.php +++ b/sms/gateway/aws/lang/en/smsgateway_aws.php @@ -22,21 +22,13 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$string['api_key'] = 'Key'; -$string['api_key_help'] = 'Amazon API key credential.'; -$string['api_region'] = 'Region'; -$string['api_region_help'] = 'Amazon API gateway region.'; -$string['api_secret'] = 'Secret'; -$string['api_secret_help'] = 'Amazon API secret credential.'; +$string['api_key'] = 'Access key'; +$string['api_region'] = 'Amazon API gateway region'; +$string['api_secret'] = 'Secret access key'; +$string['aws_information'] = 'Complete the following fields using the information provided by AWS'; $string['aws_sns'] = 'AWS SNS'; -$string['countrycode'] = 'Country number code'; -$string['countrycode_help'] = 'The calling code without the leading + as a default if users do not enter an international number with a + prefix. - -See this link for a list of calling codes: {$a}'; -$string['gateway'] = 'SMS Gateway'; -$string['gateway_help'] = 'The SMS provider you wish to send messages via'; +$string['gateway'] = 'AWS service'; +$string['aws_information'] = 'Complete the following fields using the information provided by AWS'; $string['pluginname'] = 'AWS'; $string['privacy:metadata'] = 'The AWS SMS gateway plugin does not store any personal data.'; -$string['usecredchain'] = 'Find AWS credentials using the default credential provider chain'; - - +$string['usecredchain'] = 'Find AWS credentials using the default provider chain'; diff --git a/sms/gateway/aws/tests/gateway_test.php b/sms/gateway/aws/tests/gateway_test.php index c993619353999..70281a75a5551 100644 --- a/sms/gateway/aws/tests/gateway_test.php +++ b/sms/gateway/aws/tests/gateway_test.php @@ -32,9 +32,22 @@ class gateway_test extends \advanced_testcase { public function test_update_message_status(): void { $this->resetAfterTest(); + $config = new \stdClass(); + $config->api_key = 'test_api_key'; + $manager = \core\di::get(\core_sms\manager::class); - $gw = $manager->create_gateway_instance(gateway::class, true); - $othergw = $manager->create_gateway_instance(gateway::class, true); + $gw = $manager->create_gateway_instance( + classname: gateway::class, + name: 'aws', + enabled: true, + config: $config, + ); + $othergw = $manager->create_gateway_instance( + classname: gateway::class, + name: 'aws', + enabled: true, + config: $config, + ); $message = new message( recipientnumber: '1234567890', @@ -42,7 +55,7 @@ public function test_update_message_status(): void { component: 'smsgateway_aws', messagetype: 'test', recipientuserid: null, - sensitive: false, + issensitive: false, gatewayid: $gw->id, ); $message2 = new message( @@ -51,7 +64,7 @@ public function test_update_message_status(): void { component: 'smsgateway_aws', messagetype: 'test', recipientuserid: null, - sensitive: false, + issensitive: false, gatewayid: $gw->id, ); diff --git a/sms/gateway/aws/version.php b/sms/gateway/aws/version.php index 1f2f7ead27304..1e5f473b2c9db 100644 --- a/sms/gateway/aws/version.php +++ b/sms/gateway/aws/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'smsgateway_aws'; -$plugin->version = 2024042200; +$plugin->version = 2024082200; $plugin->requires = 2024041600; $plugin->maturity = MATURITY_STABLE;