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 39 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 Linked login configuration page.
  19   *
  20   * @package    auth_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('/auth/oauth2/linkedlogins.php');
  30  $PAGE->set_context(context_user::instance($USER->id));
  31  $PAGE->set_pagelayout('admin');
  32  $strheading = get_string('linkedlogins', 'auth_oauth2');
  33  $PAGE->set_title($strheading);
  34  $PAGE->set_heading($strheading);
  35  
  36  require_login();
  37  
  38  if (!\auth_oauth2\api::is_enabled()) {
  39      throw new \moodle_exception('notenabled', 'auth_oauth2');
  40  }
  41  
  42  $action = optional_param('action', '', PARAM_ALPHAEXT);
  43  if ($action == 'new') {
  44      require_sesskey();
  45      $issuerid = required_param('issuerid', PARAM_INT);
  46      $issuer = \core\oauth2\api::get_issuer($issuerid);
  47  
  48      if (!$issuer->is_available_for_login()) {
  49          throw new \moodle_exception('issuernologin', 'auth_oauth2');
  50      }
  51  
  52      // We do a login dance with this issuer.
  53      $addparams = ['action' => 'new', 'issuerid' => $issuerid, 'sesskey' => sesskey()];
  54      $addurl = new moodle_url('/auth/oauth2/linkedlogins.php', $addparams);
  55      $client = \core\oauth2\api::get_user_oauth_client($issuer, $addurl);
  56  
  57      if (optional_param('logout', false, PARAM_BOOL)) {
  58          $client->log_out();
  59      }
  60  
  61      if (!$client->is_logged_in()) {
  62          redirect($client->get_login_url());
  63      }
  64  
  65      $userinfo = $client->get_userinfo();
  66  
  67      if (!empty($userinfo)) {
  68          try {
  69              \auth_oauth2\api::link_login($userinfo, $issuer);
  70              redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
  71          } catch (Exception $e) {
  72              redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
  73          }
  74      } else {
  75          redirect($PAGE->url, get_string('notloggedin', 'auth_oauth2'), null, \core\output\notification::NOTIFY_ERROR);
  76      }
  77  } else if ($action == 'delete') {
  78      require_sesskey();
  79      $linkedloginid = required_param('linkedloginid', PARAM_INT);
  80  
  81      auth_oauth2\api::delete_linked_login($linkedloginid);
  82      redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
  83  }
  84  
  85  $renderer = $PAGE->get_renderer('auth_oauth2');
  86  
  87  $linkedloginid = optional_param('id', '', PARAM_RAW);
  88  $linkedlogin = null;
  89  
  90  auth_oauth2\api::clean_orphaned_linked_logins();
  91  
  92  $issuers = \core\oauth2\api::get_all_issuers(true);
  93  
  94  $anyshowinloginpage = false;
  95  $issuerbuttons = array();
  96  foreach ($issuers as $issuer) {
  97      if (!$issuer->is_available_for_login()) {
  98          continue;
  99      }
 100      $anyshowinloginpage = true;
 101  
 102      $addparams = ['action' => 'new', 'issuerid' => $issuer->get('id'), 'sesskey' => sesskey(), 'logout' => true];
 103      $addurl = new moodle_url('/auth/oauth2/linkedlogins.php', $addparams);
 104      $issuerbuttons[$issuer->get('id')] = $renderer->single_button($addurl, get_string('createnewlinkedlogin', 'auth_oauth2',
 105          s($issuer->get_display_name())));
 106  }
 107  
 108  if (!$anyshowinloginpage) {
 109      // Just a notification that we can't make it.
 110      $preferencesurl = new moodle_url('/user/preferences.php');
 111      redirect($preferencesurl, get_string('noissuersavailable', 'auth_oauth2'), null, \core\output\notification::NOTIFY_WARNING);
 112  }
 113  
 114  echo $OUTPUT->header();
 115  echo $OUTPUT->heading(get_string('linkedlogins', 'auth_oauth2'));
 116  echo $OUTPUT->doc_link('Linked_Logins', get_string('linkedloginshelp', 'auth_oauth2'));
 117  $linkedlogins = auth_oauth2\api::get_linked_logins();
 118  
 119  echo $renderer->linked_logins_table($linkedlogins);
 120  
 121  foreach ($issuerbuttons as $issuerbutton) {
 122      echo $issuerbutton;
 123  }
 124  
 125  echo $OUTPUT->footer();
 126  
 127