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.
   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_auth
  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_auth\privacy;
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use context;
  30  use core_privacy\local\metadata\collection;
  31  use core_privacy\local\request\transform;
  32  use core_privacy\local\request\writer;
  33  
  34  /**
  35   * Data provider class.
  36   *
  37   * @package    core_auth
  38   * @copyright  2018 Frédéric Massart
  39   * @author     Frédéric Massart <fred@branchup.tech>
  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\user_preference_provider {
  45  
  46      /**
  47       * Returns metadata.
  48       *
  49       * @param collection $collection The initialised 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          $collection->add_user_preference('auth_forcepasswordchange', 'privacy:metadata:userpref:forcepasswordchange');
  55          $collection->add_user_preference('create_password', 'privacy:metadata:userpref:createpassword');
  56          $collection->add_user_preference('login_failed_count', 'privacy:metadata:userpref:loginfailedcount');
  57          $collection->add_user_preference('login_failed_count_since_success',
  58              'privacy:metadata:userpref:loginfailedcountsincesuccess');
  59          $collection->add_user_preference('login_failed_last', 'privacy:metadata:userpref:loginfailedlast');
  60          $collection->add_user_preference('login_lockout', 'privacy:metadata:userpref:loginlockout');
  61          $collection->add_user_preference('login_lockout_ignored', 'privacy:metadata:userpref:loginlockoutignored');
  62          $collection->add_user_preference('login_lockout_secret', 'privacy:metadata:userpref:loginlockoutsecret');
  63  
  64          return $collection;
  65      }
  66  
  67      /**
  68       * Export all user preferences for the plugin.
  69       *
  70       * @param int $userid The userid of the user whose data is to be exported.
  71       */
  72      public static function export_user_preferences(int $userid) {
  73  
  74          $yesno = function($v) {
  75              return transform::yesno($v);
  76          };
  77          $datetime = function($v) {
  78              return $v ? transform::datetime($v) : null;
  79          };
  80  
  81          $prefs = [
  82              ['auth_forcepasswordchange', 'forcepasswordchange', $yesno],
  83              ['create_password', 'createpassword', $yesno],
  84              ['login_failed_count', 'loginfailedcount', null],
  85              ['login_failed_count_since_success', 'loginfailedcountsincesuccess', null],
  86              ['login_failed_last', 'loginfailedlast', $datetime],
  87              ['login_lockout', 'loginlockout', $datetime],
  88              ['login_lockout_ignored', 'loginlockoutignored', $yesno],
  89              ['login_lockout_secret', 'loginlockoutsecret', null],
  90          ];
  91  
  92          foreach ($prefs as $prefdata) {
  93              list($prefname, $langkey, $transformer) = $prefdata;
  94              $value = get_user_preferences($prefname, null, $userid);
  95              if ($value === null) {
  96                  continue;
  97              }
  98              writer::export_user_preference('core_auth', $prefname, $transformer ? $transformer($value) : $value,
  99                  get_string("privacy:metadata:userpref:{$langkey}", 'core_auth'));
 100          }
 101      }
 102  
 103  }