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 * Web services / external tokens management UI.
20 * @package core_webservice
22 * @copyright 2009 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 use core_reportbuilder\system_report_factory
;
27 use core_webservice\reportbuilder\local\systemreports\tokens
;
29 require(__DIR__
. '/../../config.php');
30 require_once($CFG->libdir
. '/adminlib.php');
31 require_once($CFG->dirroot
. '/webservice/lib.php');
33 $action = optional_param('action', '', PARAM_ALPHANUMEXT
);
34 $tokenid = optional_param('tokenid', '', PARAM_SAFEDIR
);
35 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
36 $fname = optional_param('fname', '', PARAM_ALPHANUM
);
37 $fusers = optional_param_array('fusers', [], PARAM_INT
);
38 $fservices = optional_param_array('fservices', [], PARAM_INT
);
40 admin_externalpage_setup('webservicetokens');
42 $PAGE->set_primary_active_tab('siteadminnode');
43 $PAGE->navbar
->add(get_string('managetokens', 'webservice'),
44 new moodle_url('/admin/webservice/tokens.php'));
46 if ($action === 'create') {
47 $PAGE->navbar
->add(get_string('createtoken', 'webservice'), $PAGE->url
);
48 $webservicemanager = new webservice();
49 $mform = new \core_webservice\token_form
(null, ['action' => 'create']);
50 $data = $mform->get_data();
52 if ($mform->is_cancelled()) {
56 ignore_user_abort(true);
58 // Check the user is allowed for the service.
59 $selectedservice = $webservicemanager->get_external_service_by_id($data->service
);
61 if ($selectedservice->restrictedusers
) {
62 $restricteduser = $webservicemanager->get_ws_authorised_user($data->service
, $data->user
);
64 if (empty($restricteduser)) {
65 $errormsg = $OUTPUT->notification(get_string('usernotallowed', 'webservice', $selectedservice->name
));
69 $user = \core_user
::get_user($data->user
, '*', MUST_EXIST
);
70 \core_user
::require_active_user($user);
72 // Generate the token.
73 if (empty($errormsg)) {
74 \core_external\util
::generate_token(
75 EXTERNAL_TOKEN_PERMANENT
,
76 \core_external\util
::get_service_by_id($data->service
),
78 context_system
::instance(),
87 echo $OUTPUT->header();
88 echo $OUTPUT->heading(get_string('createtoken', 'webservice'));
89 if (!empty($errormsg)) {
93 echo $OUTPUT->footer();
97 if ($action === 'delete') {
98 $PAGE->navbar
->add(get_string('deletetoken', 'webservice'), $PAGE->url
);
99 $webservicemanager = new webservice();
100 $token = $webservicemanager->get_token_by_id_with_details($tokenid);
102 if ($token->creatorid
!= $USER->id
) {
103 require_capability('moodle/webservice:managealltokens', context_system
::instance());
106 if ($confirm && confirm_sesskey()) {
107 $webservicemanager->delete_user_ws_token($token->id
);
108 redirect($PAGE->url
);
111 echo $OUTPUT->header();
113 echo $OUTPUT->confirm(
114 get_string('deletetokenconfirm', 'webservice', [
115 'user' => $token->firstname
. ' ' . $token->lastname
,
116 'service' => $token->name
,
118 new single_button(new moodle_url('/admin/webservice/tokens.php', [
119 'tokenid' => $token->id
,
120 'action' => 'delete',
122 'sesskey' => sesskey(),
123 ]), get_string('delete')),
127 echo $OUTPUT->footer();
131 echo $OUTPUT->header();
132 echo $OUTPUT->container_start('d-flex flex-wrap');
133 echo $OUTPUT->heading(get_string('managetokens', 'core_webservice'));
134 echo html_writer
::div($OUTPUT->render(new single_button(new moodle_url($PAGE->url
, ['action' => 'create']),
135 get_string('createtoken', 'core_webservice'), 'get', single_button
::BUTTON_PRIMARY
)), 'ms-auto');
136 echo $OUTPUT->container_end();
138 if (!empty($SESSION->webservicenewlycreatedtoken
)) {
139 $webservicemanager = new webservice();
140 $newtoken = $webservicemanager->get_created_by_user_ws_token(
142 $SESSION->webservicenewlycreatedtoken
145 // Unset the session variable.
146 unset($SESSION->webservicenewlycreatedtoken
);
147 // Display the newly created token.
148 echo $OUTPUT->render_from_template(
149 'core_admin/webservice_token_new', ['token' => $newtoken->token
, 'tokenname' => $newtoken->tokenname
]
154 $report = system_report_factory
::create(tokens
::class, context_system
::instance());
155 echo $report->output();
157 echo $OUTPUT->footer();