From d43f16f616ca61fa231fef4881ca63ef766aec4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Sat, 2 Sep 2023 23:36:41 +0200 Subject: [PATCH] Fix user pagination for 'Any' user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- libraries/classes/Server/Privileges.php | 13 ++++++--- templates/server/privileges/initials_row.twig | 8 +++++- test/classes/Server/PrivilegesTest.php | 28 +++++++++++++------ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/libraries/classes/Server/Privileges.php b/libraries/classes/Server/Privileges.php index 70e985f4e7..44c9dc0f32 100644 --- a/libraries/classes/Server/Privileges.php +++ b/libraries/classes/Server/Privileges.php @@ -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 '" diff --git a/templates/server/privileges/initials_row.twig b/templates/server/privileges/initials_row.twig index 622310d65b..40315e66d1 100644 --- a/templates/server/privileges/initials_row.twig +++ b/templates/server/privileges/initials_row.twig @@ -4,7 +4,13 @@ {% if tmp_initial is not same as(null) %} {% if initial_was_found %}
  • - {{ tmp_initial }} + + {% if tmp_initial is same as('') %} + {{ 'Any' |trans }} + {% else %} + {{ tmp_initial }} + {% endif %} +
  • {% else %}
  • diff --git a/test/classes/Server/PrivilegesTest.php b/test/classes/Server/PrivilegesTest.php index 52df46922b..b028cfaf51 100644 --- a/test/classes/Server/PrivilegesTest.php +++ b/test/classes/Server/PrivilegesTest.php @@ -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 'Z', $actual ); - $this->assertStringContainsString( - '-', + $this->assertMatchesRegularExpression( + '/\s*-\s*<\/a>/', $actual ); - $this->assertStringContainsString( - '"', + $this->assertMatchesRegularExpression( + '/\s*"\s*<\/a>/', $actual ); - $this->assertStringContainsString( - '%', + $this->assertMatchesRegularExpression( + '/\s*%\s*<\/a>/', $actual ); - $this->assertStringContainsString( - '\\', + $this->assertMatchesRegularExpression( + '/\s*\\\\\s*<\/a>/', + $actual + ); + $this->assertMatchesRegularExpression( + '/\s*' . + '' . preg_quote(__('Any')) . '<\/span>' . + '\s*<\/a>/', $actual ); $this->assertStringContainsString('Show all', $actual);