Extract dependencies from Authentication middleware
Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
This commit is contained in:
parent
b0cab49eee
commit
7bfb1dde0f
@ -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,
|
||||
|
||||
@ -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\:
|
||||
|
||||
@ -4852,15 +4852,6 @@
|
||||
<code><![CDATA[empty($server['ssl_ca'])]]></code>
|
||||
</RiskyTruthyFalsyComparison>
|
||||
</file>
|
||||
<file src="src/Http/Middleware/Authentication.php">
|
||||
<DeprecatedMethod>
|
||||
<code><![CDATA[Config::getInstance()]]></code>
|
||||
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
|
||||
</DeprecatedMethod>
|
||||
<PossiblyUnusedMethod>
|
||||
<code><![CDATA[__construct]]></code>
|
||||
</PossiblyUnusedMethod>
|
||||
</file>
|
||||
<file src="src/Http/Middleware/ConfigErrorAndPermissionChecking.php">
|
||||
<PossiblyUnusedMethod>
|
||||
<code><![CDATA[__construct]]></code>
|
||||
|
||||
@ -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 */
|
||||
|
||||
47
tests/unit/Http/Middleware/AuthenticationTest.php
Normal file
47
tests/unit/Http/Middleware/AuthenticationTest.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Tests\Http\Middleware;
|
||||
|
||||
use PhpMyAdmin\Config;
|
||||
use PhpMyAdmin\ConfigStorage\Relation;
|
||||
use PhpMyAdmin\Http\Factory\ResponseFactory;
|
||||
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
|
||||
use PhpMyAdmin\Http\Middleware\Authentication;
|
||||
use PhpMyAdmin\Plugins\AuthenticationPluginFactory;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Tests\AbstractTestCase;
|
||||
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
#[CoversClass(Authentication::class)]
|
||||
final class AuthenticationTest extends AbstractTestCase
|
||||
{
|
||||
public function testAuthenticationWithoutSelectServer(): void
|
||||
{
|
||||
$configMock = self::createMock(Config::class);
|
||||
$configMock->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));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user