MDL-63535 core_rss: Coding style fix
[moodle.git] / lib / userkey / classes / privacy / provider.php
CommitLineData
04427895
AN
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.
13//
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/>.
16
17/**
18 * Privacy class for requesting user data.
19 *
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
23 */
24
25namespace core_userkey\privacy;
26
27defined('MOODLE_INTERNAL') || die();
28
6a91cc80
AN
29use core_privacy\local\metadata\collection;
30use core_privacy\local\request\transform;
31use core_privacy\local\request\writer;
c61132c6 32use core_privacy\local\request\userlist;
04427895
AN
33
34/**
35 * Privacy class for requesting user data.
36 *
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
40 */
41class provider implements
42 \core_privacy\local\metadata\provider,
43
44 \core_privacy\local\request\subsystem\plugin_provider {
45
46 /**
47 * Returns meta data about this system.
48 *
49 * @param collection $collection The initialised collection to add items to.
50 * @return collection A listing of user data stored through this system.
51 */
52 public static function get_metadata(collection $collection) : collection {
53 $collection->add_database_table('user_private_key', [
54 'script' => 'privacy:metadata:user_private_key:script',
55 'value' => 'privacy:metadata:user_private_key:value',
56 'userid' => 'privacy:metadata:user_private_key:userid',
57 'instance' => 'privacy:metadata:user_private_key:instance',
58 'iprestriction' => 'privacy:metadata:user_private_key:iprestriction',
59 'validuntil' => 'privacy:metadata:user_private_key:validuntil',
60 'timecreated' => 'privacy:metadata:user_private_key:timecreated',
61 ], 'privacy:metadata:user_private_key');
62
63 return $collection;
64 }
65
c61132c6
MG
66 /**
67 * Get the list of users within a specific context for this system.
68 *
69 * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
70 * @param context $context The context.
71 * @param string $script The unique target identifier.
72 * @param int $instance The instance ID.
73 */
74 public static function get_user_contexts_with_script(userlist $userlist, \context $context, string $script,
75 int $instance = null) {
76 if (!$context instanceof \context_user) {
77 return;
78 }
79
80 $params = [
81 'userid' => $context->instanceid,
82 'script' => $script
83 ];
84
85 $whereinstance = '';
86
87 if (!empty($instance)) {
88 $params['instance'] = $instance;
89 $whereinstance = ' AND k.instance = :instance';
90 }
91
92 $sql = "SELECT k.userid
93 FROM {user_private_key} k
94 WHERE k.script = :script
95 AND k.userid = :userid
96 {$whereinstance}";
97
98 $userlist->add_from_sql('userid', $sql, $params);
99 }
100
04427895
AN
101 /**
102 * Exports the data relating to user keys for the specified scripts and instance, within the specified
103 * context/subcontext.
104 *
105 * @param \context $context Context owner of the data.
106 * @param array $subcontext Context owner of the data.
107 * @param string $script The owner of the data (usually a component name).
108 * @param int $instance The instance owner of the data.
109 */
110 public static function export_userkeys(\context $context, array $subcontext, string $script, $instance = null) {
111 global $DB, $USER;
112
113 $searchparams = [
114 'script' => $script,
115 'userid' => $USER->id,
116 ];
117
118 if (null !== $instance) {
119 $searchparams['instance'] = $instance;
120 }
121
122 $keys = $DB->get_recordset('user_private_key', $searchparams);
123 $keydata = [];
124 foreach ($keys as $key) {
125 $keydata[] = (object) [
126 'script' => $key->script,
127 'instance' => $key->instance,
128 'iprestriction' => $key->iprestriction,
129 'validuntil' => transform::datetime($key->validuntil),
130 'timecreated' => transform::datetime($key->timecreated),
131 ];
132 }
133 $keys->close();
134
135 if (!empty($keydata)) {
136 $data = (object) [
137 'keys' => $keydata,
138 ];
139
140 writer::with_context($context)->export_related_data($subcontext, 'userkeys', $data);
141 }
142 }
143
144 /**
145 * Deletes all userkeys for a script.
146 *
147 * @param string $script The owner of the data (usually a component name).
148 * @param int $userid The owner of the data.
149 * @param int $instance The instance owner of the data.
150 */
151 public static function delete_userkeys(string $script, $userid = null, $instance = null) {
152 global $DB;
153
154 $searchparams = [
155 'script' => $script,
156 ];
157
158 if (null !== $userid) {
159 $searchparams['userid'] = $userid;
160 }
161
162 if (null !== $instance) {
163 $searchparams['instance'] = $instance;
164 }
165
166 $DB->delete_records('user_private_key', $searchparams);
167 }
168}