Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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  /**
  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          $collection->add_subsystem_link('core_userkey', [], 'privacy:metadata:core_userkey');
  77  
  78          return $collection;
  79      }
  80  
  81      /**
  82       * Get the list of contexts that contain user information for the specified user.
  83       *
  84       * This is currently just the user context.
  85       *
  86       * @param int $userid The user to search.
  87       * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
  88       */
  89      public static function get_contexts_for_userid(int $userid) : contextlist {
  90          $sql = "SELECT ctx.id
  91                    FROM {user_private_key} k
  92                    JOIN {user} u ON k.userid = u.id
  93                    JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel
  94                   WHERE k.userid = :userid AND k.script = :script";
  95          $params = [
  96              'userid' => $userid,
  97              'contextlevel' => CONTEXT_USER,
  98              'script' => 'core_files',
  99          ];
 100          $contextlist = new contextlist();
 101          $contextlist->add_from_sql($sql, $params);
 102  
 103          return $contextlist;
 104      }
 105  
 106      /**
 107       * Get the list of users within a specific context.
 108       *
 109       * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
 110       */
 111      public static function get_users_in_context(userlist $userlist) {
 112          $context = $userlist->get_context();
 113  
 114          if (!$context instanceof \context_user) {
 115              return;
 116          }
 117  
 118          \core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'core_files');
 119      }
 120  
 121      /**
 122       * Export all user data for the specified user, in the specified contexts.
 123       *
 124       * @param approved_contextlist $contextlist The approved contexts to export information for.
 125       */
 126      public static function export_user_data(approved_contextlist $contextlist) {
 127          // If the user has data, then only the CONTEXT_USER should be present so get the first context.
 128          $contexts = $contextlist->get_contexts();
 129          if (count($contexts) == 0) {
 130              return;
 131          }
 132  
 133          // Sanity check that context is at the user context level, then get the userid.
 134          $context = reset($contexts);
 135          if ($context->contextlevel !== CONTEXT_USER) {
 136              return;
 137          }
 138  
 139          // Export associated userkeys.
 140          $subcontext = [
 141              get_string('files'),
 142          ];
 143          \core_userkey\privacy\provider::export_userkeys($context, $subcontext, 'core_files');
 144      }
 145  
 146      /**
 147       * Delete all use data which matches the specified deletion_criteria.
 148       *
 149       * @param context $context A user context.
 150       */
 151      public static function delete_data_for_all_users_in_context(\context $context) {
 152          // Sanity check that context is at the user context level, then get the userid.
 153          if ($context->contextlevel !== CONTEXT_USER) {
 154              return;
 155          }
 156  
 157          // Delete all the userkeys.
 158          \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
 159      }
 160  
 161      /**
 162       * Delete multiple users within a single context.
 163       *
 164       * @param approved_userlist $userlist The approved context and user information to delete information for.
 165       */
 166      public static function delete_data_for_users(approved_userlist $userlist) {
 167          $context = $userlist->get_context();
 168  
 169          if ($context instanceof \context_user) {
 170              \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
 171          }
 172      }
 173  
 174      /**
 175       * Delete all user data for the specified user, in the specified contexts.
 176       *
 177       * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
 178       */
 179      public static function delete_data_for_user(approved_contextlist $contextlist) {
 180          // If the user has data, then only the user context should be present so get the first context.
 181          $contexts = $contextlist->get_contexts();
 182          if (count($contexts) == 0) {
 183              return;
 184          }
 185  
 186          // Sanity check that context is at the user context level, then get the userid.
 187          $context = reset($contexts);
 188          if ($context->contextlevel !== CONTEXT_USER) {
 189              return;
 190          }
 191  
 192          // Delete all the userkeys for core_files..
 193          \core_userkey\privacy\provider::delete_userkeys('core_files', $context->instanceid);
 194      }
 195  }