Differences Between: [Versions 402 and 403]
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 namespace core_external\privacy; 18 19 use context; 20 use context_user; 21 use core_privacy\local\metadata\collection; 22 use core_privacy\local\request\approved_contextlist; 23 use core_privacy\local\request\transform; 24 use core_privacy\local\request\writer; 25 use core_privacy\local\request\userlist; 26 use core_privacy\local\request\approved_userlist; 27 28 /** 29 * Data provider class. 30 * 31 * @package core_external 32 * @copyright 2018 Frédéric Massart 33 * @author Frédéric Massart <fred@branchup.tech> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class provider implements 37 \core_privacy\local\metadata\provider, 38 \core_privacy\local\request\core_userlist_provider, 39 \core_privacy\local\request\subsystem\provider { 40 41 /** 42 * Returns metadata. 43 * 44 * @param collection $collection The initialised collection to add items to. 45 * @return collection A listing of user data stored through this system. 46 */ 47 public static function get_metadata(collection $collection) : collection { 48 49 $collection->add_database_table('external_tokens', [ 50 'token' => 'privacy:metadata:tokens:token', 51 'privatetoken' => 'privacy:metadata:tokens:privatetoken', 52 'tokentype' => 'privacy:metadata:tokens:tokentype', 53 'userid' => 'privacy:metadata:tokens:userid', 54 'creatorid' => 'privacy:metadata:tokens:creatorid', 55 'iprestriction' => 'privacy:metadata:tokens:iprestriction', 56 'validuntil' => 'privacy:metadata:tokens:validuntil', 57 'timecreated' => 'privacy:metadata:tokens:timecreated', 58 'lastaccess' => 'privacy:metadata:tokens:lastaccess', 59 ], 'privacy:metadata:tokens'); 60 61 $collection->add_database_table('external_services_users', [ 62 'userid' => 'privacy:metadata:serviceusers:userid', 63 'iprestriction' => 'privacy:metadata:serviceusers:iprestriction', 64 'validuntil' => 'privacy:metadata:serviceusers:validuntil', 65 'timecreated' => 'privacy:metadata:serviceusers:timecreated', 66 ], 'privacy:metadata:serviceusers'); 67 68 return $collection; 69 } 70 71 /** 72 * Get the list of contexts that contain user information for the specified user. 73 * 74 * @param int $userid The user to search. 75 * @return \core_privacy\local\request\contextlist $contextlist The contextlist containing the list of contexts 76 * used in this plugin. 77 */ 78 public static function get_contexts_for_userid(int $userid): \core_privacy\local\request\contextlist { 79 $contextlist = new \core_privacy\local\request\contextlist(); 80 81 $sql = " 82 SELECT ctx.id 83 FROM {external_tokens} t 84 JOIN {context} ctx 85 ON ctx.instanceid = t.userid 86 AND ctx.contextlevel = :userlevel 87 WHERE t.userid = :userid1 88 OR t.creatorid = :userid2"; 89 $contextlist->add_from_sql($sql, ['userlevel' => CONTEXT_USER, 'userid1' => $userid, 'userid2' => $userid]); 90 91 $sql = " 92 SELECT ctx.id 93 FROM {external_services_users} su 94 JOIN {context} ctx 95 ON ctx.instanceid = su.userid 96 AND ctx.contextlevel = :userlevel 97 WHERE su.userid = :userid"; 98 $contextlist->add_from_sql($sql, ['userlevel' => CONTEXT_USER, 'userid' => $userid]); 99 100 return $contextlist; 101 } 102 103 /** 104 * Get the list of users within a specific context. 105 * 106 * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination. 107 */ 108 public static function get_users_in_context(userlist $userlist) { 109 global $DB; 110 111 $context = $userlist->get_context(); 112 113 if (!$context instanceof \context_user) { 114 return; 115 } 116 117 $userid = $context->instanceid; 118 119 $hasdata = false; 120 $hasdata = $hasdata || $DB->record_exists_select('external_tokens', 'userid = ? OR creatorid = ?', [$userid, $userid]); 121 $hasdata = $hasdata || $DB->record_exists('external_services_users', ['userid' => $userid]); 122 123 if ($hasdata) { 124 $userlist->add_user($userid); 125 } 126 } 127 128 /** 129 * Export all user data for the specified user, in the specified contexts. 130 * 131 * @param approved_contextlist $contextlist The approved contexts to export information for. 132 */ 133 public static function export_user_data(approved_contextlist $contextlist) { 134 global $DB; 135 136 $userid = $contextlist->get_user()->id; 137 $contexts = array_reduce($contextlist->get_contexts(), function($carry, $context) use ($userid) { 138 if ($context->contextlevel == CONTEXT_USER) { 139 if ($context->instanceid == $userid) { 140 $carry['has_mine'] = true; 141 } else { 142 $carry['others'][] = $context->instanceid; 143 } 144 } 145 return $carry; 146 }, [ 147 'has_mine' => false, 148 'others' => [] 149 ]); 150 151 $path = [get_string('services', 'core_external')]; 152 153 // Exporting my stuff. 154 if ($contexts['has_mine']) { 155 156 $data = []; 157 158 // Exporting my tokens. 159 $sql = " 160 SELECT t.*, s.name as externalservicename 161 FROM {external_tokens} t 162 JOIN {external_services} s 163 ON s.id = t.externalserviceid 164 WHERE t.userid = :userid 165 ORDER BY t.id"; 166 $recordset = $DB->get_recordset_sql($sql, ['userid' => $userid]); 167 foreach ($recordset as $record) { 168 if (!isset($data['tokens'])) { 169 $data['tokens'] = []; 170 } 171 $data['tokens'][] = static::transform_token($record); 172 } 173 $recordset->close(); 174 175 // Exporting the services I have access to. 176 $sql = " 177 SELECT su.*, s.name as externalservicename 178 FROM {external_services_users} su 179 JOIN {external_services} s 180 ON s.id = su.externalserviceid 181 WHERE su.userid = :userid 182 ORDER BY su.id"; 183 $recordset = $DB->get_recordset_sql($sql, ['userid' => $userid]); 184 foreach ($recordset as $record) { 185 if (!isset($data['services_user'])) { 186 $data['services_user'] = []; 187 } 188 $data['services_user'][] = [ 189 'external_service' => $record->externalservicename, 190 'ip_restriction' => $record->iprestriction, 191 'valid_until' => $record->validuntil ? transform::datetime($record->validuntil) : null, 192 'created_on' => transform::datetime($record->timecreated), 193 ]; 194 } 195 $recordset->close(); 196 197 if (!empty($data)) { 198 writer::with_context(context_user::instance($userid))->export_data($path, (object) $data); 199 }; 200 } 201 202 // Exporting the tokens I created. 203 if (!empty($contexts['others'])) { 204 list($insql, $inparams) = $DB->get_in_or_equal($contexts['others'], SQL_PARAMS_NAMED); 205 $sql = " 206 SELECT t.*, s.name as externalservicename 207 FROM {external_tokens} t 208 JOIN {external_services} s 209 ON s.id = t.externalserviceid 210 WHERE t.userid $insql 211 AND t.creatorid = :userid1 212 AND t.userid <> :userid2 213 ORDER BY t.userid, t.id"; 214 $params = array_merge($inparams, ['userid1' => $userid, 'userid2' => $userid]); 215 $recordset = $DB->get_recordset_sql($sql, $params); 216 static::recordset_loop_and_export($recordset, 'userid', [], function($carry, $record) { 217 $carry[] = static::transform_token($record); 218 return $carry; 219 }, function($userid, $data) use ($path) { 220 writer::with_context(context_user::instance($userid))->export_related_data($path, 'created_by_you', (object) [ 221 'tokens' => $data 222 ]); 223 }); 224 } 225 } 226 227 /** 228 * Delete all data for all users in the specified context. 229 * 230 * @param context $context The specific context to delete data for. 231 */ 232 public static function delete_data_for_all_users_in_context(context $context) { 233 if ($context->contextlevel != CONTEXT_USER) { 234 return; 235 } 236 static::delete_user_data($context->instanceid); 237 } 238 239 /** 240 * Delete multiple users within a single context. 241 * 242 * @param approved_userlist $userlist The approved context and user information to delete information for. 243 */ 244 public static function delete_data_for_users(approved_userlist $userlist) { 245 246 $context = $userlist->get_context(); 247 248 if ($context instanceof \context_user) { 249 static::delete_user_data($context->instanceid); 250 } 251 } 252 253 /** 254 * Delete all user data for the specified user, in the specified contexts. 255 * 256 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for. 257 */ 258 public static function delete_data_for_user(approved_contextlist $contextlist) { 259 $userid = $contextlist->get_user()->id; 260 foreach ($contextlist as $context) { 261 if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $userid) { 262 static::delete_user_data($context->instanceid); 263 break; 264 } 265 } 266 } 267 268 /** 269 * Delete user data. 270 * 271 * @param int $userid The user ID. 272 * @return void 273 */ 274 protected static function delete_user_data($userid) { 275 global $DB; 276 $DB->delete_records('external_tokens', ['userid' => $userid]); 277 $DB->delete_records('external_services_users', ['userid' => $userid]); 278 } 279 280 /** 281 * Transform a token entry. 282 * 283 * @param object $record The token record. 284 * @return array 285 */ 286 protected static function transform_token($record) { 287 $notexportedstr = get_string('privacy:request:notexportedsecurity', 'core_external'); 288 return [ 289 'external_service' => $record->externalservicename, 290 'token' => $notexportedstr, 291 'private_token' => $record->privatetoken ? $notexportedstr : null, 292 'ip_restriction' => $record->iprestriction, 293 'valid_until' => $record->validuntil ? transform::datetime($record->validuntil) : null, 294 'created_on' => transform::datetime($record->timecreated), 295 'last_access' => $record->lastaccess ? transform::datetime($record->lastaccess) : null, 296 ]; 297 } 298 299 /** 300 * Loop and export from a recordset. 301 * 302 * @param \moodle_recordset $recordset The recordset. 303 * @param string $splitkey The record key to determine when to export. 304 * @param mixed $initial The initial data to reduce from. 305 * @param callable $reducer The function to return the dataset, receives current dataset, and the current record. 306 * @param callable $export The function to export the dataset, receives the last value from $splitkey and the dataset. 307 * @return void 308 */ 309 protected static function recordset_loop_and_export(\moodle_recordset $recordset, $splitkey, $initial, 310 callable $reducer, callable $export) { 311 312 $data = $initial; 313 $lastid = null; 314 315 foreach ($recordset as $record) { 316 if ($lastid && $record->{$splitkey} != $lastid) { 317 $export($lastid, $data); 318 $data = $initial; 319 } 320 $data = $reducer($data, $record); 321 $lastid = $record->{$splitkey}; 322 } 323 $recordset->close(); 324 325 if (!empty($lastid)) { 326 $export($lastid, $data); 327 } 328 } 329 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body