MDL-66804 user: consistent user access times in privacy export.
[moodle.git] / user / classes / privacy / provider.php
CommitLineData
c49f3092
AG
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_user
21 * @copyright 2018 Adrian Greeve <adrian@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_user\privacy;
26
27defined('MOODLE_INTERNAL') || die();
28
29use \core_privacy\local\metadata\collection;
30use \core_privacy\local\request\transform;
31use \core_privacy\local\request\contextlist;
32use \core_privacy\local\request\approved_contextlist;
33use \core_privacy\local\request\writer;
d4d28485
MG
34use core_privacy\local\request\userlist;
35use \core_privacy\local\request\approved_userlist;
c49f3092
AG
36
37/**
38 * Privacy class for requesting user data.
39 *
40 * @package core_comment
41 * @copyright 2018 Adrian Greeve <adrian@moodle.com>
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
d4d28485
MG
44class provider implements
45 \core_privacy\local\metadata\provider,
46 \core_privacy\local\request\core_userlist_provider,
47 \core_privacy\local\request\subsystem\provider {
c49f3092
AG
48
49 /**
50 * Returns information about the user data stored in this component.
51 *
52 * @param collection $collection A list of information about this component
53 * @return collection The collection object filled out with information about this component.
54 */
55 public static function get_metadata(collection $collection) : collection {
56 $userfields = [
57 'id' => 'privacy:metadata:id',
58 'auth' => 'privacy:metadata:auth',
59 'confirmed' => 'privacy:metadata:confirmed',
60 'policyagreed' => 'privacy:metadata:policyagreed',
61 'deleted' => 'privacy:metadata:deleted',
62 'suspended' => 'privacy:metadata:suspended',
63 'mnethostid' => 'privacy:metadata:mnethostid',
64 'username' => 'privacy:metadata:username',
65 'password' => 'privacy:metadata:password',
66 'idnumber' => 'privacy:metadata:idnumber',
67 'firstname' => 'privacy:metadata:firstname',
68 'lastname' => 'privacy:metadata:lastname',
69 'email' => 'privacy:metadata:email',
70 'emailstop' => 'privacy:metadata:emailstop',
71 'icq' => 'privacy:metadata:icq',
72 'skype' => 'privacy:metadata:skype',
73 'yahoo' => 'privacy:metadata:yahoo',
74 'aim' => 'privacy:metadata:aim',
75 'msn' => 'privacy:metadata:msn',
76 'phone1' => 'privacy:metadata:phone',
77 'phone2' => 'privacy:metadata:phone',
78 'institution' => 'privacy:metadata:institution',
79 'department' => 'privacy:metadata:department',
80 'address' => 'privacy:metadata:address',
81 'city' => 'privacy:metadata:city',
82 'country' => 'privacy:metadata:country',
83 'lang' => 'privacy:metadata:lang',
84 'calendartype' => 'privacy:metadata:calendartype',
85 'theme' => 'privacy:metadata:theme',
86 'timezone' => 'privacy:metadata:timezone',
87 'firstaccess' => 'privacy:metadata:firstaccess',
88 'lastaccess' => 'privacy:metadata:lastaccess',
89 'lastlogin' => 'privacy:metadata:lastlogin',
90 'currentlogin' => 'privacy:metadata:currentlogin',
91 'lastip' => 'privacy:metadata:lastip',
92 'secret' => 'privacy:metadata:secret',
93 'picture' => 'privacy:metadata:picture',
94 'url' => 'privacy:metadata:url',
95 'description' => 'privacy:metadata:description',
96 'maildigest' => 'privacy:metadata:maildigest',
97 'maildisplay' => 'privacy:metadata:maildisplay',
98 'autosubscribe' => 'privacy:metadata:autosubscribe',
99 'trackforums' => 'privacy:metadata:trackforums',
100 'timecreated' => 'privacy:metadata:timecreated',
101 'timemodified' => 'privacy:metadata:timemodified',
102 'trustbitmask' => 'privacy:metadata:trustbitmask',
103 'imagealt' => 'privacy:metadata:imagealt',
104 'lastnamephonetic' => 'privacy:metadata:lastnamephonetic',
105 'firstnamephonetic' => 'privacy:metadata:firstnamephonetic',
106 'middlename' => 'privacy:metadata:middlename',
107 'alternatename' => 'privacy:metadata:alternatename'
108 ];
109
110 $passwordhistory = [
111 'userid' => 'privacy:metadata:userid',
112 'hash' => 'privacy:metadata:hash',
113 'timecreated' => 'privacy:metadata:timecreated'
114 ];
115
116 $lastaccess = [
117 'userid' => 'privacy:metadata:userid',
118 'courseid' => 'privacy:metadata:courseid',
119 'timeaccess' => 'privacy:metadata:timeaccess'
120 ];
121
122 $userpasswordresets = [
123 'userid' => 'privacy:metadata:userid',
124 'timerequested' => 'privacy:metadata:timerequested',
125 'timererequested' => 'privacy:metadata:timererequested',
126 'token' => 'privacy:metadata:token'
127 ];
128
129 $userdevices = [
130 'userid' => 'privacy:metadata:userid',
131 'appid' => 'privacy:metadata:appid',
132 'name' => 'privacy:metadata:devicename',
133 'model' => 'privacy:metadata:model',
134 'platform' => 'privacy:metadata:platform',
135 'version' => 'privacy:metadata:version',
136 'pushid' => 'privacy:metadata:pushid',
137 'uuid' => 'privacy:metadata:uuid',
138 'timecreated' => 'privacy:metadata:timecreated',
139 'timemodified' => 'privacy:metadata:timemodified'
140 ];
141
142 $usersessions = [
143 'state' => 'privacy:metadata:state',
144 'sid' => 'privacy:metadata:sid',
145 'userid' => 'privacy:metadata:userid',
146 'sessdata' => 'privacy:metadata:sessdata',
147 'timecreated' => 'privacy:metadata:timecreated',
148 'timemodified' => 'privacy:metadata:timemodified',
149 'firstip' => 'privacy:metadata:firstip',
150 'lastip' => 'privacy:metadata:lastip'
151 ];
152
153 $courserequest = [
154 'fullname' => 'privacy:metadata:fullname',
155 'shortname' => 'privacy:metadata:shortname',
156 'summary' => 'privacy:metadata:summary',
157 'category' => 'privacy:metadata:category',
158 'reason' => 'privacy:metadata:reason',
159 'requester' => 'privacy:metadata:requester'
160 ];
161
d1620c57
MG
162 $mypages = [
163 'userid' => 'privacy:metadata:my_pages:userid',
164 'name' => 'privacy:metadata:my_pages:name',
165 'private' => 'privacy:metadata:my_pages:private',
166 ];
167
168 $userpreferences = [
169 'userid' => 'privacy:metadata:user_preferences:userid',
170 'name' => 'privacy:metadata:user_preferences:name',
171 'value' => 'privacy:metadata:user_preferences:value'
172 ];
173
c49f3092
AG
174 $collection->add_database_table('user', $userfields, 'privacy:metadata:usertablesummary');
175 $collection->add_database_table('user_password_history', $passwordhistory, 'privacy:metadata:passwordtablesummary');
176 $collection->add_database_table('user_password_resets', $userpasswordresets, 'privacy:metadata:passwordresettablesummary');
177 $collection->add_database_table('user_lastaccess', $lastaccess, 'privacy:metadata:lastaccesstablesummary');
178 $collection->add_database_table('user_devices', $userdevices, 'privacy:metadata:devicetablesummary');
179 $collection->add_database_table('course_request', $courserequest, 'privacy:metadata:requestsummary');
180 $collection->add_database_table('sessions', $usersessions, 'privacy:metadata:sessiontablesummary');
d1620c57
MG
181 $collection->add_database_table('my_pages', $mypages, 'privacy:metadata:my_pages');
182 $collection->add_database_table('user_preferences', $userpreferences, 'privacy:metadata:user_preferences');
c49f3092
AG
183 $collection->add_subsystem_link('core_files', [], 'privacy:metadata:filelink');
184
185 return $collection;
186 }
187
188 /**
189 * Get the list of contexts that contain user information for the specified user.
190 *
191 * @param int $userid The user to search.
192 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
193 */
194 public static function get_contexts_for_userid(int $userid) : contextlist {
195 $params = ['userid' => $userid, 'contextuser' => CONTEXT_USER];
196 $sql = "SELECT id
197 FROM {context}
198 WHERE instanceid = :userid and contextlevel = :contextuser";
199 $contextlist = new contextlist();
200 $contextlist->add_from_sql($sql, $params);
201 return $contextlist;
202 }
203
d4d28485
MG
204 /**
205 * Get the list of users within a specific context.
206 *
207 * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
208 */
209 public static function get_users_in_context(userlist $userlist) {
210 $context = $userlist->get_context();
211
212 if (!$context instanceof \context_user) {
213 return;
214 }
215
dfb3677c 216 $userlist->add_user($context->instanceid);
d4d28485
MG
217 }
218
c49f3092
AG
219 /**
220 * Export all user data for the specified user, in the specified contexts.
221 *
222 * @param approved_contextlist $contextlist The approved contexts to export information for.
223 */
224 public static function export_user_data(approved_contextlist $contextlist) {
225 $context = $contextlist->current();
226 $user = \core_user::get_user($contextlist->get_user()->id);
227 static::export_user($user, $context);
228 static::export_password_history($user->id, $context);
229 static::export_password_resets($user->id, $context);
230 static::export_lastaccess($user->id, $context);
231 static::export_course_requests($user->id, $context);
232 static::export_user_devices($user->id, $context);
233 static::export_user_session_data($user->id, $context);
234 }
235
236 /**
237 * Delete all data for all users in the specified context.
238 *
239 * @param context $context The specific context to delete data for.
240 */
241 public static function delete_data_for_all_users_in_context(\context $context) {
242 // Only delete data for a user context.
243 if ($context->contextlevel == CONTEXT_USER) {
244 static::delete_user_data($context->instanceid, $context);
245 }
246 }
247
d4d28485
MG
248 /**
249 * Delete multiple users within a single context.
250 *
251 * @param approved_userlist $userlist The approved context and user information to delete information for.
252 */
253 public static function delete_data_for_users(approved_userlist $userlist) {
254
255 $context = $userlist->get_context();
256
257 if ($context instanceof \context_user) {
258 static::delete_user_data($context->instanceid, $context);
259 }
260 }
261
c49f3092
AG
262 /**
263 * Delete all user data for the specified user, in the specified contexts.
264 *
265 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
266 */
267 public static function delete_data_for_user(approved_contextlist $contextlist) {
268 foreach ($contextlist as $context) {
269 // Let's be super certain that we have the right information for this user here.
270 if ($context->contextlevel == CONTEXT_USER && $contextlist->get_user()->id == $context->instanceid) {
271 static::delete_user_data($contextlist->get_user()->id, $contextlist->current());
272 }
273 }
274 }
275
276 /**
277 * Deletes non vital information about a user.
278 *
279 * @param int $userid The user ID to delete
280 * @param \context $context The user context
281 */
282 protected static function delete_user_data(int $userid, \context $context) {
283 global $DB;
284
285 // Delete password history.
286 $DB->delete_records('user_password_history', ['userid' => $userid]);
287 // Delete last access.
288 $DB->delete_records('user_lastaccess', ['userid' => $userid]);
289 // Delete password resets.
290 $DB->delete_records('user_password_resets', ['userid' => $userid]);
291 // Delete user devices.
292 $DB->delete_records('user_devices', ['userid' => $userid]);
293 // Delete user course requests.
294 $DB->delete_records('course_request', ['requester' => $userid]);
295 // Delete sessions.
296 $DB->delete_records('sessions', ['userid' => $userid]);
297 // Do I delete user preferences? Seems like the right place to do it.
298 $DB->delete_records('user_preferences', ['userid' => $userid]);
299
300 // Delete all of the files for this user.
301 $fs = get_file_storage();
302 $fs->delete_area_files($context->id, 'user');
303
304 // For the user record itself we only want to remove unnecessary data. We still need the core data to keep as a record
305 // that we actually did follow the request to be forgotten.
306 $user = \core_user::get_user($userid);
307 // Update fields we wish to change to nothing.
308 $user->deleted = 1;
309 $user->idnumber = '';
310 $user->emailstop = 0;
311 $user->icq = '';
312 $user->skype = '';
313 $user->yahoo = '';
314 $user->aim = '';
315 $user->msn = '';
316 $user->phone1 = '';
317 $user->phone2 = '';
318 $user->institution = '';
319 $user->department = '';
320 $user->address = '';
321 $user->city = '';
322 $user->country = '';
323 $user->lang = '';
324 $user->calendartype = '';
325 $user->theme = '';
326 $user->timezone = '';
327 $user->firstaccess = 0;
328 $user->lastaccess = 0;
329 $user->lastlogin = 0;
330 $user->currentlogin = 0;
331 $user->lastip = 0;
332 $user->secret = '';
333 $user->picture = '';
334 $user->url = '';
335 $user->description = '';
336 $user->descriptionformat = 0;
337 $user->mailformat = 0;
338 $user->maildigest = 0;
339 $user->maildisplay = 0;
340 $user->autosubscribe = 0;
341 $user->trackforums = 0;
342 $user->timecreated = 0;
343 $user->timemodified = 0;
344 $user->trustbitmask = 0;
345 $user->imagealt = '';
346 $user->lastnamephonetic = '';
347 $user->firstnamephonetic = '';
348 $user->middlename = '';
349 $user->alternatename = '';
350 $DB->update_record('user', $user);
351 }
352
353 /**
354 * Export core user data.
355 *
356 * @param \stdClass $user The user object.
357 * @param \context $context The user context.
358 */
359 protected static function export_user(\stdClass $user, \context $context) {
360 $data = (object) [
361 'auth' => $user->auth,
362 'confirmed' => transform::yesno($user->confirmed),
363 'policyagreed' => transform::yesno($user->policyagreed),
364 'deleted' => transform::yesno($user->deleted),
365 'suspended' => transform::yesno($user->suspended),
366 'username' => $user->username,
367 'idnumber' => $user->idnumber,
368 'firstname' => format_string($user->firstname, true, ['context' => $context]),
369 'lastname' => format_string($user->lastname, true, ['context' => $context]),
370 'email' => $user->email,
371 'emailstop' => transform::yesno($user->emailstop),
372 'icq' => format_string($user->icq, true, ['context' => $context]),
373 'skype' => format_string($user->skype, true, ['context' => $context]),
374 'yahoo' => format_string($user->yahoo, true, ['context' => $context]),
375 'aim' => format_string($user->aim, true, ['context' => $context]),
376 'msn' => format_string($user->msn, true, ['context' => $context]),
377 'phone1' => format_string($user->phone1, true, ['context' => $context]),
378 'phone2' => format_string($user->phone2, true, ['context' => $context]),
379 'institution' => format_string($user->institution, true, ['context' => $context]),
380 'department' => format_string($user->department, true, ['context' => $context]),
381 'address' => format_string($user->address, true, ['context' => $context]),
382 'city' => format_string($user->city, true, ['context' => $context]),
383 'country' => format_string($user->country, true, ['context' => $context]),
384 'lang' => $user->lang,
385 'calendartype' => $user->calendartype,
386 'theme' => $user->theme,
387 'timezone' => $user->timezone,
8208292f
PH
388 'firstaccess' => $user->firstaccess ? transform::datetime($user->firstaccess) : null,
389 'lastaccess' => $user->lastaccess ? transform::datetime($user->lastaccess) : null,
390 'lastlogin' => $user->lastlogin ? transform::datetime($user->lastlogin) : null,
391 'currentlogin' => $user->currentlogin ? transform::datetime($user->currentlogin) : null,
c49f3092
AG
392 'lastip' => $user->lastip,
393 'secret' => $user->secret,
394 'picture' => $user->picture,
395 'url' => $user->url,
396 'description' => format_text($user->description, $user->descriptionformat, ['context' => $context]),
397 'maildigest' => transform::yesno($user->maildigest),
398 'maildisplay' => $user->maildisplay,
399 'autosubscribe' => transform::yesno($user->autosubscribe),
400 'trackforums' => transform::yesno($user->trackforums),
401 'timecreated' => transform::datetime($user->timecreated),
402 'timemodified' => transform::datetime($user->timemodified),
403 'imagealt' => format_string($user->imagealt, true, ['context' => $context]),
404 'lastnamephonetic' => format_string($user->lastnamephonetic, true, ['context' => $context]),
405 'firstnamephonetic' => format_string($user->firstnamephonetic, true, ['context' => $context]),
406 'middlename' => format_string($user->middlename, true, ['context' => $context]),
407 'alternatename' => format_string($user->alternatename, true, ['context' => $context])
408 ];
409 if (isset($data->description)) {
410 $data->description = writer::with_context($context)->rewrite_pluginfile_urls(
411 [get_string('privacy:descriptionpath', 'user')], 'user', 'profile', '', $data->description);
412 }
413 writer::with_context($context)->export_area_files([], 'user', 'profile', 0)
414 ->export_data([], $data);
415 // Export profile images.
416 writer::with_context($context)->export_area_files([get_string('privacy:profileimagespath', 'user')], 'user', 'icon', 0);
417 // Export private files.
418 writer::with_context($context)->export_area_files([get_string('privacy:privatefilespath', 'user')], 'user', 'private', 0);
419 // Export draft files.
420 writer::with_context($context)->export_area_files([get_string('privacy:draftfilespath', 'user')], 'user', 'draft', false);
421 }
422
423 /**
424 * Export information about the last time a user accessed a course.
425 *
426 * @param int $userid The user ID.
427 * @param \context $context The user context.
428 */
429 protected static function export_lastaccess(int $userid, \context $context) {
430 global $DB;
431 $sql = "SELECT c.id, c.fullname, ul.timeaccess
432 FROM {user_lastaccess} ul
433 JOIN {course} c ON c.id = ul.courseid
434 WHERE ul.userid = :userid";
435 $params = ['userid' => $userid];
436 $records = $DB->get_records_sql($sql, $params);
437 if (!empty($records)) {
438 $lastaccess = (object) array_map(function($record) use ($context) {
439 return [
440 'course_name' => format_string($record->fullname, true, ['context' => $context]),
441 'timeaccess' => transform::datetime($record->timeaccess)
442 ];
443 }, $records);
444 writer::with_context($context)->export_data([get_string('privacy:lastaccesspath', 'user')], $lastaccess);
445 }
446 }
447
448 /**
449 * Exports information about password resets.
450 *
451 * @param int $userid The user ID
452 * @param \context $context Context for this user.
453 */
454 protected static function export_password_resets(int $userid, \context $context) {
455 global $DB;
456 $records = $DB->get_records('user_password_resets', ['userid' => $userid]);
457 if (!empty($records)) {
458 $passwordresets = (object) array_map(function($record) {
459 return [
460 'timerequested' => transform::datetime($record->timerequested),
461 'timererequested' => transform::datetime($record->timererequested)
462 ];
463 }, $records);
464 writer::with_context($context)->export_data([get_string('privacy:passwordresetpath', 'user')], $passwordresets);
465 }
466 }
467
468 /**
469 * Exports information about the user's mobile devices.
470 *
471 * @param int $userid The user ID.
472 * @param \context $context Context for this user.
473 */
474 protected static function export_user_devices(int $userid, \context $context) {
475 global $DB;
476 $records = $DB->get_records('user_devices', ['userid' => $userid]);
477 if (!empty($records)) {
478 $userdevices = (object) array_map(function($record) {
479 return [
480 'appid' => $record->appid,
481 'name' => $record->name,
482 'model' => $record->model,
483 'platform' => $record->platform,
484 'version' => $record->version,
485 'timecreated' => transform::datetime($record->timecreated),
486 'timemodified' => transform::datetime($record->timemodified)
487 ];
488 }, $records);
489 writer::with_context($context)->export_data([get_string('privacy:devicespath', 'user')], $userdevices);
490 }
491 }
492
493 /**
494 * Exports information about course requests this user made.
495 *
496 * @param int $userid The user ID.
497 * @param \context $context The context object
498 */
499 protected static function export_course_requests(int $userid, \context $context) {
500 global $DB;
501 $sql = "SELECT cr.shortname, cr.fullname, cr.summary, cc.name AS category, cr.reason
502 FROM {course_request} cr
503 JOIN {course_categories} cc ON cr.category = cc.id
504 WHERE cr.requester = :userid";
505 $params = ['userid' => $userid];
506 $records = $DB->get_records_sql($sql, $params);
507 if ($records) {
508 writer::with_context($context)->export_data([get_string('privacy:courserequestpath', 'user')], (object) $records);
509 }
510 }
511
512 /**
513 * Get details about the user's password history.
514 *
515 * @param int $userid The user ID that we are getting the password history for.
516 * @param \context $context the user context.
517 */
518 protected static function export_password_history(int $userid, \context $context) {
519 global $DB;
520
521 // Just provide a count of how many entries we have.
522 $recordcount = $DB->count_records('user_password_history', ['userid' => $userid]);
523 if ($recordcount) {
524 $passwordhistory = (object) ['password_history_count' => $recordcount];
525 writer::with_context($context)->export_data([get_string('privacy:passwordhistorypath', 'user')], $passwordhistory);
526 }
527 }
528
529 /**
530 * Exports information about the user's session.
531 *
532 * @param int $userid The user ID.
533 * @param \context $context The context for this user.
534 */
535 protected static function export_user_session_data(int $userid, \context $context) {
536 global $DB, $SESSION;
537
538 $records = $DB->get_records('sessions', ['userid' => $userid]);
539 if (!empty($records)) {
540 $sessiondata = (object) array_map(function($record) {
541 return [
542 'state' => $record->state,
543 'sessdata' => base64_decode($record->sessdata),
544 'timecreated' => transform::datetime($record->timecreated),
545 'timemodified' => transform::datetime($record->timemodified),
546 'firstip' => $record->firstip,
547 'lastip' => $record->lastip
548 ];
549 }, $records);
550 writer::with_context($context)->export_data([get_string('privacy:sessionpath', 'user')], $sessiondata);
551 }
552 }
553}