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   * OAuth 2 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/issuers.php');
  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 = optional_param('id', '', PARAM_RAW);
  42  $issuer = null;
  43  $mform = null;
  44  
  45  if ($issuerid) {
  46      $issuer = \core\oauth2\api::get_issuer($issuerid);
  47      if (!$issuer) {
  48          throw new \moodle_exception('invaliddata');
  49      }
  50  }
  51  
  52  if ($action == 'edit') {
  53      if ($issuer) {
  54          $PAGE->navbar->add(get_string('editissuer', 'tool_oauth2', s($issuer->get('name'))));
  55      } else {
  56          $PAGE->navbar->add(get_string('createnewservice', 'tool_oauth2') . ' ' . get_string('custom_service', 'tool_oauth2'));
  57      }
  58  
  59      $mform = new \tool_oauth2\form\issuer(null, ['persistent' => $issuer]);
  60  }
  61  
  62  if ($mform && $mform->is_cancelled()) {
  63      redirect(new moodle_url('/admin/tool/oauth2/issuers.php'));
  64  } else if ($action == 'edit') {
  65  
  66      if ($data = $mform->get_data()) {
  67          try {
  68              if (!empty($data->id)) {
  69                  core\oauth2\api::update_issuer($data);
  70              } else {
  71                  core\oauth2\api::create_issuer($data);
  72              }
  73              redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
  74          } catch (Exception $e) {
  75              redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
  76          }
  77      } else {
  78          echo $OUTPUT->header();
  79          if ($issuer) {
  80              echo $OUTPUT->heading(get_string('editissuer', 'tool_oauth2', s($issuer->get('name'))));
  81          } else {
  82              echo $OUTPUT->heading(get_string('createnewservice', 'tool_oauth2') . ' ' . get_string('custom_service', 'tool_oauth2'));
  83          }
  84          $mform->display();
  85          echo $OUTPUT->footer();
  86      }
  87  } else if ($action == 'savetemplate') {
  88  
  89      $type = required_param('type', PARAM_ALPHANUM);
  90      $mform = new \tool_oauth2\form\issuer(null, [
  91          'persistent' => $issuer,
  92          'type' => $type,
  93          'showrequireconfirm' => true, // Ensure the "requireconfirmation" field is included in form data.
  94      ]);
  95      if ($mform->is_cancelled()) {
  96          redirect(new moodle_url('/admin/tool/oauth2/issuers.php'));
  97      }
  98      if ($mform->is_submitted() && $data = $mform->get_data()) {
  99          $issuer = new core\oauth2\issuer(0, $data);
 100          $issuer->create();
 101          $issuer = core\oauth2\api::create_endpoints_for_standard_issuer($type, $issuer);
 102          redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
 103      } else {
 104          echo $OUTPUT->header();
 105          echo $OUTPUT->heading(get_string('createnewservice', 'tool_oauth2') . ' ' . get_string($type . '_service', 'tool_oauth2'));
 106          $mform->display();
 107          echo $OUTPUT->footer();
 108      }
 109  
 110  } else if ($action == 'edittemplate') {
 111  
 112      $type = required_param('type', PARAM_ALPHANUM);
 113      $docs = required_param('docslink', PARAM_ALPHAEXT);
 114      require_sesskey();
 115      $issuer = core\oauth2\api::init_standard_issuer($type);
 116      $mform = new \tool_oauth2\form\issuer(null, ['persistent' => $issuer, 'type' => $type]);
 117  
 118      $PAGE->navbar->add(get_string('createnewservice', 'tool_oauth2') . ' ' . get_string($type . '_service', 'tool_oauth2'));
 119      echo $OUTPUT->header();
 120      echo $OUTPUT->heading(get_string('createnewservice', 'tool_oauth2') . ' ' . get_string($type . '_service', 'tool_oauth2'));
 121      $mform->display();
 122      echo $OUTPUT->footer();
 123  
 124  } else if ($action == 'enable') {
 125  
 126      require_sesskey();
 127      core\oauth2\api::enable_issuer($issuerid);
 128      redirect($PAGE->url, get_string('issuerenabled', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
 129  
 130  } else if ($action == 'disable') {
 131  
 132      require_sesskey();
 133      core\oauth2\api::disable_issuer($issuerid);
 134      redirect($PAGE->url, get_string('issuerdisabled', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
 135  
 136  } else if ($action == 'delete') {
 137  
 138      if (!optional_param('confirm', false, PARAM_BOOL)) {
 139          $continueparams = ['action' => 'delete', 'id' => $issuerid, 'sesskey' => sesskey(), 'confirm' => true];
 140          $continueurl = new moodle_url('/admin/tool/oauth2/issuers.php', $continueparams);
 141          $cancelurl = new moodle_url('/admin/tool/oauth2/issuers.php');
 142          echo $OUTPUT->header();
 143          echo $OUTPUT->confirm(get_string('deleteconfirm', 'tool_oauth2', s($issuer->get('name'))), $continueurl, $cancelurl);
 144          echo $OUTPUT->footer();
 145      } else {
 146          require_sesskey();
 147          core\oauth2\api::delete_issuer($issuerid);
 148          redirect($PAGE->url, get_string('issuerdeleted', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
 149      }
 150  
 151  } else if ($action == 'auth') {
 152  
 153      if (!optional_param('confirm', false, PARAM_BOOL)) {
 154          $continueparams = ['action' => 'auth', 'id' => $issuerid, 'sesskey' => sesskey(), 'confirm' => true];
 155          $continueurl = new moodle_url('/admin/tool/oauth2/issuers.php', $continueparams);
 156          $cancelurl = new moodle_url('/admin/tool/oauth2/issuers.php');
 157          echo $OUTPUT->header();
 158          echo $OUTPUT->confirm(get_string('authconfirm', 'tool_oauth2', s($issuer->get('name'))), $continueurl, $cancelurl);
 159          echo $OUTPUT->footer();
 160      } else {
 161          require_sesskey();
 162          $params = ['sesskey' => sesskey(), 'id' => $issuerid, 'action' => 'auth', 'confirm' => true, 'response' => true];
 163          if (core\oauth2\api::connect_system_account($issuer, new moodle_url('/admin/tool/oauth2/issuers.php', $params))) {
 164              redirect($PAGE->url, get_string('authconnected', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
 165          } else {
 166              redirect($PAGE->url, get_string('authnotconnected', 'tool_oauth2'), null, \core\output\notification::NOTIFY_ERROR);
 167          }
 168      }
 169  } else if ($action == 'moveup') {
 170      require_sesskey();
 171      core\oauth2\api::move_up_issuer($issuerid);
 172      redirect($PAGE->url);
 173  
 174  } else if ($action == 'movedown') {
 175      require_sesskey();
 176      core\oauth2\api::move_down_issuer($issuerid);
 177      redirect($PAGE->url);
 178  
 179  } else {
 180      echo $OUTPUT->header();
 181      echo $OUTPUT->heading(get_string('pluginname', 'tool_oauth2'));
 182      echo $OUTPUT->doc_link('OAuth2_Services', get_string('serviceshelp', 'tool_oauth2'));
 183      $issuers = core\oauth2\api::get_all_issuers(true);
 184      echo $renderer->issuers_table($issuers);
 185  
 186      echo $renderer->container_start();
 187      echo get_string('createnewservice', 'tool_oauth2') . ' ';
 188  
 189      // Google template.
 190      $docs = 'admin/tool/oauth2/issuers/google';
 191      $params = ['action' => 'edittemplate', 'type' => 'google', 'sesskey' => sesskey(), 'docslink' => $docs];
 192      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 193      echo $renderer->single_button($addurl, get_string('google_service', 'tool_oauth2'));
 194  
 195      // Microsoft template.
 196      $docs = 'admin/tool/oauth2/issuers/microsoft';
 197      $params = ['action' => 'edittemplate', 'type' => 'microsoft', 'sesskey' => sesskey(), 'docslink' => $docs];
 198      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 199      echo $renderer->single_button($addurl, get_string('microsoft_service', 'tool_oauth2'));
 200  
 201      // Facebook template.
 202      $docs = 'admin/tool/oauth2/issuers/facebook';
 203      $params = ['action' => 'edittemplate', 'type' => 'facebook', 'sesskey' => sesskey(), 'docslink' => $docs];
 204      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 205      echo $renderer->single_button($addurl, get_string('facebook_service', 'tool_oauth2'));
 206  
 207      // Nextcloud template.
 208      $docs = 'admin/tool/oauth2/issuers/nextcloud';
 209      $params = ['action' => 'edittemplate', 'type' => 'nextcloud', 'sesskey' => sesskey(), 'docslink' => $docs];
 210      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 211      echo $renderer->single_button($addurl, get_string('nextcloud_service', 'tool_oauth2'));
 212  
 213      // IMS Open Badges Connect template.
 214      $docs = 'admin/tool/oauth2/issuers/imsobv2p1';
 215      $params = ['action' => 'edittemplate', 'type' => 'imsobv2p1', 'sesskey' => sesskey(), 'docslink' => $docs];
 216      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 217      echo $renderer->single_button($addurl, get_string('imsobv2p1_service', 'tool_oauth2'));
 218  
 219      // Linkedin template.
 220      $docs = 'admin/tool/oauth2/issuers/linkedin';
 221      $params = ['action' => 'edittemplate', 'type' => 'linkedin', 'sesskey' => sesskey(), 'docslink' => $docs];
 222      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 223      echo $renderer->single_button($addurl, get_string('linkedin_service', 'tool_oauth2'));
 224  
 225      // Clever template.
 226      $docs = 'admin/tool/oauth2/issuers/clever';
 227      $params = ['action' => 'edittemplate', 'type' => 'clever', 'sesskey' => sesskey(), 'docslink' => $docs];
 228      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 229      echo $renderer->single_button($addurl, get_string('clever_service', 'tool_oauth2'));
 230  
 231      // Generic issuer.
 232      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['action' => 'edit']);
 233      echo $renderer->single_button($addurl, get_string('custom_service', 'tool_oauth2'));
 234  
 235      echo $renderer->container_end();
 236      echo $OUTPUT->footer();
 237  }