PageRenderTime 25ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/repository/onedrive/classes/privacy/provider.php

https://bitbucket.org/moodle/moodle
PHP | 259 lines | 146 code | 37 blank | 76 comment | 10 complexity | 395b2f46b6df7c0bc2a5a497a54de021 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Privacy Subsystem implementation for repository_onedrive.
  18. *
  19. * @package repository_onedrive
  20. * @copyright 2018 Zig Tan <zig@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace repository_onedrive\privacy;
  24. use core_privacy\local\metadata\collection;
  25. use core_privacy\local\request\approved_contextlist;
  26. use core_privacy\local\request\approved_userlist;
  27. use core_privacy\local\request\context;
  28. use core_privacy\local\request\contextlist;
  29. use core_privacy\local\request\transform;
  30. use core_privacy\local\request\userlist;
  31. use \core_privacy\local\request\writer;
  32. defined('MOODLE_INTERNAL') || die();
  33. /**
  34. * Privacy Subsystem for repository_onedrive implementing metadata and plugin providers.
  35. *
  36. * @copyright 2018 Zig Tan <zig@moodle.com>
  37. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38. */
  39. class provider implements
  40. \core_privacy\local\metadata\provider,
  41. \core_privacy\local\request\core_userlist_provider,
  42. \core_privacy\local\request\plugin\provider {
  43. /**
  44. * Returns meta data about this system.
  45. *
  46. * @param collection $collection The initialised collection to add items to.
  47. * @return collection A listing of user data stored through this system.
  48. */
  49. public static function get_metadata(collection $collection) : collection {
  50. $collection->add_external_location_link(
  51. 'onedrive.live.com',
  52. [
  53. 'searchtext' => 'privacy:metadata:repository_onedrive:searchtext'
  54. ],
  55. 'privacy:metadata:repository_onedrive'
  56. );
  57. // The repository_onedrive has a 'repository_onedrive_access' table that contains user data.
  58. $collection->add_database_table(
  59. 'repository_onedrive_access',
  60. [
  61. 'itemid' => 'privacy:metadata:repository_onedrive:repository_onedrive_access:itemid',
  62. 'permissionid' => 'privacy:metadata:repository_onedrive:repository_onedrive_access:permissionid',
  63. 'timecreated' => 'privacy:metadata:repository_onedrive:repository_onedrive_access:timecreated',
  64. 'timemodified' => 'privacy:metadata:repository_onedrive:repository_onedrive_access:timemodified',
  65. 'usermodified' => 'privacy:metadata:repository_onedrive:repository_onedrive_access:usermodified'
  66. ],
  67. 'privacy:metadata:repository_onedrive'
  68. );
  69. return $collection;
  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 contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
  76. */
  77. public static function get_contexts_for_userid(int $userid) : contextlist {
  78. $contextlist = new contextlist();
  79. // The data is associated at the user context level, so retrieve the user's context id.
  80. $sql = "SELECT c.id
  81. FROM {repository_onedrive_access} roa
  82. JOIN {context} c ON c.instanceid = roa.usermodified AND c.contextlevel = :contextuser
  83. WHERE roa.usermodified = :userid
  84. GROUP BY c.id";
  85. $params = [
  86. 'contextuser' => CONTEXT_USER,
  87. 'userid' => $userid
  88. ];
  89. $contextlist->add_from_sql($sql, $params);
  90. return $contextlist;
  91. }
  92. /**
  93. * Get the list of users who have data within a context.
  94. *
  95. * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
  96. */
  97. public static function get_users_in_context(userlist $userlist) {
  98. $context = $userlist->get_context();
  99. if (!$context instanceof \context_user) {
  100. return;
  101. }
  102. // The data is associated at the user context level, so retrieve the user's context id.
  103. $sql = "SELECT usermodified AS userid
  104. FROM {repository_onedrive_access}
  105. WHERE usermodified = ?";
  106. $params = [$context->instanceid];
  107. $userlist->add_from_sql('userid', $sql, $params);
  108. }
  109. /**
  110. * Export all user data for the specified user, in the specified contexts.
  111. *
  112. * @param approved_contextlist $contextlist The approved contexts to export information for.
  113. */
  114. public static function export_user_data(approved_contextlist $contextlist) {
  115. global $DB;
  116. // If the user has data, then only the User context should be present so get the first context.
  117. $contexts = $contextlist->get_contexts();
  118. if (count($contexts) == 0) {
  119. return;
  120. }
  121. $context = reset($contexts);
  122. // Sanity check that context is at the User context level, then get the userid.
  123. if ($context->contextlevel !== CONTEXT_USER) {
  124. return;
  125. }
  126. $userid = $context->instanceid;
  127. $sql = "SELECT roa.id as id,
  128. roa.itemid as itemid,
  129. roa.permissionid as permissionid,
  130. roa.timecreated as timecreated,
  131. roa.timemodified as timemodified
  132. FROM {repository_onedrive_access} roa
  133. WHERE roa.usermodified = :userid";
  134. $params = [
  135. 'userid' => $userid
  136. ];
  137. $onedriveaccesses = $DB->get_records_sql($sql, $params);
  138. $index = 0;
  139. foreach ($onedriveaccesses as $onedriveaccess) {
  140. // Data export is organised in: {User Context}/Repository plug-ins/{Plugin Name}/Access/{index}/data.json.
  141. $index++;
  142. $subcontext = [
  143. get_string('plugin', 'core_repository'),
  144. get_string('pluginname', 'repository_onedrive'),
  145. get_string('access', 'repository_onedrive'),
  146. $index
  147. ];
  148. $data = (object) [
  149. 'itemid' => $onedriveaccess->itemid,
  150. 'permissionid' => $onedriveaccess->permissionid,
  151. 'timecreated' => transform::datetime($onedriveaccess->timecreated),
  152. 'timemodified' => transform::datetime($onedriveaccess->timemodified)
  153. ];
  154. writer::with_context($context)->export_data($subcontext, $data);
  155. }
  156. }
  157. /**
  158. * Delete all data for all users in the specified context.
  159. *
  160. * @param context $context The specific context to delete data for.
  161. */
  162. public static function delete_data_for_all_users_in_context(\context $context) {
  163. global $DB;
  164. // Sanity check that context is at the User context level, then get the userid.
  165. if ($context->contextlevel !== CONTEXT_USER) {
  166. return;
  167. }
  168. $userid = $context->instanceid;
  169. $DB->delete_records('repository_onedrive_access', ['usermodified' => $userid]);
  170. }
  171. /**
  172. * Delete all user data for the specified user, in the specified contexts.
  173. *
  174. * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
  175. */
  176. public static function delete_data_for_user(approved_contextlist $contextlist) {
  177. global $DB;
  178. // If the user has data, then only the User context should be present so get the first context.
  179. $contexts = $contextlist->get_contexts();
  180. if (count($contexts) == 0) {
  181. return;
  182. }
  183. $context = reset($contexts);
  184. // Sanity check that context is at the User context level, then get the userid.
  185. if ($context->contextlevel !== CONTEXT_USER) {
  186. return;
  187. }
  188. $userid = $context->instanceid;
  189. $DB->delete_records('repository_onedrive_access', ['usermodified' => $userid]);
  190. }
  191. /**
  192. * Delete multiple users within a single context.
  193. *
  194. * @param approved_userlist $userlist The approved context and user information to delete information for.
  195. */
  196. public static function delete_data_for_users(approved_userlist $userlist) {
  197. global $DB;
  198. $context = $userlist->get_context();
  199. // Sanity check that context is at the User context level, then get the userid.
  200. if ($context->contextlevel !== CONTEXT_USER) {
  201. return;
  202. }
  203. $userids = $userlist->get_userids();
  204. list($insql, $inparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
  205. $params = [
  206. 'contextid' => $context->id,
  207. 'contextuser' => CONTEXT_USER,
  208. ];
  209. $params = array_merge($params, $inparams);
  210. // Fetch the repository_onedrive_access IDs in the context for approved users.
  211. $sql = "SELECT roa.id
  212. FROM {repository_onedrive_access} roa
  213. JOIN {context} c ON c.instanceid = roa.usermodified
  214. AND c.contextlevel = :contextuser
  215. AND c.id = :contextid
  216. WHERE roa.usermodified {$insql}";
  217. $accessids = $DB->get_fieldset_sql($sql, $params);
  218. // Delete the relevant repository_onedrive_access data.
  219. list($insql, $params) = $DB->get_in_or_equal($accessids, SQL_PARAMS_NAMED);
  220. $DB->delete_records_select('repository_onedrive_access', "id {$insql}", $params);
  221. }
  222. }