Extract ResponseRenderer dependency from Privileges class

Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
This commit is contained in:
Maurício Meneghini Fauth 2026-02-26 15:34:21 -03:00
parent 7b759b46a2
commit 495a4d031e
No known key found for this signature in database
GPG Key ID: 6A16FD38AFC89CC8
6 changed files with 54 additions and 49 deletions

View File

@ -11043,7 +11043,7 @@ parameters:
-
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
identifier: empty.notAllowed
count: 13
count: 12
path: src/Server/Privileges.php
-
@ -11079,7 +11079,7 @@ parameters:
-
message: '#^Only booleans are allowed in a negated boolean, PhpMyAdmin\\Dbal\\ResultInterface\|false given\.$#'
identifier: booleanNot.exprNotBoolean
count: 13
count: 11
path: src/Server/Privileges.php
-

View File

@ -7555,7 +7555,6 @@
<code><![CDATA[empty($_POST['pma_pw'])]]></code>
<code><![CDATA[empty($_POST['pma_pw2'])]]></code>
<code><![CDATA[empty($_POST['userGroup'])]]></code>
<code><![CDATA[empty($_REQUEST['ajax_page_request'])]]></code>
<code><![CDATA[empty($row['password'])]]></code>
<code><![CDATA[empty($row['password'])]]></code>
</RiskyTruthyFalsyComparison>

View File

