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 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   * Provides the {@see \core_webservice\token_form} class.
  19   *
  20   * @package     core_webservice
  21   * @category    admin
  22   * @copyright   2020 David Mudrák <david@moodle.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_webservice;
  27  
  28  use core_user;
  29  
  30  /**
  31   * Form to create and edit a web service token.
  32   *
  33   * Tokens allow users call external functions provided by selected web services. They can optionally have IP restriction
  34   * and date validity defined.
  35   *
  36   * @copyright 2010 Jerome Mouneyrac <jerome@moodle.com>
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class token_form extends \moodleform {
  40  
  41      /**
  42       * Defines the form fields.
  43       */
  44      public function definition() {
  45          global $DB;
  46  
  47          $mform = $this->_form;
  48          $data = $this->_customdata;
  49  
  50          $mform->addElement('header', 'token', get_string('token', 'webservice'));
  51  
  52          // User selector.
  53          $attributes = [
  54              'multiple' => false,
  55              'ajax' => 'core_user/form_user_selector',
  56              'valuehtmlcallback' => function($userid) {
  57                  global $OUTPUT;
  58  
  59                  $context = \context_system::instance();
  60                  $fields = \core_user\fields::for_name()->with_identity($context, false);
  61                  $record = core_user::get_user($userid, 'id ' . $fields->get_sql()->selects, MUST_EXIST);
  62  
  63                  $user = (object)[
  64                      'id' => $record->id,
  65                      'fullname' => fullname($record, has_capability('moodle/site:viewfullnames', $context)),
  66                      'extrafields' => [],
  67                  ];
  68  
  69                  foreach ($fields->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]) as $extrafield) {
  70                      $user->extrafields[] = (object)[
  71                          'name' => $extrafield,
  72                          'value' => s($record->$extrafield)
  73                      ];
  74                  }
  75  
  76                  return $OUTPUT->render_from_template('core_user/form_user_selector_suggestion', $user);
  77              },
  78          ];
  79          $mform->addElement('autocomplete', 'user', get_string('user'), [], $attributes);
  80          $mform->addRule('user', get_string('required'), 'required', null, 'client');
  81  
  82          // Service selector.
  83          $options = $DB->get_records_menu('external_services', null, '', 'id, name');
  84          $mform->addElement('select', 'service', get_string('service', 'webservice'), $options);
  85          $mform->addRule('service', get_string('required'), 'required', null, 'client');
  86          $mform->setType('service', PARAM_INT);
  87  
  88          $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));
  89          $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
  90  
  91          $mform->addElement('date_selector', 'validuntil',
  92                  get_string('validuntil', 'webservice'), array('optional' => true));
  93          $mform->setType('validuntil', PARAM_INT);
  94  
  95          $mform->addElement('hidden', 'action');
  96          $mform->setType('action', PARAM_ALPHANUMEXT);
  97  
  98          $this->add_action_buttons(true);
  99  
 100          $this->set_data($data);
 101      }
 102  
 103      /**
 104       * Validate the submitted data.
 105       *
 106       * @param array $data Submitted data.
 107       * @param array $files Submitted files.
 108       * @return array Validation errors.
 109       */
 110      public function validation($data, $files) {
 111          global $DB;
 112  
 113          $errors = parent::validation($data, $files);
 114  
 115          if ($DB->get_field('user', 'suspended', ['id' => $data['user']], MUST_EXIST)) {
 116              $errors['user'] = get_string('suspended', 'core') . ' - ' . get_string('forbiddenwsuser', 'core_webservice');
 117          }
 118  
 119          return $errors;
 120      }
 121  }