diff --git a/admin/tests/behat/manage_tokens.feature b/admin/tests/behat/manage_tokens.feature index 1544f7f3428..bc3e3dc7f5d 100644 --- a/admin/tests/behat/manage_tokens.feature +++ b/admin/tests/behat/manage_tokens.feature @@ -24,8 +24,9 @@ Feature: Manage external services tokens And I set the field "Service" to "Moodle mobile web service" And I set the field "IP restriction" to "127.0.0.1" When I press "Save changes" - Then I should see "Firstname1 Lastname1" in the "Webservice1" "table_row" - And I should see "127.0.0.1" in the "Webservice1" "table_row" + Then the following should exist in the "generaltable" table: + | Name | First name | Service | IP restriction | Last access | + | Webservice1 | Firstname1 Lastname1 | Moodle mobile web service | 127.0.0.1 | Never | # Verify the message and the "Copy to clipboard" button. And I should see "Copy the token now. It won't be shown again once you leave this page." diff --git a/webservice/classes/token_table.php b/webservice/classes/token_table.php index 4d0d813aa37..034b48e139d 100644 --- a/webservice/classes/token_table.php +++ b/webservice/classes/token_table.php @@ -89,6 +89,8 @@ class token_table extends \table_sql { $columns[] = 'iprestriction'; $headers[] = get_string('validuntil', 'webservice'); $columns[] = 'validuntil'; + $headers[] = get_string('lastaccess'); + $columns[] = 'lastaccess'; if ($this->showalltokens) { // Only need to show creator if you can see tokens created by other people. $headers[] = get_string('tokencreator', 'webservice'); @@ -138,6 +140,20 @@ class token_table extends \table_sql { } } + /** + * Generate the last access column + * + * @param \stdClass $data + * @return string + */ + public function col_lastaccess(\stdClass $data): string { + if (empty($data->lastaccess)) { + return get_string('never'); + } else { + return userdate($data->lastaccess, get_string('strftimedatetime', 'langconfig')); + } + } + /** * Generate the fullname column. Also includes capabilities the user is missing for the webservice (if any) * @@ -282,7 +298,7 @@ class token_table extends \table_sql { $params = ['tokenmode' => EXTERNAL_TOKEN_PERMANENT]; - $selectfields = "SELECT t.id, t.name, t.iprestriction, t.validuntil, t.creatorid, + $selectfields = "SELECT t.id, t.name, t.iprestriction, t.validuntil, t.creatorid, t.lastaccess, u.id AS userid, $usernamefields, s.id AS serviceid, s.name AS servicename, s.shortname AS serviceshortname, $creatorfields "; diff --git a/webservice/lib.php b/webservice/lib.php index 74b4e1b5519..28bf084cb61 100644 --- a/webservice/lib.php +++ b/webservice/lib.php @@ -397,7 +397,7 @@ class webservice { //here retrieve token list (including linked users firstname/lastname and linked services name) $sql = "SELECT t.id, t.creatorid, t.name as tokenname, u.firstname, u.lastname, - s.id as wsid, s.name as servicename, s.enabled, s.restrictedusers, t.validuntil + s.id as wsid, s.name as servicename, s.enabled, s.restrictedusers, t.validuntil, t.lastaccess FROM {external_tokens} t, {user} u, {external_services} s WHERE diff --git a/webservice/renderer.php b/webservice/renderer.php index 0698d830f8d..734eefcd600 100644 --- a/webservice/renderer.php +++ b/webservice/renderer.php @@ -293,8 +293,8 @@ class core_webservice_renderer extends plugin_renderer_base { $strtoken = get_string('tokenname', 'webservice'); $strservice = get_string('service', 'webservice'); $strcreator = get_string('tokencreator', 'webservice'); - $strcontext = get_string('context', 'webservice'); $strvaliduntil = get_string('validuntil', 'webservice'); + $strlastaccess = get_string('lastaccess'); $return = $this->output->heading(get_string('securitykeys', 'webservice'), 3, 'main', true); $return .= $this->output->box_start('generalbox webservicestokenui'); @@ -302,8 +302,8 @@ class core_webservice_renderer extends plugin_renderer_base { $return .= get_string('keyshelp', 'webservice'); $table = new html_table(); - $table->head = array($strtoken, $strservice, $strvaliduntil, $strcreator, $stroperation); - $table->align = array('left', 'left', 'left', 'center', 'left', 'center'); + $table->head = array($strtoken, $strservice, $strvaliduntil, $strlastaccess, $strcreator, $stroperation); + $table->align = array('left', 'left', 'left', 'center', 'center', 'left', 'center'); $table->width = '100%'; $table->data = array(); @@ -338,11 +338,16 @@ class core_webservice_renderer extends plugin_renderer_base { $validuntil = userdate($token->validuntil, get_string('strftimedatetime', 'langconfig')); } + $lastaccess = ''; + if (!empty($token->lastaccess)) { + $lastaccess = userdate($token->lastaccess, get_string('strftimedatetime', 'langconfig')); + } + $servicename = $token->servicename; if (!$token->enabled) { // That is the (1 token-1ws) related ws is not enabled. $servicename = ''.$token->servicename.''; } - $row = array($token->tokenname, $servicename, $validuntil, $creatoratag, $reset); + $row = array($token->tokenname, $servicename, $validuntil, $lastaccess, $creatoratag, $reset); if ($documentation) { $doclink = new moodle_url('/webservice/wsdoc.php',