2 // This file is part of Moodle - https://moodle.org/
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.
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.
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/>.
18 * Provides the {@see \core_webservice\token_form} class.
20 * @package core_webservice
22 * @copyright 2020 David Mudrák <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core_webservice
;
31 * Form to create and edit a web service token.
33 * Tokens allow users call external functions provided by selected web services. They can optionally have IP restriction
34 * and date validity defined.
36 * @copyright 2010 Jerome Mouneyrac <jerome@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class token_form
extends \moodleform
{
42 * Defines the form fields.
44 public function definition() {
47 $mform = $this->_form
;
48 $data = $this->_customdata
;
50 $mform->addElement('header', 'token', get_string('token', 'webservice'));
55 'ajax' => 'core_user/form_user_selector',
56 'valuehtmlcallback' => function($userid) {
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
);
65 'fullname' => fullname($record, has_capability('moodle/site:viewfullnames', $context)),
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)
76 return $OUTPUT->render_from_template('core_user/form_user_selector_suggestion', $user);
79 $mform->addElement('autocomplete', 'user', get_string('user'), [], $attributes);
80 $mform->addRule('user', get_string('required'), 'required', null, 'client');
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
);
88 $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));
89 $mform->setType('iprestriction', PARAM_RAW_TRIMMED
);
91 $mform->addElement('date_selector', 'validuntil',
92 get_string('validuntil', 'webservice'), array('optional' => true));
93 $mform->setType('validuntil', PARAM_INT
);
95 $mform->addElement('hidden', 'action');
96 $mform->setType('action', PARAM_ALPHANUMEXT
);
98 $this->add_action_buttons(true);
100 $this->set_data($data);
104 * Validate the submitted data.
106 * @param array $data Submitted data.
107 * @param array $files Submitted files.
108 * @return array Validation errors.
110 public function validation($data, $files) {
113 $errors = parent
::validation($data, $files);
115 if ($DB->get_field('user', 'suspended', ['id' => $data['user']], MUST_EXIST
)) {
116 $errors['user'] = get_string('suspended', 'core') . ' - ' . get_string('forbiddenwsuser', 'core_webservice');