Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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   * Data provider.
  19   *
  20   * @package    core_files
  21   * @copyright  2018 Frédéric Massart
  22   * @author     Frédéric Massart <fred@branchup.tech>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_files\privacy;
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_privacy\local\metadata\collection;
  30  use core_privacy\local\request\contextlist;
  31  use core_privacy\local\request\approved_contextlist;
  32  use core_privacy\local\request\userlist;
  33  use core_privacy\local\request\approved_userlist;
  34  
  35  /**
  36   * Data provider class.
  37   *
  38   * This only describes the files table, all components must handle the file exporting
  39   * and deletion themselves.
  40   *
  41   * @package    core_files
  42   * @copyright  2018 Frédéric Massart
  43   * @author     Frédéric Massart <fred@branchup.tech>
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class provider implements
  47          \core_privacy\local\metadata\provider,
  48          \core_privacy\local\request\subsystem\plugin_provider,
  49          \core_privacy\local\request\core_userlist_provider,
  50          // We store a userkey for token-based file access.
  51          \core_privacy\local\request\subsystem\provider,
  52          \core_privacy\local\request\shared_userlist_provider {
  53  
  54      /**
  55       * Returns metadata.
  56       *
  57       * @param collection $collection The initialised collection to add items to.
  58       * @return collection A listing of user data stored through this system.
  59       */
  60      public static function get_metadata(collection $collection) : collection {
  61  
  62          $collection->add_database_table('files', [
  63              'contenthash' => 'privacy:metadata:files:contenthash',
  64              'filepath' => 'privacy:metadata:files:filepath',
  65              'filename' => 'privacy:metadata:files:filename',
  66              'userid' => 'privacy:metadata:files:userid',
  67              'filesize' => 'privacy:metadata:files:filesize',
  68              'mimetype' => 'privacy:metadata:files:mimetype',
  69              'source' => 'privacy:metadata:files:source',
  70              'author' => 'privacy:metadata:files:author',
  71              'license' => 'privacy:metadata:files:license',
  72              'timecreated' => 'privacy:metadata:files:timecreated',
  73              'timemodified' => 'privacy:metadata:files:timemodified',
  74          ], 'privacy:metadata:files');
  75  
  76          // Regarding this block, we are unable to export or purge this data, as
  77          // it would damage the file conversion data across the whole site.
  78          $collection->add_database_table('file_conversion', [
  79              'usermodified' => 'privacy:metadata:file_conversion:usermodified',
  80          ], 'privacy:metadata:file_conversions');
  81  
  82          $collection->add_subsystem_link('core_userkey', [], 'privacy:metadata:core_userkey');
  83  
  84          return $collection;
  85      }
  86  
  87      /**
  88       * Get the list of contexts that contain user information for the specified user.
  89       *
  90       * This is currently just the user context.
  91       *
  92       * @param int $userid The user to search.
  93       * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
  94       */
  95      public static function get_contexts_for_userid(int $userid) : contextlist {
  96          $sql = "SELECT ctx.id
  97                    FROM {user_private_key} k
  98                    JOIN {user} u ON k.userid = u.id
  99                    JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel
 100                   WHERE k.userid = :userid AND k.script = :script";
 101          $params = [
 102              'userid' => $userid,
 103              'contextlevel' => CONTEXT_USER,
 104              'script' => 'core_files',
 105          ];
 106          $contextlist = new contextlist();
 107          $contextlist->add_from_sql($sql, $params);
 108  
 109          return $contextlist;
 110      }
 111  
 112      /**
 113       * Get the list of users within a specific context.
 114       *
 115       * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
 116       */
 117      public static function get_users_in_context(userlist $userlist) {
 118          $context = $userlist->get_context();
 119  
 120          if (!$context instanceof \context_user) {
 121              return;
 122          }
 123  
 124          \core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'core_files');
 125      }
 126  
 127      /**
 128       * Export all user data for the specified user, in the specified contexts.
 129       *
 130       * @param approved_contextlist $contextlist The approved contexts to export information for.
 131       */
 132      public static function export_user_data(approved_contextlist $contextlist) {
 133          // If the user has data, then only the CONTEXT_USER should be present so get the first context.
 134          $contexts = $contextlist->get_contexts();
 135          if (count($contexts) == 0) {
 136              return;
 137          }
 138  
 139          // Sanity check that context is at the user context level, then get the userid.
 140          $context = reset($contexts);
 141          if ($context->contextlevel !== CONTEXT_USER) {
 142              return;
 143          }
 144  
 145          // Export associated userkeys.
 146          $subcontext = [
 147              get_string('files'),
 148          ];
 149          \core_userkey\privacy\provider::export_userkeys($context, $subcontext, 'core_files');
 150      }
 151  
 152      /**
 153       * Delete all use data which matches the specified deletion_criteria.
 154       *
 155       * @param context $context A user context.
 156       */
 157      public static function delete_data_for_all_users_in_context(\context $context) {
 158          // Sanity check that context is at the user context level, then get the userid.
 159          if ($context->contextlevel !== CONTEXT_USER) {
 160              return;
 161          }
 162  
 163          // Delete all the userkeys.
 164          \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
 165      }
 166  
 167      /**
 168       * Delete multiple users within a single context.
 169       *
 170       * @param approved_userlist $userlist The approved context and user information to delete information for.
 171       */
 172      public static function delete_data_for_users(approved_userlist $userlist) {
 173          $context = $userlist->get_context();
 174  
 175          if ($context instanceof \context_user) {
 176              \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
 177          }
 178      }
 179  
 180      /**
 181       * Delete all user data for the specified user, in the specified contexts.
 182       *
 183       * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
 184       */
 185      public static function delete_data_for_user(approved_contextlist $contextlist) {
 186          // If the user has data, then only the user context should be present so get the first context.
 187          $contexts = $contextlist->get_contexts();
 188          if (count($contexts) == 0) {
 189              return;
 190          }
 191  
 192          // Sanity check that context is at the user context level, then get the userid.
 193          $context = reset($contexts);
 194          if ($context->contextlevel !== CONTEXT_USER) {
 195              return;
 196          }
 197  
 198          // Delete all the userkeys for core_files..
 199          \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
 200      }
 201  }