Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
40 / 44 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
| provider | |
90.91% |
40 / 44 |
|
57.14% |
4 / 7 |
16.19 | |
0.00% |
0 / 1 |
| get_metadata | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| get_contexts_for_userid | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| get_users_in_context | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| export_user_data | |
86.67% |
13 / 15 |
|
0.00% |
0 / 1 |
4.04 | |||
| delete_data_for_all_users_in_context | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| delete_data_for_user | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| delete_data_for_users | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| 1 | <?php |
| 2 | // This file is part of Moodle - https://moodle.org/ |
| 3 | // |
| 4 | // Moodle is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // Moodle is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with Moodle. If not, see <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | namespace message_webpush\privacy; |
| 18 | |
| 19 | use core_privacy\local\metadata\collection; |
| 20 | use core_privacy\local\request\approved_contextlist; |
| 21 | use core_privacy\local\request\approved_userlist; |
| 22 | use core_privacy\local\request\contextlist; |
| 23 | use core_privacy\local\request\transform; |
| 24 | use core_privacy\local\request\userlist; |
| 25 | use core_privacy\local\request\writer; |
| 26 | use message_webpush\local\subscriptions; |
| 27 | |
| 28 | /** |
| 29 | * Privacy provider: the browsers a user turned on, kept in the system context, and the push service they use. |
| 30 | * |
| 31 | * @package message_webpush |
| 32 | * @copyright 2026 DevLMS |
| 33 | * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 34 | */ |
| 35 | class provider implements |
| 36 | \core_privacy\local\metadata\provider, |
| 37 | \core_privacy\local\request\core_userlist_provider, |
| 38 | \core_privacy\local\request\plugin\provider { |
| 39 | #[\Override] |
| 40 | public static function get_metadata(collection $collection): collection { |
| 41 | $collection->add_database_table(subscriptions::TABLE, [ |
| 42 | 'userid' => 'privacy:metadata:subs:userid', |
| 43 | 'endpoint' => 'privacy:metadata:subs:endpoint', |
| 44 | 'p256dh' => 'privacy:metadata:subs:keys', |
| 45 | 'auth' => 'privacy:metadata:subs:keys', |
| 46 | 'label' => 'privacy:metadata:subs:label', |
| 47 | 'timecreated' => 'privacy:metadata:subs:timecreated', |
| 48 | 'timeacked' => 'privacy:metadata:subs:timeacked', |
| 49 | ], 'privacy:metadata:subs'); |
| 50 | $collection->add_external_location_link('pushservice', [ |
| 51 | 'endpoint' => 'privacy:metadata:pushservice:endpoint', |
| 52 | 'message' => 'privacy:metadata:pushservice:message', |
| 53 | ], 'privacy:metadata:pushservice'); |
| 54 | return $collection; |
| 55 | } |
| 56 | |
| 57 | #[\Override] |
| 58 | public static function get_contexts_for_userid(int $userid): contextlist { |
| 59 | global $DB; |
| 60 | $contextlist = new contextlist(); |
| 61 | if ($DB->record_exists(subscriptions::TABLE, ['userid' => $userid])) { |
| 62 | $contextlist->add_system_context(); |
| 63 | } |
| 64 | return $contextlist; |
| 65 | } |
| 66 | |
| 67 | #[\Override] |
| 68 | public static function get_users_in_context(userlist $userlist) { |
| 69 | if ($userlist->get_context()->contextlevel != CONTEXT_SYSTEM) { |
| 70 | return; |
| 71 | } |
| 72 | $userlist->add_from_sql('userid', 'SELECT userid FROM {' . subscriptions::TABLE . '}', []); |
| 73 | } |
| 74 | |
| 75 | #[\Override] |
| 76 | public static function export_user_data(approved_contextlist $contextlist) { |
| 77 | global $DB; |
| 78 | $context = \context_system::instance(); |
| 79 | if (!in_array($context->id, $contextlist->get_contextids())) { |
| 80 | return; |
| 81 | } |
| 82 | $rows = $DB->get_records(subscriptions::TABLE, ['userid' => $contextlist->get_user()->id], 'timecreated, id'); |
| 83 | if (!$rows) { |
| 84 | return; |
| 85 | } |
| 86 | // The address and keys are the browser's secrets: the export says which browsers, not how to reach them. |
| 87 | $browsers = array_map(fn($row) => (object) [ |
| 88 | 'label' => $row->label, |
| 89 | 'turnedon' => transform::datetime($row->timecreated), |
| 90 | 'lastconfirmed' => $row->timeacked ? transform::datetime($row->timeacked) : '-', |
| 91 | ], array_values($rows)); |
| 92 | writer::with_context($context)->export_data( |
| 93 | [get_string('pluginname', 'message_webpush')], |
| 94 | (object) ['browsers' => $browsers] |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | #[\Override] |
| 99 | public static function delete_data_for_all_users_in_context(\context $context) { |
| 100 | global $DB; |
| 101 | if ($context->contextlevel == CONTEXT_SYSTEM) { |
| 102 | $DB->delete_records(subscriptions::TABLE); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #[\Override] |
| 107 | public static function delete_data_for_user(approved_contextlist $contextlist) { |
| 108 | global $DB; |
| 109 | if (in_array(\context_system::instance()->id, $contextlist->get_contextids())) { |
| 110 | $DB->delete_records(subscriptions::TABLE, ['userid' => $contextlist->get_user()->id]); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[\Override] |
| 115 | public static function delete_data_for_users(approved_userlist $userlist) { |
| 116 | global $DB; |
| 117 | if ($userlist->get_context()->contextlevel != CONTEXT_SYSTEM || !$userlist->get_userids()) { |
| 118 | return; |
| 119 | } |
| 120 | [$in, $params] = $DB->get_in_or_equal($userlist->get_userids()); |
| 121 | $DB->delete_records_select(subscriptions::TABLE, "userid $in", $params); |
| 122 | } |
| 123 | } |