Fix user pagination for 'Any' user

If the user name is empty any user may login from this host.

This fixes the pagination to display 'Any' instead of an empty string
and makes filtering with this link work.

Signed-off-by: Maximilian Krög <maxi_kroeg@web.de>
This commit is contained in:
Maximilian Krög 2023-09-02 23:36:41 +02:00
parent bf17145c70
commit d43f16f616
No known key found for this signature in database
GPG Key ID: 3C00897BB53AAB9C
3 changed files with 35 additions and 14 deletions

View File

@ -51,6 +51,7 @@ use function sprintf;
use function str_contains;
use function str_replace;
use function strlen;
use function strtr;
use function trim;
use function uksort;
@ -132,16 +133,20 @@ class Privileges
*
* @return string the generated condition
*/
public function rangeOfUsers($initial = '')
public function rangeOfUsers(?string $initial = null)
{
// strtolower() is used because the User field
// might be BINARY, so LIKE would be case sensitive
if ($initial === null || $initial === '') {
if ($initial === null) {
return '';
}
if ($initial === '') {
return " WHERE `User` = ''";
}
$like = strtr($initial, ['_' => '\\_', '%' => '\\%', '\\' => '\\\\']) . '%';
// strtolower() is used because the User field
// might be BINARY, so LIKE would be case sensitive
return " WHERE `User` LIKE '"
. $this->dbi->escapeString($like) . "'"
. " OR `User` LIKE '"

View File

@ -4,7 +4,13 @@
{% if tmp_initial is not same as(null) %}
{% if initial_was_found %}
<li class="page-item{{ initial is same as(tmp_initial) ? ' active' }}"{{ initial is same as(tmp_initial) ? ' aria-current="page"' }}>
<a class="page-link" href="{{ url('/server/privileges', {'viewing_mode': viewing_mode, 'initial': tmp_initial}) }}">{{ tmp_initial }}</a>
<a class="page-link" href="{{ url('/server/privileges', {'viewing_mode': viewing_mode, 'initial': tmp_initial}) }}">
{% if tmp_initial is same as('') %}
<span class="text-danger text-nowrap">{{ 'Any' |trans }}</span>
{% else %}
{{ tmp_initial }}
{% endif %}
</a>
</li>
{% else %}
<li class="page-item disabled">

View File

@ -28,6 +28,7 @@ use function __;
use function _pgettext;
use function htmlspecialchars;
use function implode;
use function preg_quote;
/**
* @covers \PhpMyAdmin\Server\Privileges
@ -244,6 +245,9 @@ class PrivilegesTest extends AbstractTestCase
$ret = $this->serverPrivileges->rangeOfUsers('%');
$this->assertEquals(' WHERE `User` LIKE \'\\%%\' OR `User` LIKE \'\\%%\'', $ret);
$ret = $this->serverPrivileges->rangeOfUsers('');
$this->assertEquals(" WHERE `User` = ''", $ret);
$ret = $this->serverPrivileges->rangeOfUsers();
$this->assertEquals('', $ret);
}
@ -1684,7 +1688,7 @@ class PrivilegesTest extends AbstractTestCase
->will($this->returnValue($resultStub));
$resultStub->expects($this->atLeastOnce())
->method('fetchRow')
->will($this->onConsecutiveCalls(['-'], ['"'], ['%'], ['\\'], []));
->will($this->onConsecutiveCalls(['-'], ['"'], ['%'], ['\\'], [''], []));
$this->serverPrivileges->dbi = $dbi;
$actual = $this->serverPrivileges->getHtmlForInitials();
@ -1696,20 +1700,26 @@ class PrivilegesTest extends AbstractTestCase
'<a class="page-link" href="#" tabindex="-1" aria-disabled="true">Z</a>',
$actual
);
$this->assertStringContainsString(
'<a class="page-link" href="index.php?route=/server/privileges&initial=-&lang=en">-</a>',
$this->assertMatchesRegularExpression(
'/<a class="page-link" href="index.php\?route=\/server\/privileges&initial=-&lang=en">\s*-\s*<\/a>/',
$actual
);
$this->assertStringContainsString(
'<a class="page-link" href="index.php?route=/server/privileges&initial=%22&lang=en">&quot;</a>',
$this->assertMatchesRegularExpression(
'/<a class="page-link" href="index.php\?route=\/server\/privileges&initial=%22&lang=en">\s*&quot;\s*<\/a>/',
$actual
);
$this->assertStringContainsString(
'<a class="page-link" href="index.php?route=/server/privileges&initial=%25&lang=en">%</a>',
$this->assertMatchesRegularExpression(
'/<a class="page-link" href="index.php\?route=\/server\/privileges&initial=%25&lang=en">\s*%\s*<\/a>/',
$actual
);
$this->assertStringContainsString(
'<a class="page-link" href="index.php?route=/server/privileges&initial=%5C&lang=en">\\</a>',
$this->assertMatchesRegularExpression(
'/<a class="page-link" href="index.php\?route=\/server\/privileges&initial=%5C&lang=en">\s*\\\\\s*<\/a>/',
$actual
);
$this->assertMatchesRegularExpression(
'/<a class="page-link" href="index.php\?route=\/server\/privileges&initial=&lang=en">\s*' .
'<span class="text-danger text-nowrap">' . preg_quote(__('Any')) . '<\/span>' .
'\s*<\/a>/',
$actual
);
$this->assertStringContainsString('Show all', $actual);