Merge branch 'MDL-87795-main' of https://github.com/albertgasset/moodle
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
issueNumber: MDL-87795
|
||||
notes:
|
||||
core_user:
|
||||
- message: >-
|
||||
Added new optional parameter `userid` to the `user_remove_user_device`
|
||||
function.
|
||||
type: improved
|
||||
@@ -0,0 +1,8 @@
|
||||
issueNumber: MDL-87795
|
||||
notes:
|
||||
message_airnotifier:
|
||||
- message: >-
|
||||
A new scheduled task, `message_airnotifier\task\cleanup_task`, has been
|
||||
added. This task removes orphaned records in the
|
||||
`message_airnotifier_devices` table.
|
||||
type: improved
|
||||
@@ -0,0 +1,51 @@
|
||||
<?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 message_airnotifier\task;
|
||||
|
||||
use core\task\scheduled_task;
|
||||
|
||||
/**
|
||||
* Scheduled task that cleans up orphaned Airnotifier devices.
|
||||
*
|
||||
* @package message_airnotifier
|
||||
* @copyright 2026 Moodle Pty Ltd
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class cleanup_task extends scheduled_task {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function get_name(): string {
|
||||
return get_string('taskcleanup', 'message_airnotifier');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function execute(): void {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records_select(
|
||||
'message_airnotifier_devices',
|
||||
'NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM {user_devices}
|
||||
WHERE {user_devices}.id = {message_airnotifier_devices}.userdeviceid
|
||||
)'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* This file defines tasks performed by the plugin.
|
||||
*
|
||||
* @package message_airnotifier
|
||||
* @copyright 2026 Moodle Pty Ltd
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$tasks = [
|
||||
[
|
||||
'classname' => 'message_airnotifier\task\cleanup_task',
|
||||
'blocking' => 0,
|
||||
'minute' => 'R',
|
||||
'hour' => 'R',
|
||||
'day' => 'R',
|
||||
'dayofweek' => '*',
|
||||
'month' => '*',
|
||||
],
|
||||
];
|
||||
@@ -80,6 +80,7 @@ $string['sendnotificationnotenc'] = 'Send notifications without encryption';
|
||||
$string['sendtest'] = 'Send test push notification to my devices';
|
||||
$string['sendtestconfirmation'] = 'A test push notification will be sent to the devices you use to connect to this site. Please ensure that your devices are connected to the Internet and that the mobile app is not open (since push notifications are only displayed when received in the background).';
|
||||
$string['serverconnectivityerror'] = 'This site is not able to connect to the notifications server {$a}';
|
||||
$string['taskcleanup'] = 'Clean up Airnotifier devices.';
|
||||
$string['unknowndevice'] = 'Unknown device';
|
||||
$string['userdevices'] = 'User devices';
|
||||
$string['airnotifier:managedevice'] = 'Manage devices';
|
||||
|
||||
@@ -160,6 +160,14 @@ class message_output_airnotifier extends message_output {
|
||||
|
||||
// JSON POST raw body request.
|
||||
$resp = $curl->post($serverurl, json_encode($params));
|
||||
|
||||
// Check if the device token is no longer registered.
|
||||
if ($curl->info['http_code'] === 404) {
|
||||
$json = json_decode($resp, true);
|
||||
if (($json['error'] ?? null) === 'Unregistered token') {
|
||||
user_remove_user_device($devicetoken->uuid, $devicetoken->appid, $devicetoken->userid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
// This file is part of Moodle - https://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 message_airnotifier\task;
|
||||
|
||||
/**
|
||||
* Test the functionality provided by the cleanup task.
|
||||
*
|
||||
* @package message_airnotifier
|
||||
* @category test
|
||||
* @copyright 2026 Moodle Pty Ltd
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CoversClass(cleanup_task::class)]
|
||||
final class cleanup_task_test extends \advanced_testcase {
|
||||
/**
|
||||
* Test that the cleanup task correctly removes orphaned Airnotifier devices.
|
||||
*/
|
||||
public function test_execute(): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$generator = $this->getDataGenerator();
|
||||
|
||||
// Add some user devices and Airnotifier devices.
|
||||
$user = $generator->create_user();
|
||||
$userdeviceid1 = $DB->insert_record('user_devices', [
|
||||
'appid' => 'com.moodle.moodlemobile',
|
||||
'name' => 'occam',
|
||||
'model' => 'Nexus 4',
|
||||
'platform' => 'Android',
|
||||
'version' => '4.2.2',
|
||||
'pushid' => 'apushdkasdfj4835',
|
||||
'uuid' => 'asdnfl348qlksfaasef859',
|
||||
'userid' => $user->id,
|
||||
'timecreated' => time(),
|
||||
'timemodified' => time(),
|
||||
]);
|
||||
$userdeviceid2 = $DB->insert_record('user_devices', [
|
||||
'appid' => 'com.moodle.moodlemobile',
|
||||
'name' => 'occam',
|
||||
'model' => 'Nexus 4',
|
||||
'platform' => 'Android',
|
||||
'version' => '4.2.2',
|
||||
'pushid' => 'dvcp4fkrdslv5454',
|
||||
'uuid' => 'dsvaxnc0p43rgndf4rvfdnm993',
|
||||
'userid' => $user->id,
|
||||
'timecreated' => time(),
|
||||
'timemodified' => time(),
|
||||
]);
|
||||
$DB->insert_record('message_airnotifier_devices', [
|
||||
'userdeviceid' => $userdeviceid1,
|
||||
'enable' => 1,
|
||||
]);
|
||||
$DB->insert_record('message_airnotifier_devices', [
|
||||
'userdeviceid' => $userdeviceid2,
|
||||
'enable' => 1,
|
||||
]);
|
||||
|
||||
// Simulate user device deleted.
|
||||
$DB->delete_records('user_devices', ['id' => $userdeviceid1]);
|
||||
|
||||
// Execute the task.
|
||||
\core\cron::setup_user();
|
||||
$task = new cleanup_task();
|
||||
$task->execute();
|
||||
|
||||
// Assert that the orphaned Airnotifier device has been deleted and the valid one still exists.
|
||||
self::assertFalse($DB->record_exists('message_airnotifier_devices', ['userdeviceid' => $userdeviceid1]));
|
||||
self::assertTrue($DB->record_exists('message_airnotifier_devices', ['userdeviceid' => $userdeviceid2]));
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -1148,13 +1148,16 @@ function user_is_previously_used_password($userid, $password) {
|
||||
*
|
||||
* @param string $uuid The device UUID.
|
||||
* @param string $appid The app id. If empty all the devices matching the UUID for the user will be removed.
|
||||
* @param int|null $userid The user id. If null, the current user will be used.
|
||||
* @return bool true if removed, false if the device didn't exists in the database
|
||||
* @since Moodle 2.9
|
||||
*/
|
||||
function user_remove_user_device($uuid, $appid = "") {
|
||||
function user_remove_user_device($uuid, $appid = "", $userid = null) {
|
||||
global $DB, $USER;
|
||||
|
||||
$conditions = array('uuid' => $uuid, 'userid' => $USER->id);
|
||||
$userid ??= $USER->id;
|
||||
|
||||
$conditions = ['uuid' => $uuid, 'userid' => $userid];
|
||||
if (!empty($appid)) {
|
||||
$conditions['appid'] = $appid;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user