MDL-63924 privacy: Add shared user providers to subsytsems
[moodle.git] / lib / userkey / classes / privacy / provider.php
blob72bd9e95809a06062c0105b009222b0fbfac3cdc
1 <?php
2 // This file is part of Moodle - http://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.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Privacy class for requesting user data.
20 * @package core_userkey
21 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_userkey\privacy;
27 defined('MOODLE_INTERNAL') || die();
29 use core_privacy\local\metadata\collection;
30 use core_privacy\local\request\transform;
31 use core_privacy\local\request\writer;
32 use core_privacy\local\request\userlist;
34 /**
35 * Privacy class for requesting user data.
37 * @package core_userkey
38 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class provider implements
42 \core_privacy\local\metadata\provider,
44 \core_privacy\local\request\subsystem\plugin_provider,
45 \core_privacy\local\request\shared_userlist_provider
48 /**
49 * Returns meta data about this system.
51 * @param collection $collection The initialised collection to add items to.
52 * @return collection A listing of user data stored through this system.
54 public static function get_metadata(collection $collection) : collection {
55 $collection->add_database_table('user_private_key', [
56 'script' => 'privacy:metadata:user_private_key:script',
57 'value' => 'privacy:metadata:user_private_key:value',
58 'userid' => 'privacy:metadata:user_private_key:userid',
59 'instance' => 'privacy:metadata:user_private_key:instance',
60 'iprestriction' => 'privacy:metadata:user_private_key:iprestriction',
61 'validuntil' => 'privacy:metadata:user_private_key:validuntil',
62 'timecreated' => 'privacy:metadata:user_private_key:timecreated',
63 ], 'privacy:metadata:user_private_key');
65 return $collection;
68 /**
69 * Get the list of users within a specific context for this system.
71 * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
72 * @param context $context The context.
73 * @param string $script The unique target identifier.
74 * @param int $instance The instance ID.
76 public static function get_user_contexts_with_script(userlist $userlist, \context $context, string $script,
77 int $instance = null) {
78 if (!$context instanceof \context_user) {
79 return;
82 $params = [
83 'userid' => $context->instanceid,
84 'script' => $script
87 $whereinstance = '';
89 if (!empty($instance)) {
90 $params['instance'] = $instance;
91 $whereinstance = ' AND k.instance = :instance';
94 $sql = "SELECT k.userid
95 FROM {user_private_key} k
96 WHERE k.script = :script
97 AND k.userid = :userid
98 {$whereinstance}";
100 $userlist->add_from_sql('userid', $sql, $params);
104 * Exports the data relating to user keys for the specified scripts and instance, within the specified
105 * context/subcontext.
107 * @param \context $context Context owner of the data.
108 * @param array $subcontext Context owner of the data.
109 * @param string $script The owner of the data (usually a component name).
110 * @param int $instance The instance owner of the data.
112 public static function export_userkeys(\context $context, array $subcontext, string $script, $instance = null) {
113 global $DB, $USER;
115 $searchparams = [
116 'script' => $script,
117 'userid' => $USER->id,
120 if (null !== $instance) {
121 $searchparams['instance'] = $instance;
124 $keys = $DB->get_recordset('user_private_key', $searchparams);
125 $keydata = [];
126 foreach ($keys as $key) {
127 $keydata[] = (object) [
128 'script' => $key->script,
129 'instance' => $key->instance,
130 'iprestriction' => $key->iprestriction,
131 'validuntil' => transform::datetime($key->validuntil),
132 'timecreated' => transform::datetime($key->timecreated),
135 $keys->close();
137 if (!empty($keydata)) {
138 $data = (object) [
139 'keys' => $keydata,
142 writer::with_context($context)->export_related_data($subcontext, 'userkeys', $data);
147 * Deletes all userkeys for a script.
149 * @param string $script The owner of the data (usually a component name).
150 * @param int $userid The owner of the data.
151 * @param int $instance The instance owner of the data.
153 public static function delete_userkeys(string $script, $userid = null, $instance = null) {
154 global $DB;
156 $searchparams = [
157 'script' => $script,
160 if (null !== $userid) {
161 $searchparams['userid'] = $userid;
164 if (null !== $instance) {
165 $searchparams['instance'] = $instance;
168 $DB->delete_records('user_private_key', $searchparams);