Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * Privacy Subsystem implementation for auth_mnet.
  18   *
  19   * @package    auth_mnet
  20   * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  namespace auth_mnet\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_privacy\local\metadata\collection;
  29  use core_privacy\local\request\contextlist;
  30  use core_privacy\local\request\approved_contextlist;
  31  use core_privacy\local\request\transform;
  32  use core_privacy\local\request\writer;
  33  use core_privacy\local\request\userlist;
  34  use core_privacy\local\request\approved_userlist;
  35  
  36  /**
  37   * Privacy provider for the mnet authentication
  38   *
  39   * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class provider implements
  43          \core_privacy\local\metadata\provider,
  44          \core_privacy\local\request\core_userlist_provider,
  45          \core_privacy\local\request\plugin\provider {
  46      /**
  47       * Returns meta data about this system.
  48       *
  49       * @param   collection $collection The initialised item collection to add items to.
  50       * @return  collection     A listing of user data stored through this system.
  51       */
  52      public static function get_metadata(collection $collection) : collection {
  53  
  54          $sessionfields = [
  55                  'userid' => 'privacy:metadata:mnet_session:userid',
  56                  'username' => 'privacy:metadata:mnet_session:username',
  57                  'token' => 'privacy:metadata:mnet_session:token',
  58                  'mnethostid' => 'privacy:metadata:mnet_session:mnethostid',
  59                  'useragent' => 'privacy:metadata:mnet_session:useragent',
  60                  'expires' => 'privacy:metadata:mnet_session:expires'
  61          ];
  62  
  63          $collection->add_database_table('mnet_session', $sessionfields, 'privacy:metadata:mnet_session');
  64  
  65          $logfields = [
  66                  'hostid' => 'privacy:metadata:mnet_log:hostid',
  67                  'remoteid' => 'privacy:metadata:mnet_log:remoteid',
  68                  'time' => 'privacy:metadata:mnet_log:time',
  69                  'userid' => 'privacy:metadata:mnet_log:userid',
  70                  'ip' => 'privacy:metadata:mnet_log:ip',
  71                  'course' => 'privacy:metadata:mnet_log:course',
  72                  'coursename' => 'privacy:metadata:mnet_log:coursename',
  73                  'module' => 'privacy:metadata:mnet_log:module',
  74                  'cmid' => 'privacy:metadata:mnet_log:cmid',
  75                  'action' => 'privacy:metadata:mnet_log:action',
  76                  'url' => 'privacy:metadata:mnet_log:url',
  77                  'info' => 'privacy:metadata:mnet_log:info'
  78          ];
  79  
  80          $collection->add_database_table('mnet_log', $logfields, 'privacy:metadata:mnet_log');
  81  
  82          $externalfields = [
  83                  'address' => 'privacy:metadata:mnet_external:address',
  84                  'aim' => 'privacy:metadata:mnet_external:aim',
  85                  'alternatename' => 'privacy:metadata:mnet_external:alternatename',
  86                  'autosubscribe' => 'privacy:metadata:mnet_external:autosubscribe',
  87                  'calendartype' => 'privacy:metadata:mnet_external:calendartype',
  88                  'city' => 'privacy:metadata:mnet_external:city',
  89                  'country' => 'privacy:metadata:mnet_external:country',
  90                  'currentlogin' => 'privacy:metadata:mnet_external:currentlogin',
  91                  'department' => 'privacy:metadata:mnet_external:department',
  92                  'description' => 'privacy:metadata:mnet_external:description',
  93                  'email' => 'privacy:metadata:mnet_external:email',
  94                  'emailstop' => 'privacy:metadata:mnet_external:emailstop',
  95                  'firstaccess' => 'privacy:metadata:mnet_external:firstaccess',
  96                  'firstname' => 'privacy:metadata:mnet_external:firstname',
  97                  'firstnamephonetic' => 'privacy:metadata:mnet_external:firstnamephonetic',
  98                  'icq' => 'privacy:metadata:mnet_external:icq',
  99                  'id' => 'privacy:metadata:mnet_external:id',
 100                  'idnumber' => 'privacy:metadata:mnet_external:idnumber',
 101                  'imagealt' => 'privacy:metadata:mnet_external:imagealt',
 102                  'institution' => 'privacy:metadata:mnet_external:institution',
 103                  'lang' => 'privacy:metadata:mnet_external:lang',
 104                  'lastaccess' => 'privacy:metadata:mnet_external:lastaccess',
 105                  'lastlogin' => 'privacy:metadata:mnet_external:lastlogin',
 106                  'lastname' => 'privacy:metadata:mnet_external:lastname',
 107                  'lastnamephonetic' => 'privacy:metadata:mnet_external:lastnamephonetic',
 108                  'maildigest' => 'privacy:metadata:mnet_external:maildigest',
 109                  'maildisplay' => 'privacy:metadata:mnet_external:maildisplay',
 110                  'middlename' => 'privacy:metadata:mnet_external:middlename',
 111                  'msn' => 'privacy:metadata:mnet_external:msn',
 112                  'phone1' => 'privacy:metadata:mnet_external:phone1',
 113                  'pnone2' => 'privacy:metadata:mnet_external:phone2',
 114                  'picture' => 'privacy:metadata:mnet_external:picture',
 115                  'policyagreed' => 'privacy:metadata:mnet_external:policyagreed',
 116                  'skype' => 'privacy:metadata:mnet_external:skype',
 117                  'suspended' => 'privacy:metadata:mnet_external:suspended',
 118                  'timezone' => 'privacy:metadata:mnet_external:timezone',
 119                  'trackforums' => 'privacy:metadata:mnet_external:trackforums',
 120                  'trustbitmask' => 'privacy:metadata:mnet_external:trustbitmask',
 121                  'url' => 'privacy:metadata:mnet_external:url',
 122                  'username' => 'privacy:metadata:mnet_external:username',
 123                  'yahoo' => 'privacy:metadata:mnet_external:yahoo',
 124          ];
 125  
 126          $collection->add_external_location_link('moodle', $externalfields, 'privacy:metadata:external:moodle');
 127  
 128          $collection->add_external_location_link('mahara', $externalfields, 'privacy:metadata:external:mahara');
 129  
 130          return $collection;
 131      }
 132  
 133      /**
 134       * Get the list of contexts that contain user information for the specified user.
 135       *
 136       * @param   int $userid The user to search.
 137       * @return  contextlist   $contextlist  The list of contexts used in this plugin.
 138       */
 139      public static function get_contexts_for_userid(int $userid) : contextlist {
 140          $sql = "SELECT ctx.id
 141                    FROM {mnet_log} ml
 142                    JOIN {context} ctx ON ctx.instanceid = ml.userid AND ctx.contextlevel = :contextlevel
 143                   WHERE ml.userid = :userid";
 144          $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
 145  
 146          $contextlist = new contextlist();
 147          $contextlist->add_from_sql($sql, $params);
 148  
 149          return $contextlist;
 150      }
 151  
 152      /**
 153       * Get the list of users within a specific context.
 154       *
 155       * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
 156       */
 157      public static function get_users_in_context(userlist $userlist) {
 158          $context = $userlist->get_context();
 159  
 160          if (!$context instanceof \context_user) {
 161              return;
 162          }
 163  
 164          $sql = "SELECT userid
 165                    FROM {mnet_log}
 166                   WHERE userid = ?";
 167          $params = [$context->instanceid];
 168          $userlist->add_from_sql('userid', $sql, $params);
 169      }
 170  
 171      /**
 172       * Export all user data for the specified user, in the specified contexts, using the supplied exporter instance.
 173       *
 174       * @param   approved_contextlist $contextlist The approved contexts to export information for.
 175       */
 176      public static function export_user_data(approved_contextlist $contextlist) {
 177          global $DB;
 178  
 179          $context = \context_user::instance($contextlist->get_user()->id);
 180  
 181          $sql = "SELECT ml.id, mh.wwwroot, mh.name, ml.remoteid, ml.time, ml.userid, ml.ip, ml.course,
 182                         ml.coursename, ml.module, ml.cmid, ml.action, ml.url, ml.info
 183                    FROM {mnet_log} ml
 184                    JOIN {mnet_host} mh ON mh.id = ml.hostid
 185                   WHERE ml.userid = :userid
 186                ORDER BY mh.name, ml.coursename";
 187          $params = ['userid' => $contextlist->get_user()->id];
 188  
 189          $data = [];
 190          $lastcourseid = null;
 191  
 192          $logentries = $DB->get_recordset_sql($sql, $params);
 193          foreach ($logentries as $logentry) {
 194              $item = (object) [
 195                      'time' => transform::datetime($logentry->time),
 196                      'remoteid' => $logentry->remoteid,
 197                      'ip' => $logentry->ip,
 198                      'course' => $logentry->course,
 199                      'coursename' => format_string($logentry->coursename),
 200                      'module' => $logentry->module,
 201                      'cmid' => $logentry->cmid,
 202                      'action' => $logentry->action,
 203                      'url' => $logentry->url,
 204                      'info' => format_string($logentry->info)
 205              ];
 206  
 207              $item->externalhost =
 208                      ($logentry->name == '') ? preg_replace('#^https?://#', '', $logentry->wwwroot) :
 209                              preg_replace('#^https?://#', '', $logentry->name);
 210  
 211              if ($lastcourseid && $lastcourseid != $logentry->course) {
 212                  $path = [get_string('pluginname', 'auth_mnet'), $data[0]->externalhost, $data[0]->coursename];
 213                  writer::with_context($context)->export_data($path, (object) $data);
 214                  $data = [];
 215              }
 216  
 217              $data[] = $item;
 218              $lastcourseid = $logentry->course;
 219          }
 220          $logentries->close();
 221  
 222          $path = [get_string('pluginname', 'auth_mnet'), $item->externalhost, $item->coursename];
 223          writer::with_context($context)->export_data($path, (object) $data);
 224      }
 225  
 226      /**
 227       * Delete all personal data for all users in the specified context.
 228       *
 229       * @param context $context Context to delete data from.
 230       */
 231      public static function delete_data_for_all_users_in_context(\context $context) {
 232          global $DB;
 233  
 234          if ($context->contextlevel != CONTEXT_USER) {
 235              return;
 236          }
 237  
 238          $DB->delete_records('mnet_log', ['userid' => $context->instanceid]);
 239      }
 240  
 241      /**
 242       * Delete multiple users within a single context.
 243       *
 244       * @param approved_userlist $userlist The approved context and user information to delete information for.
 245       */
 246      public static function delete_data_for_users(approved_userlist $userlist) {
 247          global $DB;
 248  
 249          $context = $userlist->get_context();
 250  
 251          if ($context instanceof \context_user) {
 252              $DB->delete_records('mnet_log', ['userid' => $context->instanceid]);
 253          }
 254      }
 255  
 256      /**
 257       * Delete all user data for the specified user, in the specified contexts.
 258       *
 259       * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
 260       */
 261      public static function delete_data_for_user(approved_contextlist $contextlist) {
 262          global $DB;
 263  
 264          if (empty($contextlist->count())) {
 265              return;
 266          }
 267  
 268          $userid = $contextlist->get_user()->id;
 269          foreach ($contextlist->get_contexts() as $context) {
 270              if ($context->contextlevel != CONTEXT_USER) {
 271                  continue;
 272              }
 273              if ($context->instanceid == $userid) {
 274                  // Because we only use user contexts the instance ID is the user ID.
 275                  $DB->delete_records('mnet_log', ['userid' => $context->instanceid]);
 276              }
 277          }
 278      }
 279  }