| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://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 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace core_webservice\reportbuilder\local\systemreports;
|
|
|
20 |
|
|
|
21 |
use context_system;
|
|
|
22 |
use core_reportbuilder\local\entities\user;
|
|
|
23 |
use core_reportbuilder\local\report\{action, column};
|
|
|
24 |
use core_reportbuilder\system_report;
|
|
|
25 |
use core_webservice\reportbuilder\local\entities\{token, service};
|
|
|
26 |
use lang_string;
|
|
|
27 |
use moodle_url;
|
|
|
28 |
use pix_icon;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Tokens system report
|
|
|
32 |
*
|
|
|
33 |
* @package core_webservice
|
|
|
34 |
* @copyright 2023 Mikel Martín <mikel@moodle.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class tokens extends system_report {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Initialise report, we need to set the main table, load our entities and set columns/filters
|
|
|
41 |
*/
|
|
|
42 |
protected function initialise(): void {
|
|
|
43 |
global $USER;
|
|
|
44 |
|
|
|
45 |
$entitytoken = new token();
|
|
|
46 |
$entitytokenalias = $entitytoken->get_table_alias('external_tokens');
|
|
|
47 |
|
|
|
48 |
$this->set_main_table('external_tokens', $entitytokenalias);
|
|
|
49 |
$this->add_entity($entitytoken);
|
|
|
50 |
|
|
|
51 |
$entityservice = new service();
|
|
|
52 |
$entityservicealias = $entityservice->get_table_alias('external_services');
|
|
|
53 |
$this->add_entity($entityservice->add_join(
|
|
|
54 |
"LEFT JOIN {external_services} {$entityservicealias}
|
|
|
55 |
ON {$entityservicealias}.id = {$entitytokenalias}.externalserviceid"
|
|
|
56 |
));
|
|
|
57 |
|
|
|
58 |
$entityuser = new user();
|
|
|
59 |
$entityuseralias = $entityuser->get_table_alias('user');
|
|
|
60 |
$this->add_entity($entityuser->add_join(
|
|
|
61 |
"LEFT JOIN {user} {$entityuseralias} ON {$entityuseralias}.id = {$entitytokenalias}.userid"
|
|
|
62 |
));
|
|
|
63 |
|
|
|
64 |
$entitycreator = new user();
|
|
|
65 |
$entitycreator->set_entity_name('creator');
|
|
|
66 |
$entitycreatoralias = $entitycreator->get_table_alias('user');
|
|
|
67 |
$this->add_entity($entitycreator->add_join(
|
|
|
68 |
"LEFT JOIN {user} {$entitycreatoralias} ON {$entitycreatoralias}.id = {$entitytokenalias}.creatorid"
|
|
|
69 |
));
|
|
|
70 |
|
|
|
71 |
// Any columns required by actions should be defined here to ensure they're always available.
|
|
|
72 |
$this->add_base_fields("{$entitytokenalias}.id");
|
|
|
73 |
|
|
|
74 |
// Only show tokens created by the current user for non-manager users.
|
|
|
75 |
if (!has_capability('moodle/webservice:managealltokens', context_system::instance())) {
|
|
|
76 |
$this->add_base_condition_simple("{$entitycreatoralias}.userid", $USER->id);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
$this->add_columns($entityuseralias, $entityservicealias);
|
|
|
80 |
$this->add_filters();
|
|
|
81 |
$this->add_actions();
|
|
|
82 |
|
|
|
83 |
$this->set_initial_sort_column('token:validuntil', SORT_ASC);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Validates access to view this report
|
|
|
88 |
*
|
|
|
89 |
* @return bool
|
|
|
90 |
*/
|
|
|
91 |
protected function can_view(): bool {
|
|
|
92 |
return has_capability('moodle/site:config', context_system::instance());
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Adds the columns we want to display in the report
|
|
|
97 |
*
|
|
|
98 |
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
|
|
99 |
* unique identifier
|
|
|
100 |
*
|
|
|
101 |
* @param string $entityuseralias
|
|
|
102 |
* @param string $entityservicealias
|
|
|
103 |
*/
|
|
|
104 |
public function add_columns(string $entityuseralias, string $entityservicealias): void {
|
|
|
105 |
$this->add_columns_from_entities([
|
|
|
106 |
'token:name',
|
|
|
107 |
'user:fullnamewithlink',
|
|
|
108 |
]);
|
|
|
109 |
|
|
|
110 |
// Include all identity field columns.
|
|
|
111 |
$identitycolumns = $this->get_entity('user')->get_identity_columns($this->get_context());
|
|
|
112 |
foreach ($identitycolumns as $identitycolumn) {
|
|
|
113 |
$this->add_column($identitycolumn);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
$this->add_columns_from_entities([
|
|
|
117 |
'service:name',
|
|
|
118 |
'token:iprestriction',
|
|
|
119 |
'token:validuntil',
|
|
|
120 |
'token:lastaccess',
|
|
|
121 |
'creator:fullnamewithlink',
|
|
|
122 |
]);
|
|
|
123 |
|
|
|
124 |
$this->get_column('user:fullnamewithlink')
|
|
|
125 |
->set_title(new lang_string('user'));
|
|
|
126 |
$this->get_column('service:name')
|
|
|
127 |
->set_title(new lang_string('service', 'core_webservice'));
|
|
|
128 |
$this->get_column('creator:fullnamewithlink')
|
|
|
129 |
->set_title(new lang_string('tokencreator', 'core_webservice'))
|
|
|
130 |
->set_is_available(has_capability('moodle/webservice:managealltokens', context_system::instance()));
|
|
|
131 |
|
|
|
132 |
$this->add_column((new column(
|
|
|
133 |
'missingcapabilities',
|
|
|
134 |
new lang_string('missingcaps', 'webservice'),
|
|
|
135 |
'user'
|
|
|
136 |
))
|
|
|
137 |
->add_joins($this->get_joins())
|
|
|
138 |
->set_type(column::TYPE_TEXT)
|
|
|
139 |
->add_field("{$entityuseralias}.id", 'userid')
|
|
|
140 |
->add_fields(implode(', ', [
|
|
|
141 |
"{$entityservicealias}.id",
|
|
|
142 |
"{$entityservicealias}.shortname",
|
|
|
143 |
]))
|
|
|
144 |
->add_callback(static function($value, \stdClass $row): string {
|
|
|
145 |
global $OUTPUT;
|
|
|
146 |
|
|
|
147 |
$missingcapabilities = self::get_missing_capabilities((int) $row->userid, (int) $row->id, (string) $row->shortname);
|
|
|
148 |
if (empty($missingcapabilities)) {
|
|
|
149 |
return '';
|
|
|
150 |
}
|
|
|
151 |
$missingcapabilities = array_map(function($missingcapability) {
|
|
|
152 |
return (object)[
|
|
|
153 |
'name' => $missingcapability,
|
|
|
154 |
'link' => get_capability_docs_link((object)['name' => $missingcapability]),
|
|
|
155 |
];
|
|
|
156 |
}, $missingcapabilities);
|
|
|
157 |
return $OUTPUT->render_from_template('core_webservice/missing_capabilities', [
|
|
|
158 |
'missingcapabilities' => $missingcapabilities,
|
|
|
159 |
'helpicon' => $OUTPUT->help_icon('missingcaps', 'webservice'),
|
|
|
160 |
]);
|
|
|
161 |
})
|
|
|
162 |
);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Adds the filters we want to display in the report
|
|
|
167 |
*
|
|
|
168 |
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
|
|
169 |
* unique identifier
|
|
|
170 |
*/
|
|
|
171 |
protected function add_filters(): void {
|
|
|
172 |
$filters = [
|
|
|
173 |
'token:name',
|
|
|
174 |
'user:fullname',
|
|
|
175 |
'service:name',
|
|
|
176 |
'token:validuntil',
|
|
|
177 |
];
|
|
|
178 |
|
|
|
179 |
$this->add_filters_from_entities($filters);
|
|
|
180 |
|
|
|
181 |
$this->get_filter('user:fullname')
|
|
|
182 |
->set_header(new lang_string('user'));
|
|
|
183 |
$this->get_filter('service:name')
|
|
|
184 |
->set_header(new lang_string('service', 'core_webservice'));
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
|
|
|
189 |
*
|
|
|
190 |
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
|
|
|
191 |
*/
|
|
|
192 |
protected function add_actions(): void {
|
|
|
193 |
|
|
|
194 |
// Action to delete token.
|
|
|
195 |
$this->add_action((new action(
|
|
|
196 |
new moodle_url('/admin/webservice/tokens.php', [
|
|
|
197 |
'action' => 'delete',
|
|
|
198 |
'tokenid' => ':id',
|
|
|
199 |
]),
|
|
|
200 |
new pix_icon('t/delete', '', 'core'),
|
|
|
201 |
['class' => 'text-danger'],
|
|
|
202 |
false,
|
|
|
203 |
new lang_string('delete', 'core')
|
|
|
204 |
)));
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Get the missing capabilities for a user
|
|
|
209 |
*
|
|
|
210 |
* @param int $userid
|
|
|
211 |
* @param int $serviceid
|
|
|
212 |
* @param string $serviceshortname
|
|
|
213 |
* @return array
|
|
|
214 |
*/
|
|
|
215 |
protected static function get_missing_capabilities(int $userid, int $serviceid, string $serviceshortname): array {
|
|
|
216 |
global $CFG;
|
|
|
217 |
require_once($CFG->dirroot . '/webservice/lib.php');
|
|
|
218 |
|
|
|
219 |
$webservicemanager = new \webservice();
|
|
|
220 |
$usermissingcaps = $webservicemanager->get_missing_capabilities_by_users([['id' => $userid]], $serviceid);
|
|
|
221 |
if ($serviceshortname != MOODLE_OFFICIAL_MOBILE_SERVICE && !is_siteadmin($userid)) {
|
|
|
222 |
return $usermissingcaps[$userid] ?? [];
|
|
|
223 |
}
|
|
|
224 |
return [];
|
|
|
225 |
}
|
|
|
226 |
}
|