MDL-63657 core_files: Coding style fix
[moodle.git] / files / classes / privacy / provider.php
CommitLineData
c9f95d29
FM
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 * Data provider.
19 *
20 * @package core_files
21 * @copyright 2018 Frédéric Massart
22 * @author Frédéric Massart <fred@branchup.tech>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core_files\privacy;
27defined('MOODLE_INTERNAL') || die();
28
29use core_privacy\local\metadata\collection;
4a74ab02
AN
30use core_privacy\local\request\contextlist;
31use core_privacy\local\request\approved_contextlist;
16ec426a 32use core_privacy\local\request\userlist;
4b3ca0eb 33use core_privacy\local\request\approved_userlist;
c9f95d29
FM
34
35/**
36 * Data provider class.
37 *
38 * This only describes the files table, all components must handle the file exporting
39 * and deletion themselves.
40 *
41 * @package core_files
42 * @copyright 2018 Frédéric Massart
43 * @author Frédéric Massart <fred@branchup.tech>
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 */
46class provider implements
16ec426a
MG
47 \core_privacy\local\metadata\provider,
48 \core_privacy\local\request\subsystem\plugin_provider,
49 \core_privacy\local\request\core_userlist_provider,
50 // We store a userkey for token-based file access.
51 \core_privacy\local\request\subsystem\provider {
c9f95d29
FM
52
53 /**
54 * Returns metadata.
55 *
56 * @param collection $collection The initialised collection to add items to.
57 * @return collection A listing of user data stored through this system.
58 */
59 public static function get_metadata(collection $collection) : collection {
60
61 $collection->add_database_table('files', [
62 'contenthash' => 'privacy:metadata:files:contenthash',
63 'filepath' => 'privacy:metadata:files:filepath',
64 'filename' => 'privacy:metadata:files:filename',
65 'userid' => 'privacy:metadata:files:userid',
66 'filesize' => 'privacy:metadata:files:filesize',
67 'mimetype' => 'privacy:metadata:files:mimetype',
68 'source' => 'privacy:metadata:files:source',
69 'author' => 'privacy:metadata:files:author',
70 'license' => 'privacy:metadata:files:license',
71 'timecreated' => 'privacy:metadata:files:timecreated',
72 'timemodified' => 'privacy:metadata:files:timemodified',
73 ], 'privacy:metadata:files');
74
4a74ab02
AN
75 $collection->add_subsystem_link('core_userkey', [], 'privacy:metadata:core_userkey');
76
c9f95d29
FM
77 return $collection;
78 }
79
4a74ab02
AN
80 /**
81 * Get the list of contexts that contain user information for the specified user.
82 *
83 * This is currently just the user context.
84 *
85 * @param int $userid The user to search.
86 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
87 */
88 public static function get_contexts_for_userid(int $userid) : contextlist {
89 $sql = "SELECT ctx.id
90 FROM {user_private_key} k
91 JOIN {user} u ON k.userid = u.id
92 JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel
93 WHERE k.userid = :userid AND k.script = :script";
94 $params = [
95 'userid' => $userid,
96 'contextlevel' => CONTEXT_USER,
97 'script' => 'core_files',
98 ];
99 $contextlist = new contextlist();
100 $contextlist->add_from_sql($sql, $params);
101
102 return $contextlist;
103 }
104
16ec426a
MG
105 /**
106 * Get the list of users within a specific context.
107 *
108 * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
109 */
110 public static function get_users_in_context(userlist $userlist) {
111 $context = $userlist->get_context();
112
113 if (!$context instanceof \context_user) {
114 return;
115 }
116
117 \core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'core_files');
118 }
119
4a74ab02
AN
120 /**
121 * Export all user data for the specified user, in the specified contexts.
122 *
123 * @param approved_contextlist $contextlist The approved contexts to export information for.
124 */
125 public static function export_user_data(approved_contextlist $contextlist) {
126 // If the user has data, then only the CONTEXT_USER should be present so get the first context.
127 $contexts = $contextlist->get_contexts();
128 if (count($contexts) == 0) {
129 return;
130 }
131
132 // Sanity check that context is at the user context level, then get the userid.
133 $context = reset($contexts);
134 if ($context->contextlevel !== CONTEXT_USER) {
135 return;
136 }
137
138 // Export associated userkeys.
139 $subcontext = [
140 get_string('files'),
141 ];
142 \core_userkey\privacy\provider::export_userkeys($context, $subcontext, 'core_files');
143 }
144
145 /**
146 * Delete all use data which matches the specified deletion_criteria.
147 *
148 * @param context $context A user context.
149 */
150 public static function delete_data_for_all_users_in_context(\context $context) {
151 // Sanity check that context is at the user context level, then get the userid.
152 if ($context->contextlevel !== CONTEXT_USER) {
153 return;
154 }
155
156 // Delete all the userkeys.
157 \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
158 }
159
16ec426a
MG
160 /**
161 * Delete multiple users within a single context.
162 *
163 * @param approved_userlist $userlist The approved context and user information to delete information for.
164 */
165 public static function delete_data_for_users(approved_userlist $userlist) {
166 $context = $userlist->get_context();
167
168 if ($context instanceof \context_user) {
169 \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
170 }
171 }
172
4a74ab02
AN
173 /**
174 * Delete all user data for the specified user, in the specified contexts.
175 *
176 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
177 */
178 public static function delete_data_for_user(approved_contextlist $contextlist) {
179 // If the user has data, then only the user context should be present so get the first context.
180 $contexts = $contextlist->get_contexts();
181 if (count($contexts) == 0) {
182 return;
183 }
184
185 // Sanity check that context is at the user context level, then get the userid.
186 $context = reset($contexts);
187 if ($context->contextlevel !== CONTEXT_USER) {
188 return;
189 }
190
191 // Delete all the userkeys for core_files..
192 \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
193 }
c9f95d29 194}