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 25 namespace core_userkey\privacy; 26 27 defined('MOODLE_INTERNAL') || die(); 28 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; 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 */ 41 class provider implements 42 \core_privacy\local\metadata\provider, 43 44 \core_privacy\local\request\subsystem\plugin_provider, 45 \core_privacy\local\request\shared_userlist_provider 46 { 47 48 /** 49 * Returns meta data about this system. 50 * 51 * @param collection $collection The initialised collection to add items to. 52 * @return collection A listing of user data stored through this system. 53 */ 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'); 64 65 return $collection; 66 } 67 68 /** 69 * Get the list of users within a specific context for this system. 70 * 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. 75 */ 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; 80 } 81 82 $params = [ 83 'userid' => $context->instanceid, 84 'script' => $script 85 ]; 86 87 $whereinstance = ''; 88 89 if (!empty($instance)) { 90 $params['instance'] = $instance; 91 $whereinstance = ' AND k.instance = :instance'; 92 } 93 94 $sql = "SELECT k.userid 95 FROM {user_private_key} k 96 WHERE k.script = :script 97 AND k.userid = :userid 98 {$whereinstance}"; 99 100 $userlist->add_from_sql('userid', $sql, $params); 101 } 102 103 /** 104 * Exports the data relating to user keys for the specified scripts and instance, within the specified 105 * context/subcontext. 106 * 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. 111 */ 112 public static function export_userkeys(\context $context, array $subcontext, string $script, $instance = null) { 113 global $DB, $USER; 114 115 $searchparams = [ 116 'script' => $script, 117 'userid' => $USER->id, 118 ]; 119 120 if (null !== $instance) { 121 $searchparams['instance'] = $instance; 122 } 123 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), 133 ]; 134 } 135 $keys->close(); 136 137 if (!empty($keydata)) { 138 $data = (object) [ 139 'keys' => $keydata, 140 ]; 141 142 writer::with_context($context)->export_related_data($subcontext, 'userkeys', $data); 143 } 144 } 145 146 /** 147 * Deletes all userkeys for a script. 148 * 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. 152 */ 153 public static function delete_userkeys(string $script, $userid = null, $instance = null) { 154 global $DB; 155 156 $searchparams = [ 157 'script' => $script, 158 ]; 159 160 if (null !== $userid) { 161 $searchparams['userid'] = $userid; 162 } 163 164 if (null !== $instance) { 165 $searchparams['instance'] = $instance; 166 } 167 168 $DB->delete_records('user_private_key', $searchparams); 169 } 170 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body