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]

   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   * OAuth 2 Endpoint Configuration page.
  19   *
  20   * @package    tool_oauth2
  21   * @copyright  2017 Damyon Wiese <damyon@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php');
  26  require_once($CFG->libdir.'/adminlib.php');
  27  require_once($CFG->libdir.'/tablelib.php');
  28  
  29  $PAGE->set_url('/admin/tool/oauth2/userfieldmappings.php', ['issuerid' => required_param('issuerid', PARAM_INT)]);
  30  $PAGE->set_context(context_system::instance());
  31  $PAGE->set_pagelayout('admin');
  32  $strheading = get_string('pluginname', 'tool_oauth2');
  33  $PAGE->set_title($strheading);
  34  $PAGE->set_heading($strheading);
  35  
  36  require_admin();
  37  
  38  $renderer = $PAGE->get_renderer('tool_oauth2');
  39  
  40  $action = optional_param('action', '', PARAM_ALPHAEXT);
  41  $issuerid = required_param('issuerid', PARAM_INT);
  42  $userfieldmappingid = optional_param('userfieldmappingid', '', PARAM_INT);
  43  $userfieldmapping = null;
  44  $mform = null;
  45  
  46  $issuer = \core\oauth2\api::get_issuer($issuerid);
  47  if (!$issuer) {
  48      throw new \moodle_exception('invaliddata');
  49  }
  50  $PAGE->navbar->override_active_url(new moodle_url('/admin/tool/oauth2/issuers.php'), true);
  51  
  52  if (!empty($userfieldmappingid)) {
  53      $userfieldmapping = \core\oauth2\api::get_user_field_mapping($userfieldmappingid);
  54  }
  55  
  56  if ($action == 'edit') {
  57      if ($userfieldmapping) {
  58          $PAGE->navbar->add(get_string('edituserfieldmapping', 'tool_oauth2', s($issuer->get('name'))));
  59      } else {
  60          $PAGE->navbar->add(get_string('createnewuserfieldmapping', 'tool_oauth2', s($issuer->get('name'))));
  61      }
  62  
  63      $mform = new \tool_oauth2\form\user_field_mapping(null, ['persistent' => $userfieldmapping, 'issuerid' => $issuerid]);
  64  }
  65  
  66  if ($mform && $mform->is_cancelled()) {
  67      redirect(new moodle_url('/admin/tool/oauth2/userfieldmappings.php', ['issuerid' => $issuerid]));
  68  } else if ($action == 'edit') {
  69  
  70      if ($data = $mform->get_data()) {
  71  
  72          try {
  73              if (!empty($data->id)) {
  74                  core\oauth2\api::update_user_field_mapping($data);
  75              } else {
  76                  core\oauth2\api::create_user_field_mapping($data);
  77              }
  78              redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
  79          } catch (Exception $e) {
  80              redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
  81          }
  82      } else {
  83          echo $OUTPUT->header();
  84          if ($issuer) {
  85              echo $OUTPUT->heading(get_string('edituserfieldmapping', 'tool_oauth2', s($issuer->get('name'))));
  86          } else {
  87              echo $OUTPUT->heading(get_string('createnewuserfieldmapping', 'tool_oauth2', s($issuer->get('name'))));
  88          }
  89          $mform->display();
  90          echo $OUTPUT->footer();
  91      }
  92  
  93  } else if ($action == 'delete') {
  94  
  95      if (!optional_param('confirm', false, PARAM_BOOL)) {
  96          $continueparams = [
  97              'action' => 'delete',
  98              'issuerid' => $issuerid,
  99              'userfieldmappingid' => $userfieldmappingid,
 100              'sesskey' => sesskey(),
 101              'confirm' => true
 102          ];
 103          $continueurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $continueparams);
 104          $cancelurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php');
 105          echo $OUTPUT->header();
 106          $str = get_string('deleteuserfieldmappingconfirm', 'tool_oauth2', s($issuer->get('name')));
 107          echo $OUTPUT->confirm($str, $continueurl, $cancelurl);
 108          echo $OUTPUT->footer();
 109      } else {
 110          require_sesskey();
 111          core\oauth2\api::delete_user_field_mapping($userfieldmappingid);
 112          redirect($PAGE->url, get_string('userfieldmappingdeleted', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
 113      }
 114  
 115  } else {
 116      echo $OUTPUT->header();
 117      echo $OUTPUT->heading(get_string('userfieldmappingsforissuer', 'tool_oauth2', s($issuer->get('name'))));
 118      $userfieldmappings = core\oauth2\api::get_user_field_mappings($issuer);
 119      echo $renderer->user_field_mappings_table($userfieldmappings, $issuerid);
 120  
 121      $addurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', ['action' => 'edit', 'issuerid' => $issuerid]);
 122      echo $renderer->single_button($addurl, get_string('createnewuserfieldmapping', 'tool_oauth2', s($issuer->get('name'))));
 123      echo $OUTPUT->footer();
 124  }