Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   1  <?php
   2  // This file is part of Moodle - https://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   * Web services / external tokens management UI.
  19   *
  20   * @package     core_webservice
  21   * @category    admin
  22   * @copyright   2009 Jerome Mouneyrac
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require(__DIR__ . '/../../config.php');
  27  require_once($CFG->libdir . '/adminlib.php');
  28  require_once($CFG->dirroot . '/webservice/lib.php');
  29  
  30  $action = optional_param('action', '', PARAM_ALPHANUMEXT);
  31  $tokenid = optional_param('tokenid', '', PARAM_SAFEDIR);
  32  $confirm = optional_param('confirm', 0, PARAM_BOOL);
  33  $fname = optional_param('fname', '', PARAM_ALPHANUM);
  34  $fusers = optional_param_array('fusers', [], PARAM_INT);
  35  $fservices = optional_param_array('fservices', [], PARAM_INT);
  36  
  37  admin_externalpage_setup('webservicetokens');
  38  
  39  $PAGE->set_primary_active_tab('siteadminnode');
  40  $PAGE->navbar->add(get_string('managetokens', 'webservice'),
  41      new moodle_url('/admin/webservice/tokens.php'));
  42  
  43  if ($action === 'create') {
  44      $PAGE->navbar->add(get_string('createtoken', 'webservice'), $PAGE->url);
  45      $webservicemanager = new webservice();
  46      $mform = new \core_webservice\token_form(null, ['action' => 'create']);
  47      $data = $mform->get_data();
  48  
  49      if ($mform->is_cancelled()) {
  50          redirect($PAGE->url);
  51  
  52      } else if ($data) {
  53          ignore_user_abort(true);
  54  
  55          // Check the user is allowed for the service.
  56          $selectedservice = $webservicemanager->get_external_service_by_id($data->service);
  57  
  58          if ($selectedservice->restrictedusers) {
  59              $restricteduser = $webservicemanager->get_ws_authorised_user($data->service, $data->user);
  60  
  61              if (empty($restricteduser)) {
  62                  $errormsg = $OUTPUT->notification(get_string('usernotallowed', 'webservice', $selectedservice->name));
  63              }
  64          }
  65  
  66          $user = \core_user::get_user($data->user, '*', MUST_EXIST);
  67          \core_user::require_active_user($user);
  68  
  69          // Generate the token.
  70          if (empty($errormsg)) {
  71              \core_external\util::generate_token(
  72                  EXTERNAL_TOKEN_PERMANENT,
  73                  \core_external\util::get_service_by_id($data->service),
  74                  $data->user,
  75                  context_system::instance(),
  76                  $data->validuntil,
  77                  $data->iprestriction,
  78                  $data->name
  79              );
  80              redirect($PAGE->url);
  81          }
  82      }
  83  
  84      echo $OUTPUT->header();
  85      echo $OUTPUT->heading(get_string('createtoken', 'webservice'));
  86      if (!empty($errormsg)) {
  87          echo $errormsg;
  88      }
  89      $mform->display();
  90      echo $OUTPUT->footer();
  91      die();
  92  }
  93  
  94  if ($action === 'delete') {
  95      $PAGE->navbar->add(get_string('deletetoken', 'webservice'), $PAGE->url);
  96      $webservicemanager = new webservice();
  97      $token = $webservicemanager->get_token_by_id_with_details($tokenid);
  98  
  99      if ($token->creatorid != $USER->id) {
 100          require_capability('moodle/webservice:managealltokens', context_system::instance());
 101      }
 102  
 103      if ($confirm && confirm_sesskey()) {
 104          $webservicemanager->delete_user_ws_token($token->id);
 105          redirect($PAGE->url);
 106      }
 107  
 108      echo $OUTPUT->header();
 109  
 110      echo $OUTPUT->confirm(
 111          get_string('deletetokenconfirm', 'webservice', [
 112              'user' => $token->firstname . ' ' . $token->lastname,
 113              'service' => $token->name,
 114          ]),
 115          new single_button(new moodle_url('/admin/webservice/tokens.php', [
 116              'tokenid' => $token->id,
 117              'action' => 'delete',
 118              'confirm' => 1,
 119              'sesskey' => sesskey(),
 120          ]), get_string('delete')),
 121          $PAGE->url
 122      );
 123  
 124      echo $OUTPUT->footer();
 125      die();
 126  }
 127  
 128  // Pre-populate the form with the values that come as a part of the URL - typically when using the table_sql control
 129  // links.
 130  $filterdata = (object)[
 131      'name' => $fname,
 132      'users' => $fusers,
 133      'services' => $fservices,
 134  ];
 135  
 136  $filter = new \core_webservice\token_filter($PAGE->url, $filterdata);
 137  
 138  $filter->set_data($filterdata);
 139  
 140  if ($filter->is_submitted()) {
 141      $filterdata = $filter->get_data();
 142  
 143      if (isset($filterdata->resetbutton)) {
 144          redirect($PAGE->url);
 145      }
 146  }
 147  
 148  echo $OUTPUT->header();
 149  echo $OUTPUT->heading(get_string('managetokens', 'core_webservice'));
 150  
 151  echo html_writer::div($OUTPUT->render(new single_button(new moodle_url($PAGE->url, ['action' => 'create']),
 152      get_string('createtoken', 'core_webservice'), 'get', single_button::BUTTON_PRIMARY)), 'my-3');
 153  
 154  if (!empty($SESSION->webservicenewlycreatedtoken)) {
 155      $webservicemanager = new webservice();
 156      $newtoken = $webservicemanager->get_created_by_user_ws_token(
 157          $USER->id,
 158          $SESSION->webservicenewlycreatedtoken
 159      );
 160      if ($newtoken) {
 161          // Unset the session variable.
 162          unset($SESSION->webservicenewlycreatedtoken);
 163          // Display the newly created token.
 164          echo $OUTPUT->render_from_template(
 165              'core_admin/webservice_token_new', ['token' => $newtoken->token, 'tokenname' => $newtoken->tokenname]
 166          );
 167      }
 168  }
 169  
 170  $filter->display();
 171  
 172  $table = new \core_webservice\token_table('webservicetokens', $filterdata);
 173  
 174  // In order to not lose the filter form values by clicking the table control links, make them part of the table's baseurl.
 175  $baseurl = new moodle_url($PAGE->url, ['fname' => $filterdata->name]);
 176  
 177  foreach ($filterdata->users as $i => $userid) {
 178      $baseurl->param("fusers[{$i}]", $userid);
 179  }
 180  
 181  foreach ($filterdata->services as $i => $serviceid) {
 182      $baseurl->param("fservices[{$i}]", $serviceid);
 183  }
 184  
 185  $table->define_baseurl($baseurl);
 186  
 187  $table->attributes['class'] = 'admintable generaltable';
 188  $table->out(30, false);
 189  
 190  echo $OUTPUT->footer();