From 2ad6fecca5b7cf5627ac8482c3114779a0e75673 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 17 Apr 2023 11:03:15 +0200 Subject: [PATCH] MDL-78072 airnotifier: Allow to set publickey when creating new device Backport of MDL-77893. Co-authored by: Andrew Lyons --- user/externallib.php | 12 ++++++++---- .../external/update_user_device_public_key_test.php | 7 ++++++- user/tests/externallib_test.php | 9 ++++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/user/externallib.php b/user/externallib.php index 0965198bd84..388b0076c31 100644 --- a/user/externallib.php +++ b/user/externallib.php @@ -24,7 +24,6 @@ */ defined('MOODLE_INTERNAL') || die(); - require_once("$CFG->libdir/externallib.php"); /** @@ -1260,7 +1259,8 @@ class core_user_external extends external_api { 'platform' => new external_value(PARAM_NOTAGS, 'the device platform \'iOS\' or \'Android\' etc.'), 'version' => new external_value(PARAM_NOTAGS, 'the device version \'6.1.2\' or \'4.2.2\' etc.'), 'pushid' => new external_value(PARAM_RAW, 'the device PUSH token/key/identifier/registration id'), - 'uuid' => new external_value(PARAM_RAW, 'the device UUID') + 'uuid' => new external_value(PARAM_RAW, 'the device UUID'), + 'publickey' => new external_value(PARAM_RAW, 'the app generated public key', VALUE_DEFAULT, null), ) ); } @@ -1276,10 +1276,11 @@ class core_user_external extends external_api { * @param string $version The device version 6.1.2 or 4.2.2 etc. * @param string $pushid The device PUSH token/key/identifier/registration id. * @param string $uuid The device UUID. + * @param string $publickey The app generated public key * @return array List of possible warnings. * @since Moodle 2.6 */ - public static function add_user_device($appid, $name, $model, $platform, $version, $pushid, $uuid) { + public static function add_user_device($appid, $name, $model, $platform, $version, $pushid, $uuid, $publickey = null) { global $CFG, $USER, $DB; require_once($CFG->dirroot . "/user/lib.php"); @@ -1290,7 +1291,8 @@ class core_user_external extends external_api { 'platform' => $platform, 'version' => $version, 'pushid' => $pushid, - 'uuid' => $uuid + 'uuid' => $uuid, + 'publickey' => $publickey, )); $warnings = array(); @@ -1313,6 +1315,7 @@ class core_user_external extends external_api { foreach ($userdevices as $userdevice) { $userdevice->version = $params['version']; // Maybe the user upgraded the device. $userdevice->pushid = $params['pushid']; + $userdevice->publickey = $params['publickey']; $userdevice->timemodified = time(); $DB->update_record('user_devices', $userdevice); } @@ -1327,6 +1330,7 @@ class core_user_external extends external_api { $userdevice->version = $params['version']; $userdevice->pushid = $params['pushid']; $userdevice->uuid = $params['uuid']; + $userdevice->publickey = $params['publickey']; $userdevice->timecreated = time(); $userdevice->timemodified = $userdevice->timecreated; diff --git a/user/tests/external/update_user_device_public_key_test.php b/user/tests/external/update_user_device_public_key_test.php index b4badbcb0e3..7efb54663f1 100644 --- a/user/tests/external/update_user_device_public_key_test.php +++ b/user/tests/external/update_user_device_public_key_test.php @@ -16,7 +16,12 @@ namespace core_user\external; -use core_external\external_api; +global $CFG; + +defined('MOODLE_INTERNAL') || die(); +require_once("{$CFG->libdir}/externallib.php"); + +use external_api; use stdClass; /** diff --git a/user/tests/externallib_test.php b/user/tests/externallib_test.php index df55cc4c18f..be10fc0698d 100644 --- a/user/tests/externallib_test.php +++ b/user/tests/externallib_test.php @@ -29,6 +29,7 @@ namespace core_user; use core_files_external; use core_user_external; use externallib_advanced_testcase; +use external_api; defined('MOODLE_INTERNAL') || die(); @@ -1085,7 +1086,8 @@ class externallib_test extends externallib_advanced_testcase { 'platform' => 'Android', 'version' => '4.2.2', 'pushid' => 'apushdkasdfj4835', - 'uuid' => 'asdnfl348qlksfaasef859' + 'uuid' => 'asdnfl348qlksfaasef859', + 'publickey' => null, ); // Call the external function. @@ -1106,9 +1108,10 @@ class externallib_test extends externallib_advanced_testcase { // Test update an existing device. $device['pushid'] = 'different than before'; + $device['publickey'] = 'MFsxCzAJBgNVBAYTAkZSMRMwEQYDVQQ'; $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], - $device['version'], $device['pushid'], $device['uuid']); - $warnings = \external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); + $device['version'], $device['pushid'], $device['uuid'], $device['publickey']); + $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); $this->assertEquals(1, $DB->count_records('user_devices')); $updated = $DB->get_record('user_devices', array('pushid' => $device['pushid']));