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 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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          print_error('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('createnewissuer', 'tool_oauth2'));
  57      }
  58  
  59      $showrequireconfirm = false;
  60      if (!empty($issuerid)) {
  61          // Show the "Require confirmation email" checkbox for trusted issuers like Google, Facebook and Microsoft.
  62          $likefacebook = $DB->sql_like('url', ':facebook');
  63          $likegoogle = $DB->sql_like('url', ':google');
  64          $likemicrosoft = $DB->sql_like('url', ':microsoft');
  65          $params = [
  66              'issuerid' => $issuerid,
  67              'facebook' => '%facebook%',
  68              'google' => '%google%',
  69              'microsoft' => '%microsoft%',
  70          ];
  71          $select = "issuerid = :issuerid AND ($likefacebook OR $likegoogle OR $likemicrosoft)";
  72          // We're querying from the oauth2_endpoint table because the base URLs of FB and Microsoft can be empty in the issuer table.
  73          $showrequireconfirm = $DB->record_exists_select('oauth2_endpoint', $select, $params);
  74      }
  75  
  76      $mform = new \tool_oauth2\form\issuer(null, ['persistent' => $issuer, 'showrequireconfirm' => $showrequireconfirm]);
  77  }
  78  
  79  if ($mform && $mform->is_cancelled()) {
  80      redirect(new moodle_url('/admin/tool/oauth2/issuers.php'));
  81  } else if ($action == 'edit') {
  82  
  83      if ($data = $mform->get_data()) {
  84          try {
  85              if (!empty($data->id)) {
  86                  core\oauth2\api::update_issuer($data);
  87              } else {
  88                  core\oauth2\api::create_issuer($data);
  89              }
  90              redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
  91          } catch (Exception $e) {
  92              redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
  93          }
  94      } else {
  95          echo $OUTPUT->header();
  96          if ($issuer) {
  97              echo $OUTPUT->heading(get_string('editissuer', 'tool_oauth2', s($issuer->get('name'))));
  98          } else {
  99              echo $OUTPUT->heading(get_string('createnewissuer', 'tool_oauth2'));
 100          }
 101          $mform->display();
 102          echo $OUTPUT->footer();
 103      }
 104  } else if ($action == 'savetemplate') {
 105  
 106      $type = required_param('type', PARAM_ALPHA);
 107      $mform = new \tool_oauth2\form\issuer(null, ['persistent' => $issuer, 'type' => $type]);
 108      if ($mform->is_cancelled()) {
 109          redirect(new moodle_url('/admin/tool/oauth2/issuers.php'));
 110      }
 111      if ($mform->is_submitted() && $data = $mform->get_data()) {
 112          $issuer = new core\oauth2\issuer(0, $data);
 113          $issuer->create();
 114          $issuer = core\oauth2\api::create_endpoints_for_standard_issuer($type, $issuer);
 115          redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
 116      } else {
 117          echo $OUTPUT->header();
 118          $mform->display();
 119          echo $OUTPUT->footer();
 120      }
 121  
 122  } else if ($action == 'edittemplate') {
 123  
 124      $type = required_param('type', PARAM_ALPHA);
 125      $docs = required_param('docslink', PARAM_ALPHAEXT);
 126      $showrequireconfirm = optional_param('showrequireconfirm', false, PARAM_BOOL);
 127      require_sesskey();
 128      $issuer = core\oauth2\api::init_standard_issuer($type);
 129      $mform = new \tool_oauth2\form\issuer(null, ['persistent' => $issuer, 'type' => $type,
 130          'showrequireconfirm' => $showrequireconfirm]);
 131  
 132      echo $OUTPUT->header();
 133      $mform->display();
 134      echo $OUTPUT->footer();
 135  
 136  } else if ($action == 'enable') {
 137  
 138      require_sesskey();
 139      core\oauth2\api::enable_issuer($issuerid);
 140      redirect($PAGE->url, get_string('issuerenabled', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
 141  
 142  } else if ($action == 'disable') {
 143  
 144      require_sesskey();
 145      core\oauth2\api::disable_issuer($issuerid);
 146      redirect($PAGE->url, get_string('issuerdisabled', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
 147  
 148  } else if ($action == 'delete') {
 149  
 150      if (!optional_param('confirm', false, PARAM_BOOL)) {
 151          $continueparams = ['action' => 'delete', 'id' => $issuerid, 'sesskey' => sesskey(), 'confirm' => true];
 152          $continueurl = new moodle_url('/admin/tool/oauth2/issuers.php', $continueparams);
 153          $cancelurl = new moodle_url('/admin/tool/oauth2/issuers.php');
 154          echo $OUTPUT->header();
 155          echo $OUTPUT->confirm(get_string('deleteconfirm', 'tool_oauth2', s($issuer->get('name'))), $continueurl, $cancelurl);
 156          echo $OUTPUT->footer();
 157      } else {
 158          require_sesskey();
 159          core\oauth2\api::delete_issuer($issuerid);
 160          redirect($PAGE->url, get_string('issuerdeleted', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
 161      }
 162  
 163  } else if ($action == 'auth') {
 164  
 165      if (!optional_param('confirm', false, PARAM_BOOL)) {
 166          $continueparams = ['action' => 'auth', 'id' => $issuerid, 'sesskey' => sesskey(), 'confirm' => true];
 167          $continueurl = new moodle_url('/admin/tool/oauth2/issuers.php', $continueparams);
 168          $cancelurl = new moodle_url('/admin/tool/oauth2/issuers.php');
 169          echo $OUTPUT->header();
 170          echo $OUTPUT->confirm(get_string('authconfirm', 'tool_oauth2', s($issuer->get('name'))), $continueurl, $cancelurl);
 171          echo $OUTPUT->footer();
 172      } else {
 173          require_sesskey();
 174          $params = ['sesskey' => sesskey(), 'id' => $issuerid, 'action' => 'auth', 'confirm' => true, 'response' => true];
 175          if (core\oauth2\api::connect_system_account($issuer, new moodle_url('/admin/tool/oauth2/issuers.php', $params))) {
 176              redirect($PAGE->url, get_string('authconnected', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
 177          } else {
 178              redirect($PAGE->url, get_string('authnotconnected', 'tool_oauth2'), null, \core\output\notification::NOTIFY_ERROR);
 179          }
 180      }
 181  } else if ($action == 'moveup') {
 182      require_sesskey();
 183      core\oauth2\api::move_up_issuer($issuerid);
 184      redirect($PAGE->url);
 185  
 186  } else if ($action == 'movedown') {
 187      require_sesskey();
 188      core\oauth2\api::move_down_issuer($issuerid);
 189      redirect($PAGE->url);
 190  
 191  } else {
 192      echo $OUTPUT->header();
 193      echo $OUTPUT->heading(get_string('pluginname', 'tool_oauth2'));
 194      echo $OUTPUT->doc_link('OAuth2_Services', get_string('serviceshelp', 'tool_oauth2'));
 195      $issuers = core\oauth2\api::get_all_issuers();
 196      echo $renderer->issuers_table($issuers);
 197  
 198      // Google template.
 199      $docs = 'admin/tool/oauth2/issuers/google';
 200      $params = ['action' => 'edittemplate', 'type' => 'google', 'sesskey' => sesskey(), 'docslink' => $docs,
 201          'showrequireconfirm' => true];
 202      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 203      echo $renderer->single_button($addurl, get_string('createnewgoogleissuer', 'tool_oauth2'));
 204  
 205      // Microsoft template.
 206      $docs = 'admin/tool/oauth2/issuers/microsoft';
 207      $params = ['action' => 'edittemplate', 'type' => 'microsoft', 'sesskey' => sesskey(), 'docslink' => $docs,
 208          'showrequireconfirm' => true];
 209      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 210      echo $renderer->single_button($addurl, get_string('createnewmicrosoftissuer', 'tool_oauth2'));
 211  
 212      // Facebook template.
 213      $docs = 'admin/tool/oauth2/issuers/facebook';
 214      $params = ['action' => 'edittemplate', 'type' => 'facebook', 'sesskey' => sesskey(), 'docslink' => $docs,
 215          'showrequireconfirm' => true];
 216      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 217      echo $renderer->single_button($addurl, get_string('createnewfacebookissuer', 'tool_oauth2'));
 218  
 219      // Nextcloud template.
 220      $docs = 'admin/tool/oauth2/issuers/nextcloud';
 221      $params = ['action' => 'edittemplate', 'type' => 'nextcloud', 'sesskey' => sesskey(), 'docslink' => $docs];
 222      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 223      echo $renderer->single_button($addurl, get_string('createnewnextcloudissuer', 'tool_oauth2'));
 224  
 225      // Linkedin template.
 226      $docs = 'admin/tool/oauth2/issuers/linkedin';
 227      $params = ['action' => 'edittemplate', 'type' => 'linkedin', 'sesskey' => sesskey(), 'docslink' => $docs];
 228      $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 229      echo $renderer->single_button($addurl, get_string('linkedin_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('createnewissuer', 'tool_oauth2'));
 234  
 235      echo $OUTPUT->footer();
 236  
 237  }