Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 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   * Class for loading/storing oauth2 endpoints from the DB.
  19   *
  20   * @package    core
  21   * @copyright  2017 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\oauth2;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core\persistent;
  29  use lang_string;
  30  /**
  31   * Class for loading/storing oauth2 user field mappings from the DB
  32   *
  33   * @copyright  2017 Damyon Wiese
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class user_field_mapping extends persistent {
  37  
  38      const TABLE = 'oauth2_user_field_mapping';
  39  
  40      /**
  41       * Return the list of valid internal user fields.
  42       *
  43       * @return array
  44       */
  45      private static function get_user_fields() {
  46          global $CFG;
  47          require_once($CFG->dirroot . '/user/profile/lib.php');
  48          return array_merge(\core_user::AUTHSYNCFIELDS, ['picture', 'username'], get_profile_field_names());
  49      }
  50  
  51      /**
  52       * Return the definition of the properties of this model.
  53       *
  54       * @return array
  55       */
  56      protected static function define_properties() {
  57          return array(
  58              'issuerid' => array(
  59                  'type' => PARAM_INT
  60              ),
  61              'externalfield' => array(
  62                  'type' => PARAM_RAW_TRIMMED,
  63              ),
  64              'internalfield' => array(
  65                  'type' => PARAM_ALPHANUMEXT,
  66                  'choices' => self::get_user_fields()
  67              )
  68          );
  69      }
  70  
  71      /**
  72       * Return the list of internal fields
  73       * in a format they can be used for choices in a select menu
  74       * @return array
  75       */
  76      public function get_internalfield_list() {
  77          global $CFG;
  78          require_once($CFG->dirroot . '/user/profile/lib.php');
  79          $userfields = array_merge(\core_user::AUTHSYNCFIELDS, ['picture', 'username']);
  80          $internalfields = array_combine($userfields, $userfields);
  81          return array_merge(['' => $internalfields], get_profile_field_list());
  82      }
  83  
  84      /**
  85       * Return the list of internal fields with flat array
  86       *
  87       * Profile fields element has its array based on profile category.
  88       * These elements need to be turned flat to make it easier to read.
  89       *
  90       * @return array
  91       */
  92      public function get_internalfields() {
  93          $userfieldlist = $this->get_internalfield_list();
  94          $userfields = [];
  95          array_walk_recursive($userfieldlist,
  96              function($value, $key) use (&$userfields) {
  97                  $userfields[] = $key;
  98              }
  99          );
 100          return $userfields;
 101      }
 102  
 103      /**
 104       * Ensures that no HTML is saved to externalfield field
 105       * but preserves all special characters that can be a part of the claim
 106       * @return boolean true if validation is successful, string error if externalfield is not validated
 107       */
 108      protected function validate_externalfield($value){
 109          // This parameter type is set to PARAM_RAW_TRIMMED and HTML check is done here.
 110          if (clean_param($value, PARAM_NOTAGS) !== $value){
 111              return new lang_string('userfieldexternalfield_error', 'tool_oauth2');
 112          }
 113          return true;
 114      }
 115  
 116  }