@ -10,6 +10,8 @@ use PhpMyAdmin\ConfigStorage\RelationCleanup;
use PhpMyAdmin\Controllers\InvocableController;
use PhpMyAdmin\Current;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Exceptions\UpdateAuthPluginFailure;
use PhpMyAdmin\Exceptions\UserPasswordUpdateFailure;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
@ -262,11 +264,23 @@ final class PrivilegesController implements InvocableController
* Updates the password
*/
if ($request->hasBodyParam('change_pw')) {
Current::$message = $serverPrivileges->updatePassword(
$errorUrl,
$serverPrivileges->username ?? '',
$serverPrivileges->hostname ?? '',
);
try {
Current::$message = $serverPrivileges->updatePassword(
$serverPrivileges->username ?? '',
$serverPrivileges->hostname ?? '',
);
} catch (UpdateAuthPluginFailure | UserPasswordUpdateFailure $exception) {
if ($request->isAjax()) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', $exception->getMessage());
return $this->response->response();
}
$this->response->addHTML($exception->getMessage() . Generator::getBackUrlHtml($errorUrl));
return $this->response->response();
}
}
/**
@ -392,6 +406,8 @@ final class PrivilegesController implements InvocableController
$this->response->addHTML($serverPrivileges->getHtmlForUserOverview(
$userPrivileges,
$request->getQueryParam('initial'),
$request->isAjax(),
$request->has('ajax_page_request'),
));
} elseif ($routinename !== '') {
$this->response->addHTML(

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Exceptions;
use RuntimeException;
final class UpdateAuthPluginFailure extends RuntimeException
{
}

View File

@ -16,6 +16,8 @@ use PhpMyAdmin\Database\Routines;
use PhpMyAdmin\Dbal\ConnectionType;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Dbal\ResultInterface;
use PhpMyAdmin\Exceptions\UpdateAuthPluginFailure;
use PhpMyAdmin\Exceptions\UserPasswordUpdateFailure;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Html\MySQLDocumentation;
use PhpMyAdmin\Http\ServerRequest;
@ -748,13 +750,9 @@ class Privileges
/**
* Update password and get message for password updating
*
* @param string $errorUrl error url
* @param string $username username
* @param string $hostname hostname
*
* @return Message success or error message after updating password
*/
public function updatePassword(string $errorUrl, string $username, string $hostname): Message
public function updatePassword(string $username, string $hostname): Message
{
// similar logic in /user-password
$message = null;
@ -841,22 +839,12 @@ class Privileges
. $this->getUserHostCondition($username, $hostname) . ';';
// Update the plugin for the user
if (! $this->dbi->tryQuery($updatePluginQuery)) {
$errorMessage = Generator::mysqlDie(
if ($this->dbi->tryQuery($updatePluginQuery) === false) {
throw new UpdateAuthPluginFailure(Generator::mysqlDie(
$this->dbi->getError(),
$updatePluginQuery,
false,
);
$response = ResponseRenderer::getInstance();
if ($response->isAjax()) {
$response->setRequestStatus(false);
$response->addJSON('message', $errorMessage);
$response->callExit();
}
$response->addHTML($errorMessage . Generator::getBackUrlHtml($errorUrl));
$response->callExit();
));
}
$this->dbi->tryQuery('FLUSH PRIVILEGES;');
@ -886,22 +874,12 @@ class Privileges
. '(' . $this->dbi->quoteString($_POST['pma_pw']) . ')');
}
if (! $this->dbi->tryQuery($localQuery)) {
$errorMessage = Generator::mysqlDie(
if ($this->dbi->tryQuery($localQuery) === false) {
throw new UserPasswordUpdateFailure(Generator::mysqlDie(
$this->dbi->getError(),
$sqlQuery,
false,
);
$response = ResponseRenderer::getInstance();
if ($response->isAjax()) {
$response->setRequestStatus(false);
$response->addJSON('message', $errorMessage);
$response->callExit();
}
$response->addHTML($errorMessage . Generator::getBackUrlHtml($errorUrl));
$response->callExit();
));
}
// Flush privileges after successful password change
@ -2456,8 +2434,12 @@ class Privileges
/**
* Get HTML snippet for display user overview page
*/
public function getHtmlForUserOverview(UserPrivileges $userPrivileges, string|null $initial): string
{
public function getHtmlForUserOverview(
UserPrivileges $userPrivileges,
string|null $initial,
bool $isAjax,
bool $isAjaxPageRequest,
): string {
$serverVersion = $this->dbi->getVersion();
$passwordColumn = Compatibility::isMySqlOrPerconaDb($this->dbi) && $serverVersion >= 50706
? 'authentication_string'
@ -2481,8 +2463,7 @@ class Privileges
$this->template->render('export_modal');
}
$response = ResponseRenderer::getInstance();
if (! $response->isAjax() || ! empty($_REQUEST['ajax_page_request'])) {
if (! $isAjax || $isAjaxPageRequest) {
if ($userPrivileges->isReload) {
$flushnote = new Message(
__(

View File

@ -417,11 +417,10 @@ class PrivilegesTest extends AbstractTestCase
$username = 'pma_username';
$hostname = 'pma_hostname';
$errUrl = 'error.php';
$_POST['pma_pw'] = 'pma_pw';
$_POST['authentication_plugin'] = 'mysql_native_password';
$message = $serverPrivileges->updatePassword($errUrl, $username, $hostname);
$message = $serverPrivileges->updatePassword($username, $hostname);
self::assertSame(
'The password for \'pma_username\'@\'pma_hostname\' was changed successfully.',
@ -1570,8 +1569,7 @@ class PrivilegesTest extends AbstractTestCase
$serverPrivileges = $this->getPrivileges($this->createDatabaseInterface($dummyDbi));
$_REQUEST = ['ajax_page_request' => '1'];
$actual = $serverPrivileges->getHtmlForUserOverview($userPrivileges, null);
$actual = $serverPrivileges->getHtmlForUserOverview($userPrivileges, null, false, true);
$dummyDbi->assertAllQueriesConsumed();
self::assertStringContainsString('Note: MySQL privilege names are expressed in English.', $actual);
self::assertStringContainsString(
@ -1592,7 +1590,7 @@ class PrivilegesTest extends AbstractTestCase
);
$dummyDbi->addResult('SELECT 1 FROM `mysql`.`user`', false);
$serverPrivileges = $this->getPrivileges($this->createDatabaseInterface($dummyDbi));
$html = $serverPrivileges->getHtmlForUserOverview($userPrivileges, null);
$html = $serverPrivileges->getHtmlForUserOverview($userPrivileges, null, false, true);
self::assertStringContainsString(
Url::getCommon(['adduser' => 1], ''),
@ -1620,7 +1618,7 @@ class PrivilegesTest extends AbstractTestCase
);
$dummyDbi->addResult('SELECT 1 FROM `mysql`.`user`', [[1]]);
$serverPrivileges = $this->getPrivileges($this->createDatabaseInterface($dummyDbi));
$actual = $serverPrivileges->getHtmlForUserOverview($userPrivileges, null);
$actual = $serverPrivileges->getHtmlForUserOverview($userPrivileges, null, false, true);
self::assertStringContainsString('Your privilege table structure seems to be older than'
. ' this MySQL version!<br>'