From 7bfb1dde0fea47f6a89f3f9a06c3533c89ea27f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 23 Dec 2025 20:15:27 -0300 Subject: [PATCH] Extract dependencies from Authentication middleware MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- app/services.php | 16 +++++-- phpstan-baseline.neon | 18 ------- psalm-baseline.xml | 9 ---- src/Http/Middleware/Authentication.php | 25 +++++----- .../Http/Middleware/AuthenticationTest.php | 47 +++++++++++++++++++ 5 files changed, 72 insertions(+), 43 deletions(-) create mode 100644 tests/unit/Http/Middleware/AuthenticationTest.php diff --git a/app/services.php b/app/services.php index 57491a6504..79937b6743 100644 --- a/app/services.php +++ b/app/services.php @@ -192,7 +192,15 @@ return [ ], Middleware\Authentication::class => [ 'class' => Middleware\Authentication::class, - 'arguments' => ['@config', '@template', '@' . ResponseFactory::class], + 'arguments' => [ + '@config', + '@template', + '@' . ResponseFactory::class, + '@' . AuthenticationPluginFactory::class, + '@dbi', + '@relation', + '@response', + ], ], Middleware\DatabaseServerVersionChecking::class => [ 'class' => Middleware\DatabaseServerVersionChecking::class, @@ -252,7 +260,7 @@ return [ ], 'response' => [ 'class' => ResponseRenderer::class, - 'factory' => [PhpMyAdmin\ResponseRenderer::class, 'getInstance'], + 'factory' => [ResponseRenderer::class, 'getInstance'], ], 'routines' => ['class' => Routines::class, 'arguments' => ['@dbi']], 'server_plugins' => ['class' => Plugins::class, 'arguments' => ['@dbi']], @@ -295,7 +303,7 @@ return [ 'class' => Indexes::class, 'arguments' => ['$dbi' => '@dbi'], ], - 'table_maintenance' => ['class' => PhpMyAdmin\Table\Maintenance::class, 'arguments' => ['$dbi' => '@dbi']], + 'table_maintenance' => ['class' => \PhpMyAdmin\Table\Maintenance::class, 'arguments' => ['$dbi' => '@dbi']], 'table_search' => ['class' => Search::class, 'arguments' => ['$dbi' => '@dbi']], Template::class => ['class' => Template::class, 'arguments' => ['$config' => '@config']], 'template' => Template::class, @@ -325,7 +333,7 @@ return [ UserPrivilegesFactory::class => ['class' => UserPrivilegesFactory::class, 'arguments' => ['@dbi']], 'version_information' => ['class' => VersionInformation::class], DatabaseInterface::class => 'dbi', - PhpMyAdmin\ResponseRenderer::class => 'response', + ResponseRenderer::class => 'response', 'bookmarkRepository' => ['class' => BookmarkRepository::class, 'arguments' => ['@dbi', '@relation']], Console::class => [ 'class' => Console::class, diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2618138822..fec73d7a93 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -7272,24 +7272,6 @@ parameters: count: 1 path: src/Http/Factory/ServerRequestFactory.php - - - message: ''' - #^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Config\: - Use dependency injection instead\.$# - ''' - identifier: staticMethod.deprecated - count: 1 - path: src/Http/Middleware/Authentication.php - - - - message: ''' - #^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Dbal\\DatabaseInterface\: - Use dependency injection instead\.$# - ''' - identifier: staticMethod.deprecated - count: 1 - path: src/Http/Middleware/Authentication.php - - message: ''' #^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Dbal\\DatabaseInterface\: diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 5ec38c2ac2..0ed509b4bf 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -4852,15 +4852,6 @@ - - - - - - - - - diff --git a/src/Http/Middleware/Authentication.php b/src/Http/Middleware/Authentication.php index ea4f595a17..2af5e39c81 100644 --- a/src/Http/Middleware/Authentication.php +++ b/src/Http/Middleware/Authentication.php @@ -8,7 +8,6 @@ use Fig\Http\Message\StatusCodeInterface; use PhpMyAdmin\Config; use PhpMyAdmin\Config\Settings\Server; use PhpMyAdmin\ConfigStorage\Relation; -use PhpMyAdmin\Container\ContainerBuilder; use PhpMyAdmin\Current; use PhpMyAdmin\Dbal\ConnectionType; use PhpMyAdmin\Dbal\DatabaseInterface; @@ -31,12 +30,16 @@ use Throwable; use function assert; use function define; -final class Authentication implements MiddlewareInterface +final readonly class Authentication implements MiddlewareInterface { public function __construct( - private readonly Config $config, - private readonly Template $template, - private readonly ResponseFactory $responseFactory, + private Config $config, + private Template $template, + private ResponseFactory $responseFactory, + private AuthenticationPluginFactory $authPluginFactory, + private DatabaseInterface $dbi, + private Relation $relation, + private ResponseRenderer $responseRenderer, ) { } @@ -46,9 +49,8 @@ final class Authentication implements MiddlewareInterface return $handler->handle($request); } - $authPluginFactory = ContainerBuilder::getContainer()->get(AuthenticationPluginFactory::class); try { - $authPlugin = $authPluginFactory->create(); + $authPlugin = $this->authPluginFactory->create(); } catch (AuthenticationPluginException $exception) { $response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR); @@ -75,7 +77,7 @@ final class Authentication implements MiddlewareInterface ])); } - $currentServer = new Server(Config::getInstance()->selectedServer); + $currentServer = new Server($this->config->selectedServer); /* Enable LOAD DATA LOCAL INFILE for LDI plugin */ if ($request->getAttribute('route') === '/import' && ($_POST['format'] ?? '') === 'ldi') { @@ -86,14 +88,13 @@ final class Authentication implements MiddlewareInterface } try { - $this->connectToDatabaseServer(DatabaseInterface::getInstance(), $currentServer); + $this->connectToDatabaseServer($this->dbi, $currentServer); } catch (AuthenticationFailure $exception) { return $authPlugin->showFailure($exception); } // Relation should only be initialized after the connection is successful - $relation = ContainerBuilder::getContainer()->get(Relation::class); - $relation->initRelationParamsCache(); + $this->relation->initRelationParamsCache(); // Tracker can only be activated after the relation has been initialized Tracker::enable(); @@ -109,7 +110,7 @@ final class Authentication implements MiddlewareInterface return $response; } } catch (ExitException) { - return ResponseRenderer::getInstance()->response(); + return $this->responseRenderer->response(); } /* Log success */ diff --git a/tests/unit/Http/Middleware/AuthenticationTest.php b/tests/unit/Http/Middleware/AuthenticationTest.php new file mode 100644 index 0000000000..ac04c94377 --- /dev/null +++ b/tests/unit/Http/Middleware/AuthenticationTest.php @@ -0,0 +1,47 @@ +expects(self::once())->method('hasSelectedServer')->willReturn(false); + + $dbi = $this->createDatabaseInterface(); + $config = new Config(); + $authentication = new Authentication( + $configMock, + new Template($config), + ResponseFactory::create(), + new AuthenticationPluginFactory(), + $dbi, + new Relation($dbi, $config), + new ResponseRenderer(), + ); + + $response = self::createStub(ResponseInterface::class); + $handler = self::createStub(RequestHandlerInterface::class); + $handler->method('handle')->willReturn($response); + $request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/'); + + self::assertSame($response, $authentication->process($request, $handler)); + } +}