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
;
33 * Form to create and edit a web service token.
35 * Tokens allow users call external functions provided by selected web services. They can optionally have IP restriction
36 * and date validity defined.
38 * @copyright 2010 Jerome Mouneyrac <jerome@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class token_form
extends \moodleform
{
44 * Defines the form fields.
46 public function definition() {
49 $mform = $this->_form
;
50 $data = $this->_customdata
;
52 $mform->addElement('header', 'token', get_string('token', 'webservice'));
54 $mform->addElement('text', 'name', get_string('tokenname', 'webservice'));
55 $mform->setType('name', PARAM_TEXT
);
56 $mform->addElement('static', 'tokennamehint', '', get_string('tokennamehint', 'webservice'));
61 'ajax' => 'core_user/form_user_selector',
62 'valuehtmlcallback' => function($userid) {
65 $context = \context_system
::instance();
66 $fields = \core_user\fields
::for_name()->with_identity($context, false);
67 $record = core_user
::get_user($userid, 'id ' . $fields->get_sql()->selects
, MUST_EXIST
);
71 'fullname' => fullname($record, has_capability('moodle/site:viewfullnames', $context)),
75 foreach ($fields->get_required_fields([\core_user\fields
::PURPOSE_IDENTITY
]) as $extrafield) {
76 $user->extrafields
[] = (object)[
77 'name' => $extrafield,
78 'value' => s($record->$extrafield)
82 return $OUTPUT->render_from_template('core_user/form_user_selector_suggestion', $user);
85 $mform->addElement('autocomplete', 'user', get_string('user'), [], $attributes);
86 $mform->addRule('user', get_string('required'), 'required', null, 'client');
89 $options = $DB->get_records_menu('external_services', null, '', 'id, name');
90 $mform->addElement('select', 'service', get_string('service', 'webservice'), $options);
91 $mform->addRule('service', get_string('required'), 'required', null, 'client');
92 $mform->setType('service', PARAM_INT
);
94 $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));
95 $mform->setType('iprestriction', PARAM_RAW_TRIMMED
);
97 $mform->addElement('date_selector', 'validuntil',
98 get_string('validuntil', 'webservice'), array('optional' => true));
99 // Expires in 30 days.
100 $expires = new DateTime();
101 $expires->add(new DateInterval("P30D"));
102 $mform->setDefault('validuntil', $expires->getTimestamp());
103 $mform->setType('validuntil', PARAM_INT
);
105 $mform->addElement('hidden', 'action');
106 $mform->setType('action', PARAM_ALPHANUMEXT
);
108 $this->add_action_buttons(true);
110 $this->set_data($data);
114 * Validate the submitted data.
116 * @param array $data Submitted data.
117 * @param array $files Submitted files.
118 * @return array Validation errors.
120 public function validation($data, $files) {
123 $errors = parent
::validation($data, $files);
125 if ($DB->get_field('user', 'suspended', ['id' => $data['user']], MUST_EXIST
)) {
126 $errors['user'] = get_string('suspended', 'core') . ' - ' . get_string('forbiddenwsuser', 'core_webservice');