1 |
efrain |
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 |
use DateInterval;
|
|
|
30 |
use DateTime;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Form to create and edit a web service token.
|
|
|
34 |
*
|
|
|
35 |
* Tokens allow users call external functions provided by selected web services. They can optionally have IP restriction
|
|
|
36 |
* and date validity defined.
|
|
|
37 |
*
|
|
|
38 |
* @copyright 2010 Jerome Mouneyrac <jerome@moodle.com>
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class token_form extends \moodleform {
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Defines the form fields.
|
|
|
45 |
*/
|
|
|
46 |
public function definition() {
|
|
|
47 |
global $DB;
|
|
|
48 |
|
|
|
49 |
$mform = $this->_form;
|
|
|
50 |
$data = $this->_customdata;
|
|
|
51 |
|
|
|
52 |
$mform->addElement('header', 'token', get_string('token', 'webservice'));
|
|
|
53 |
|
|
|
54 |
$mform->addElement('text', 'name', get_string('tokenname', 'webservice'));
|
|
|
55 |
$mform->setType('name', PARAM_TEXT);
|
|
|
56 |
$mform->addElement('static', 'tokennamehint', '', get_string('tokennamehint', 'webservice'));
|
|
|
57 |
|
|
|
58 |
// User selector.
|
|
|
59 |
$attributes = [
|
|
|
60 |
'multiple' => false,
|
|
|
61 |
'ajax' => 'core_user/form_user_selector',
|
|
|
62 |
'valuehtmlcallback' => function($userid) {
|
|
|
63 |
global $OUTPUT;
|
|
|
64 |
|
|
|
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);
|
|
|
68 |
|
|
|
69 |
$user = (object)[
|
|
|
70 |
'id' => $record->id,
|
|
|
71 |
'fullname' => fullname($record, has_capability('moodle/site:viewfullnames', $context)),
|
|
|
72 |
'extrafields' => [],
|
|
|
73 |
];
|
|
|
74 |
|
|
|
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)
|
|
|
79 |
];
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
return $OUTPUT->render_from_template('core_user/form_user_selector_suggestion', $user);
|
|
|
83 |
},
|
|
|
84 |
];
|
|
|
85 |
$mform->addElement('autocomplete', 'user', get_string('user'), [], $attributes);
|
|
|
86 |
$mform->addRule('user', get_string('required'), 'required', null, 'client');
|
|
|
87 |
|
|
|
88 |
// Service selector.
|
|
|
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);
|
|
|
93 |
|
|
|
94 |
$mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));
|
|
|
95 |
$mform->setType('iprestriction', PARAM_RAW_TRIMMED);
|
|
|
96 |
|
|
|
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);
|
|
|
104 |
|
|
|
105 |
$mform->addElement('hidden', 'action');
|
|
|
106 |
$mform->setType('action', PARAM_ALPHANUMEXT);
|
|
|
107 |
|
|
|
108 |
$this->add_action_buttons(true);
|
|
|
109 |
|
|
|
110 |
$this->set_data($data);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Validate the submitted data.
|
|
|
115 |
*
|
|
|
116 |
* @param array $data Submitted data.
|
|
|
117 |
* @param array $files Submitted files.
|
|
|
118 |
* @return array Validation errors.
|
|
|
119 |
*/
|
|
|
120 |
public function validation($data, $files) {
|
|
|
121 |
global $DB;
|
|
|
122 |
|
|
|
123 |
$errors = parent::validation($data, $files);
|
|
|
124 |
|
|
|
125 |
if ($DB->get_field('user', 'suspended', ['id' => $data['user']], MUST_EXIST)) {
|
|
|
126 |
$errors['user'] = get_string('suspended', 'core') . ' - ' . get_string('forbiddenwsuser', 'core_webservice');
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
return $errors;
|
|
|
130 |
}
|
|
|
131 |
}
|