|
| 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 | + |
| 33 | +/** |
| 34 | + * Privacy class for requesting user data. |
| 35 | + * |
| 36 | + * @package core_userkey |
| 37 | + * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk> |
| 38 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 39 | + */ |
| 40 | +class provider implements |
| 41 | + \core_privacy\local\metadata\provider, |
| 42 | + |
| 43 | + \core_privacy\local\request\subsystem\plugin_provider { |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns meta data about this system. |
| 47 | + * |
| 48 | + * @param collection $collection The initialised collection to add items to. |
| 49 | + * @return collection A listing of user data stored through this system. |
| 50 | + */ |
| 51 | + public static function get_metadata(collection $collection) : collection { |
| 52 | + $collection->add_database_table('user_private_key', [ |
| 53 | + 'script' => 'privacy:metadata:user_private_key:script', |
| 54 | + 'value' => 'privacy:metadata:user_private_key:value', |
| 55 | + 'userid' => 'privacy:metadata:user_private_key:userid', |
| 56 | + 'instance' => 'privacy:metadata:user_private_key:instance', |
| 57 | + 'iprestriction' => 'privacy:metadata:user_private_key:iprestriction', |
| 58 | + 'validuntil' => 'privacy:metadata:user_private_key:validuntil', |
| 59 | + 'timecreated' => 'privacy:metadata:user_private_key:timecreated', |
| 60 | + ], 'privacy:metadata:user_private_key'); |
| 61 | + |
| 62 | + return $collection; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Exports the data relating to user keys for the specified scripts and instance, within the specified |
| 67 | + * context/subcontext. |
| 68 | + * |
| 69 | + * @param \context $context Context owner of the data. |
| 70 | + * @param array $subcontext Context owner of the data. |
| 71 | + * @param string $script The owner of the data (usually a component name). |
| 72 | + * @param int $instance The instance owner of the data. |
| 73 | + */ |
| 74 | + public static function export_userkeys(\context $context, array $subcontext, $script, $instance = null) { |
| 75 | + global $DB, $USER; |
| 76 | + |
| 77 | + $searchparams = [ |
| 78 | + 'script' => $script, |
| 79 | + 'userid' => $USER->id, |
| 80 | + ]; |
| 81 | + |
| 82 | + if (null !== $instance) { |
| 83 | + $searchparams['instance'] = $instance; |
| 84 | + } |
| 85 | + |
| 86 | + $keys = $DB->get_recordset('user_private_key', $searchparams); |
| 87 | + $keydata = []; |
| 88 | + foreach ($keys as $key) { |
| 89 | + $keydata[] = (object) [ |
| 90 | + 'script' => $key->script, |
| 91 | + 'instance' => $key->instance, |
| 92 | + 'iprestriction' => $key->iprestriction, |
| 93 | + 'validuntil' => transform::datetime($key->validuntil), |
| 94 | + 'timecreated' => transform::datetime($key->timecreated), |
| 95 | + ]; |
| 96 | + } |
| 97 | + $keys->close(); |
| 98 | + |
| 99 | + if (!empty($keydata)) { |
| 100 | + $data = (object) [ |
| 101 | + 'keys' => $keydata, |
| 102 | + ]; |
| 103 | + |
| 104 | + writer::with_context($context)->export_related_data($subcontext, 'userkeys', $data); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Deletes all userkeys for a script. |
| 110 | + * |
| 111 | + * @param string $script The owner of the data (usually a component name). |
| 112 | + * @param int $userid The owner of the data. |
| 113 | + * @param int $instance The instance owner of the data. |
| 114 | + */ |
| 115 | + public static function delete_userkeys($script, $userid = null, $instance = null) { |
| 116 | + global $DB; |
| 117 | + |
| 118 | + $searchparams = [ |
| 119 | + 'script' => $script, |
| 120 | + ]; |
| 121 | + |
| 122 | + if (null !== $userid) { |
| 123 | + $searchparams['userid'] = $userid; |
| 124 | + } |
| 125 | + |
| 126 | + if (null !== $instance) { |
| 127 | + $searchparams['instance'] = $instance; |
| 128 | + } |
| 129 | + |
| 130 | + $DB->delete_records('user_private_key', $searchparams); |
| 131 | + } |
| 132 | +} |
0 commit comments