Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 401] [Versions 310 and 402] [Versions 310 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   * OAuth 2 Endpoing 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/endpoints.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  $endpointid = optional_param('endpointid', '', PARAM_INT);
  43  $endpoint = null;
  44  $mform = null;
  45  
  46  $issuer = \core\oauth2\api::get_issuer($issuerid);
  47  if (!$issuer) {
  48      print_error('invaliddata');
  49  }
  50  $PAGE->navbar->override_active_url(new moodle_url('/admin/tool/oauth2/issuers.php'), true);
  51  
  52  if (!empty($endpointid)) {
  53      $endpoint = \core\oauth2\api::get_endpoint($endpointid);
  54  }
  55  
  56  if ($action == 'edit') {
  57      if ($endpoint) {
  58          $strparams = [ 'issuer' => s($issuer->get('name')), 'endpoint' => s($endpoint->get('name')) ];
  59          $PAGE->navbar->add(get_string('editendpoint', 'tool_oauth2', $strparams));
  60      } else {
  61          $PAGE->navbar->add(get_string('createnewendpoint', 'tool_oauth2', s($issuer->get('name'))));
  62      }
  63  
  64      $mform = new \tool_oauth2\form\endpoint(null, ['persistent' => $endpoint, 'issuerid' => $issuerid]);
  65  }
  66  
  67  if ($mform && $mform->is_cancelled()) {
  68      redirect(new moodle_url('/admin/tool/oauth2/endpoints.php', ['issuerid' => $issuerid]));
  69  } else if ($action == 'edit') {
  70  
  71      if ($data = $mform->get_data()) {
  72  
  73          try {
  74              if (!empty($data->id)) {
  75                  core\oauth2\api::update_endpoint($data);
  76              } else {
  77                  core\oauth2\api::create_endpoint($data);
  78              }
  79              redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
  80          } catch (Exception $e) {
  81              redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
  82          }
  83      } else {
  84          echo $OUTPUT->header();
  85          if ($endpoint) {
  86              $strparams = [ 'issuer' => s($issuer->get('name')), 'endpoint' => s($endpoint->get('name')) ];
  87              echo $OUTPUT->heading(get_string('editendpoint', 'tool_oauth2', $strparams));
  88          } else {
  89              echo $OUTPUT->heading(get_string('createnewendpoint', 'tool_oauth2', s($issuer->get('name'))));
  90          }
  91          $mform->display();
  92          echo $OUTPUT->footer();
  93      }
  94  
  95  } else if ($action == 'delete') {
  96  
  97      if (!optional_param('confirm', false, PARAM_BOOL)) {
  98          $continueparams = [
  99              'action' => 'delete',
 100              'issuerid' => $issuerid,
 101              'endpointid' => $endpointid,
 102              'sesskey' => sesskey(),
 103              'confirm' => true
 104          ];
 105          $continueurl = new moodle_url('/admin/tool/oauth2/endpoints.php', $continueparams);
 106          $cancelurl = new moodle_url('/admin/tool/oauth2/endpoints.php');
 107          echo $OUTPUT->header();
 108          $strparams = [ 'issuer' => s($issuer->get('name')), 'endpoint' => s($endpoint->get('name')) ];
 109          echo $OUTPUT->confirm(get_string('deleteendpointconfirm', 'tool_oauth2', $strparams), $continueurl, $cancelurl);
 110          echo $OUTPUT->footer();
 111      } else {
 112          require_sesskey();
 113          core\oauth2\api::delete_endpoint($endpointid);
 114          redirect($PAGE->url, get_string('endpointdeleted', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
 115      }
 116  
 117  } else {
 118      echo $OUTPUT->header();
 119      echo $OUTPUT->heading(get_string('endpointsforissuer', 'tool_oauth2', s($issuer->get('name'))));
 120      $endpoints = core\oauth2\api::get_endpoints($issuer);
 121      echo $renderer->endpoints_table($endpoints, $issuerid);
 122  
 123      $addurl = new moodle_url('/admin/tool/oauth2/endpoints.php', ['action' => 'edit', 'issuerid' => $issuerid]);
 124      echo $renderer->single_button($addurl, get_string('createnewendpoint', 'tool_oauth2', s($issuer->get('name'))));
 125      echo $OUTPUT->footer();
 126  }