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 401 and 402] [Versions 401 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->libdir . '/externallib.php');
  29  require_once($CFG->dirroot . '/webservice/lib.php');
  30  
  31  $action = optional_param('action', '', PARAM_ALPHANUMEXT);
  32  $tokenid = optional_param('tokenid', '', PARAM_SAFEDIR);
  33  $confirm = optional_param('confirm', 0, PARAM_BOOL);
  34  $ftoken = optional_param('ftoken', '', PARAM_ALPHANUM);
  35  $fusers = optional_param_array('fusers', [], PARAM_INT);
  36  $fservices = optional_param_array('fservices', [], PARAM_INT);
  37  
  38  admin_externalpage_setup('webservicetokens');
  39  
  40  $PAGE->set_primary_active_tab('siteadminnode');
  41  $PAGE->navbar->add(get_string('managetokens', 'webservice'),
  42      new moodle_url('/admin/webservice/tokens.php'));
  43  
  44  if ($action === 'create') {
  45      $PAGE->navbar->add(get_string('createtoken', 'webservice'), $PAGE->url);
  46      $webservicemanager = new webservice();
  47      $mform = new \core_webservice\token_form(null, ['action' => 'create']);
  48      $data = $mform->get_data();
  49  
  50      if ($mform->is_cancelled()) {
  51          redirect($PAGE->url);
  52  
  53      } else if ($data) {
  54          ignore_user_abort(true);
  55  
  56          // Check the user is allowed for the service.
  57          $selectedservice = $webservicemanager->get_external_service_by_id($data->service);
  58  
  59          if ($selectedservice->restrictedusers) {
  60              $restricteduser = $webservicemanager->get_ws_authorised_user($data->service, $data->user);
  61  
  62              if (empty($restricteduser)) {
  63                  $errormsg = $OUTPUT->notification(get_string('usernotallowed', 'webservice', $selectedservice->name));
  64              }
  65          }
  66  
  67          $user = \core_user::get_user($data->user, '*', MUST_EXIST);
  68          \core_user::require_active_user($user);
  69  
  70          // Generate the token.
  71          if (empty($errormsg)) {
  72              external_generate_token(EXTERNAL_TOKEN_PERMANENT, $data->service, $data->user, context_system::instance(),
  73                  $data->validuntil, $data->iprestriction);
  74              redirect($PAGE->url);
  75          }
  76      }
  77  
  78      echo $OUTPUT->header();
  79      echo $OUTPUT->heading(get_string('createtoken', 'webservice'));
  80      if (!empty($errormsg)) {
  81          echo $errormsg;
  82      }
  83      $mform->display();
  84      echo $OUTPUT->footer();
  85      die();
  86  }
  87  
  88  if ($action === 'delete') {
  89      $PAGE->navbar->add(get_string('deletetoken', 'webservice'), $PAGE->url);
  90      $webservicemanager = new webservice();
  91      $token = $webservicemanager->get_token_by_id_with_details($tokenid);
  92  
  93      if ($token->creatorid != $USER->id) {
  94          require_capability('moodle/webservice:managealltokens', context_system::instance());
  95      }
  96  
  97      if ($confirm && confirm_sesskey()) {
  98          $webservicemanager->delete_user_ws_token($token->id);
  99          redirect($PAGE->url);
 100      }
 101  
 102      echo $OUTPUT->header();
 103  
 104      echo $OUTPUT->confirm(
 105          get_string('deletetokenconfirm', 'webservice', [
 106              'user' => $token->firstname . ' ' . $token->lastname,
 107              'service' => $token->name,
 108          ]),
 109          new single_button(new moodle_url('/admin/webservice/tokens.php', [
 110              'tokenid' => $token->id,
 111              'action' => 'delete',
 112              'confirm' => 1,
 113              'sesskey' => sesskey(),
 114          ]), get_string('delete')),
 115          $PAGE->url
 116      );
 117  
 118      echo $OUTPUT->footer();
 119      die();
 120  }
 121  
 122  // Pre-populate the form with the values that come as a part of the URL - typically when using the table_sql control
 123  // links.
 124  $filterdata = (object)[
 125      'token' => $ftoken,
 126      'users' => $fusers,
 127      'services' => $fservices,
 128  ];
 129  
 130  $filter = new \core_webservice\token_filter($PAGE->url, $filterdata);
 131  
 132  $filter->set_data($filterdata);
 133  
 134  if ($filter->is_submitted()) {
 135      $filterdata = $filter->get_data();
 136  
 137      if (isset($filterdata->resetbutton)) {
 138          redirect($PAGE->url);
 139      }
 140  }
 141  
 142  echo $OUTPUT->header();
 143  echo $OUTPUT->heading(get_string('managetokens', 'core_webservice'));
 144  
 145  echo html_writer::div($OUTPUT->render(new single_button(new moodle_url($PAGE->url, ['action' => 'create']),
 146      get_string('createtoken', 'core_webservice'), 'get', true)), 'my-3');
 147  
 148  $filter->display();
 149  
 150  $table = new \core_webservice\token_table('webservicetokens', $filterdata);
 151  
 152  // In order to not lose the filter form values by clicking the table control links, make them part of the table's baseurl.
 153  $baseurl = new moodle_url($PAGE->url, ['ftoken' => $filterdata->token]);
 154  
 155  foreach ($filterdata->users as $i => $userid) {
 156      $baseurl->param("fusers[{$i}]", $userid);
 157  }
 158  
 159  foreach ($filterdata->services as $i => $serviceid) {
 160      $baseurl->param("fservices[{$i}]", $serviceid);
 161  }
 162  
 163  $table->define_baseurl($baseurl);
 164  
 165  $table->attributes['class'] = 'admintable generaltable';
 166  $table->data = [];
 167  $table->out(30, false);
 168  
 169  echo $OUTPUT->footer();