MDL-67748 admin: Add filter to the WS tokens management page
The patch adds ability to filter the list of token by the token value, the user and the service. Also the button to create a new token is made more prominent and easier to spot.
This commit is contained in:
@@ -16,7 +16,7 @@ Feature: Manage tokens
|
||||
@javascript
|
||||
Scenario: Add & delete a token
|
||||
Given I navigate to "Server > Web services > Manage tokens" in site administration
|
||||
And I follow "Add"
|
||||
And I press "Create token"
|
||||
And I set the field "User" to "Joe Bloggs"
|
||||
And I set the field "IP restriction" to "127.0.0.1"
|
||||
When I press "Save changes"
|
||||
|
||||
+44
-13
@@ -31,6 +31,9 @@ require_once($CFG->dirroot . '/webservice/lib.php');
|
||||
$action = optional_param('action', '', PARAM_ALPHANUMEXT);
|
||||
$tokenid = optional_param('tokenid', '', PARAM_SAFEDIR);
|
||||
$confirm = optional_param('confirm', 0, PARAM_BOOL);
|
||||
$ftoken = optional_param('ftoken', '', PARAM_ALPHANUM);
|
||||
$fusers = optional_param_array('fusers', [], PARAM_INT);
|
||||
$fservices = optional_param_array('fservices', [], PARAM_INT);
|
||||
|
||||
admin_externalpage_setup('webservicetokens');
|
||||
|
||||
@@ -52,9 +55,7 @@ if ($action === 'create') {
|
||||
$restricteduser = $webservicemanager->get_ws_authorised_user($data->service, $data->user);
|
||||
|
||||
if (empty($restricteduser)) {
|
||||
$allowuserurl = new moodle_url('/admin/webservice/service_users.php', ['id' => $selectedservice->id]);
|
||||
$allowuserlink = html_writer::link($selectedservice->name, $allowuserurl);
|
||||
$errormsg = $OUTPUT->notification(get_string('usernotallowed', 'webservice', $allowuserlink));
|
||||
$errormsg = $OUTPUT->notification(get_string('usernotallowed', 'webservice', $selectedservice->name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,21 +113,51 @@ if ($action === 'delete') {
|
||||
die();
|
||||
}
|
||||
|
||||
// Pre-populate the form with the values that come as a part of the URL - typically when using the table_sql control
|
||||
// links.
|
||||
$filterdata = (object)[
|
||||
'token' => $ftoken,
|
||||
'users' => $fusers,
|
||||
'services' => $fservices,
|
||||
];
|
||||
|
||||
$filter = new \core_webservice\token_filter($PAGE->url, $filterdata);
|
||||
|
||||
$filter->set_data($filterdata);
|
||||
|
||||
if ($filter->is_submitted()) {
|
||||
$filterdata = $filter->get_data();
|
||||
|
||||
if (isset($filterdata->resetbutton)) {
|
||||
redirect($PAGE->url);
|
||||
}
|
||||
}
|
||||
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading(get_string('managetokens', 'core_webservice'));
|
||||
|
||||
$table = new \core_webservice\token_table('webservicetokens');
|
||||
$table->define_baseurl($PAGE->url);
|
||||
echo html_writer::div($OUTPUT->render(new single_button(new moodle_url($PAGE->url, ['action' => 'create']),
|
||||
get_string('createtoken', 'core_webservice'), 'get', true)), 'my-3');
|
||||
|
||||
$filter->display();
|
||||
|
||||
$table = new \core_webservice\token_table('webservicetokens', $filterdata);
|
||||
|
||||
// In order to not lose the filter form values by clicking the table control links, make them part of the table's baseurl.
|
||||
$baseurl = new moodle_url($PAGE->url, ['ftoken' => $filterdata->token]);
|
||||
|
||||
foreach ($filterdata->users as $i => $userid) {
|
||||
$baseurl->param("fusers[{$i}]", $userid);
|
||||
}
|
||||
|
||||
foreach ($filterdata->services as $i => $serviceid) {
|
||||
$baseurl->param("fservices[{$i}]", $serviceid);
|
||||
}
|
||||
|
||||
$table->define_baseurl($baseurl);
|
||||
|
||||
$table->attributes['class'] = 'admintable generaltable';
|
||||
$table->data = [];
|
||||
$table->out(30, false);
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
|
||||
// TODO Add button
|
||||
//$tokenpageurl = "$CFG->wwwroot/$CFG->admin/webservice/tokens.php?sesskey=" . sesskey();
|
||||
//
|
||||
//$return .= $OUTPUT->box_end();
|
||||
//// add a token to the table
|
||||
//$return .= "<a href=\"".$tokenpageurl."&action=create\">";
|
||||
//$return .= get_string('add')."</a>";
|
||||
|
||||
@@ -205,6 +205,9 @@ $string['token'] = 'Token';
|
||||
$string['tokenauthlog'] = 'Token authentication';
|
||||
$string['tokencreatedbyadmin'] = 'Can only be reset by administrator (*)';
|
||||
$string['tokencreator'] = 'Creator';
|
||||
$string['tokenfilter'] = 'Tokens filter';
|
||||
$string['tokenfiltersubmit'] = 'Show only matching tokens';
|
||||
$string['tokenfilterreset'] = 'Show all tokens';
|
||||
$string['unknownoptionkey'] = 'Unknown option key ({$a})';
|
||||
$string['unnamedstringparam'] = 'A string parameter is unnamed.';
|
||||
$string['updateusersettings'] = 'Update';
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
// This file is part of Moodle - https://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Provides the {@see core_webservice\token_filter} class.
|
||||
*
|
||||
* @package core_webservice
|
||||
* @copyright 2020 David Mudrák <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core_webservice;
|
||||
|
||||
use moodleform;
|
||||
|
||||
/**
|
||||
* Form allowing to filter displayed tokens.
|
||||
*
|
||||
* @copyright 2020 David Mudrák <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class token_filter extends moodleform {
|
||||
|
||||
/**
|
||||
* Defines the form fields.
|
||||
*/
|
||||
public function definition() {
|
||||
global $DB;
|
||||
|
||||
$mform = $this->_form;
|
||||
$presetdata = $this->_customdata;
|
||||
|
||||
$mform->addElement('header', 'tokenfilter', get_string('tokenfilter', 'webservice'));
|
||||
|
||||
if (empty($presetdata->token) && empty($presetdata->users) && empty($presetdata->services)) {
|
||||
$mform->setExpanded('tokenfilter', false);
|
||||
} else {
|
||||
$mform->setExpanded('tokenfilter', true);
|
||||
}
|
||||
|
||||
// Token.
|
||||
$mform->addElement('text', 'token', get_string('token', 'core_webservice'), ['size' => 32]);
|
||||
$mform->setType('token', PARAM_ALPHANUM);
|
||||
|
||||
// User selector.
|
||||
$attributes = [
|
||||
'multiple' => true,
|
||||
'ajax' => 'core_user/form_user_selector',
|
||||
'valuehtmlcallback' => function($userid) {
|
||||
global $DB, $OUTPUT;
|
||||
|
||||
$context = \context_system::instance();
|
||||
$fields = \core\user_fields::for_name()->with_identity($context, false);
|
||||
$record = \core_user::get_user($userid, 'id' . $fields->get_sql()->selects, MUST_EXIST);
|
||||
|
||||
$user = (object)[
|
||||
'id' => $record->id,
|
||||
'fullname' => fullname($record, has_capability('moodle/site:viewfullnames', $context)),
|
||||
'extrafields' => [],
|
||||
];
|
||||
|
||||
foreach ($fields->get_required_fields([\core\user_fields::PURPOSE_IDENTITY]) as $extrafield) {
|
||||
$user->extrafields[] = (object)[
|
||||
'name' => $extrafield,
|
||||
'value' => s($record->$extrafield)
|
||||
];
|
||||
}
|
||||
|
||||
return $OUTPUT->render_from_template('core_user/form_user_selector_suggestion', $user);
|
||||
},
|
||||
];
|
||||
$mform->addElement('autocomplete', 'users', get_string('user'), [], $attributes);
|
||||
|
||||
// Service selector.
|
||||
$options = $DB->get_records_menu('external_services', null, '', 'id, name');
|
||||
$attributes = [
|
||||
'multiple' => true,
|
||||
];
|
||||
$mform->addElement('autocomplete', 'services', get_string('service', 'webservice'), $options, $attributes);
|
||||
|
||||
// Action buttons.
|
||||
$mform->addGroup([
|
||||
$mform->createElement('submit', 'submitbutton', get_string('tokenfiltersubmit', 'core_webservice')),
|
||||
$mform->createElement('submit', 'resetbutton', get_string('tokenfilterreset', 'core_webservice'), [], false),
|
||||
], 'actionbuttons', '', ' ', false);
|
||||
}
|
||||
}
|
||||
@@ -47,11 +47,16 @@ class token_table extends \table_sql {
|
||||
/** @var bool $hasviewfullnames Does the user have the viewfullnames capability. */
|
||||
protected $hasviewfullnames;
|
||||
|
||||
/** @var object */
|
||||
protected $filterdata;
|
||||
|
||||
/**
|
||||
* Sets up the table.
|
||||
*
|
||||
* @param int $id The id of the table
|
||||
* @param object $filterdata The data submitted by the {@see token_filter}.
|
||||
*/
|
||||
public function __construct($id) {
|
||||
public function __construct($id, $filterdata = null) {
|
||||
parent::__construct($id);
|
||||
|
||||
// Get the context.
|
||||
@@ -61,6 +66,9 @@ class token_table extends \table_sql {
|
||||
$this->showalltokens = has_capability('moodle/webservice:managealltokens', $context);
|
||||
$this->hasviewfullnames = has_capability('moodle/site:viewfullnames', $context);
|
||||
|
||||
// Filter form values.
|
||||
$this->filterdata = $filterdata;
|
||||
|
||||
// Define the headers and columns.
|
||||
$headers = [];
|
||||
$columns = [];
|
||||
@@ -227,41 +235,55 @@ class token_table extends \table_sql {
|
||||
$usernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
|
||||
$creatorfields = $userfieldsapi->get_sql('c', false, 'creator', '', false)->selects;
|
||||
|
||||
$params = ["tokenmode" => EXTERNAL_TOKEN_PERMANENT];
|
||||
$params = ['tokenmode' => EXTERNAL_TOKEN_PERMANENT];
|
||||
|
||||
// TODO: in order to let the administrator delete obsolete token, split the request in multiple request or use LEFT JOIN.
|
||||
$selectfields = "SELECT t.id, t.token, t.iprestriction, t.validuntil, t.creatorid,
|
||||
u.id AS userid, $usernamefields,
|
||||
s.id AS serviceid, s.name,
|
||||
$creatorfields ";
|
||||
|
||||
if ($this->showalltokens) {
|
||||
// Show all tokens.
|
||||
$sql = "SELECT t.id, t.token, u.id AS userid, $usernamefields, s.name, t.iprestriction, t.validuntil, s.id AS serviceid,
|
||||
t.creatorid, $creatorfields
|
||||
FROM {external_tokens} t, {user} u, {external_services} s, {user} c
|
||||
WHERE t.tokentype = :tokenmode AND s.id = t.externalserviceid AND t.userid = u.id AND c.id = t.creatorid";
|
||||
$countsql = "SELECT COUNT(t.id)
|
||||
FROM {external_tokens} t, {user} u, {external_services} s, {user} c
|
||||
WHERE t.tokentype = :tokenmode AND s.id = t.externalserviceid AND t.userid = u.id AND c.id = t.creatorid";
|
||||
} else {
|
||||
$selectcount = "SELECT COUNT(t.id) ";
|
||||
|
||||
$sql = " FROM {external_tokens} t
|
||||
JOIN {user} u ON u.id = t.userid
|
||||
JOIN {external_services} s ON s.id = t.externalserviceid
|
||||
JOIN {user} c ON c.id = t.creatorid
|
||||
WHERE t.tokentype = :tokenmode";
|
||||
|
||||
if (!$this->showalltokens) {
|
||||
// Only show tokens created by the current user.
|
||||
$sql = "SELECT t.id, t.token, u.id AS userid, $usernamefields, s.name, t.iprestriction, t.validuntil, s.id AS serviceid,
|
||||
t.creatorid, $creatorfields
|
||||
FROM {external_tokens} t, {user} u, {external_services} s, {user} c
|
||||
WHERE t.creatorid=:userid AND t.tokentype = :tokenmode AND s.id = t.externalserviceid AND t.userid = u.id AND
|
||||
c.id = t.creatorid";
|
||||
$countsql = "SELECT COUNT(t.id)
|
||||
FROM {external_tokens} t, {user} u, {external_services} s, {user} c
|
||||
WHERE t.creatorid=:userid AND t.tokentype = :tokenmode AND s.id = t.externalserviceid AND
|
||||
t.userid = u.id AND c.id = t.creatorid";
|
||||
$params["userid"] = $USER->id;
|
||||
$sql .= " AND t.creatorid = :userid";
|
||||
$params['userid'] = $USER->id;
|
||||
}
|
||||
|
||||
if ($this->filterdata->token !== '') {
|
||||
$sql .= " AND " . $DB->sql_like("t.token", ":token");
|
||||
$params['token'] = "%" . $DB->sql_like_escape($this->filterdata->token) . "%";
|
||||
}
|
||||
|
||||
if (!empty($this->filterdata->users)) {
|
||||
list($sqlusers, $paramsusers) = $DB->get_in_or_equal($this->filterdata->users, SQL_PARAMS_NAMED, 'user');
|
||||
$sql .= " AND t.userid {$sqlusers}";
|
||||
$params += $paramsusers;
|
||||
}
|
||||
|
||||
if (!empty($this->filterdata->services)) {
|
||||
list($sqlservices, $paramsservices) = $DB->get_in_or_equal($this->filterdata->services, SQL_PARAMS_NAMED, 'service');
|
||||
$sql .= " AND s.id {$sqlservices}";
|
||||
$params += $paramsservices;
|
||||
}
|
||||
|
||||
$sort = $this->get_sql_sort();
|
||||
$sortsql = '';
|
||||
|
||||
if ($sort) {
|
||||
$sql = $sql . ' ORDER BY ' . $sort;
|
||||
$sortsql = " ORDER BY {$sort}";
|
||||
}
|
||||
|
||||
$total = $DB->count_records_sql($countsql, $params);
|
||||
$total = $DB->count_records_sql($selectcount . $sql, $params);
|
||||
$this->pagesize($pagesize, $total);
|
||||
|
||||
$this->rawdata = $DB->get_recordset_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
|
||||
$this->rawdata = $DB->get_recordset_sql($selectfields . $sql . $sortsql, $params, $this->get_page_start(),
|
||||
$this->get_page_size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user