Extract dependency from Middleware\TokenRequestParamChecking
Uses DI instead. Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
This commit is contained in:
parent
67cc4ffe5f
commit
55d6f9acb4
@ -177,7 +177,10 @@ return [
|
||||
'class' => Middleware\UrlParamsSetting::class,
|
||||
'arguments' => [Config::class],
|
||||
],
|
||||
Middleware\TokenRequestParamChecking::class => ['class' => Middleware\TokenRequestParamChecking::class],
|
||||
Middleware\TokenRequestParamChecking::class => [
|
||||
'class' => Middleware\TokenRequestParamChecking::class,
|
||||
'arguments' => [ResponseRenderer::class],
|
||||
],
|
||||
Middleware\DatabaseAndTableSetting::class => ['class' => Middleware\DatabaseAndTableSetting::class],
|
||||
Middleware\SqlQueryGlobalSetting::class => ['class' => Middleware\SqlQueryGlobalSetting::class],
|
||||
Middleware\LanguageLoading::class => ['class' => Middleware\LanguageLoading::class],
|
||||
|
||||
@ -26,8 +26,12 @@ use function session_id;
|
||||
* GET Requests would never have token and therefore checking
|
||||
* mismatch does not make sense.
|
||||
*/
|
||||
final class TokenRequestParamChecking implements MiddlewareInterface
|
||||
final readonly class TokenRequestParamChecking implements MiddlewareInterface
|
||||
{
|
||||
public function __construct(private ResponseRenderer $responseRenderer)
|
||||
{
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
assert($request instanceof ServerRequest);
|
||||
@ -60,11 +64,10 @@ final class TokenRequestParamChecking implements MiddlewareInterface
|
||||
|
||||
if ($request->isAjax()) {
|
||||
// There is no point in even attempting to process an ajax request if there is a token mismatch
|
||||
$responseRenderer = ResponseRenderer::getInstance();
|
||||
$responseRenderer->setRequestStatus(false);
|
||||
$responseRenderer->addJSON('message', Message::error(__('Error: Token mismatch')));
|
||||
$this->responseRenderer->setRequestStatus(false);
|
||||
$this->responseRenderer->addJSON('message', Message::error(__('Error: Token mismatch')));
|
||||
|
||||
return $responseRenderer->response();
|
||||
return $this->responseRenderer->response();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -7,8 +7,8 @@ namespace PhpMyAdmin\Tests\Http\Middleware;
|
||||
use PhpMyAdmin\Dbal\DatabaseInterface;
|
||||
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
|
||||
use PhpMyAdmin\Http\Middleware\TokenRequestParamChecking;
|
||||
use PhpMyAdmin\ResponseRenderer;
|
||||
use PhpMyAdmin\Tests\Stubs\DbiDummy;
|
||||
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
@ -20,7 +20,7 @@ final class TokenRequestParamCheckingTest extends TestCase
|
||||
{
|
||||
$_POST = [];
|
||||
|
||||
$middleware = new TokenRequestParamChecking();
|
||||
$middleware = new TokenRequestParamChecking(new ResponseRenderer());
|
||||
|
||||
$_POST['test'] = 'test';
|
||||
$_SESSION[' PMA_token '] = 'token';
|
||||
@ -36,7 +36,7 @@ final class TokenRequestParamCheckingTest extends TestCase
|
||||
{
|
||||
$_POST = [];
|
||||
|
||||
$middleware = new TokenRequestParamChecking();
|
||||
$middleware = new TokenRequestParamChecking(new ResponseRenderer());
|
||||
|
||||
$_POST['test'] = 'test';
|
||||
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/');
|
||||
@ -47,14 +47,14 @@ final class TokenRequestParamCheckingTest extends TestCase
|
||||
|
||||
public function testCheckTokenRequestParamWithTokenMismatch(): void
|
||||
{
|
||||
$middleware = new TokenRequestParamChecking();
|
||||
$responseRenderer = new ResponseRenderer();
|
||||
$middleware = new TokenRequestParamChecking($responseRenderer);
|
||||
|
||||
$dbi = DatabaseInterface::getInstanceForTest(new DbiDummy());
|
||||
DatabaseInterface::$instance = $dbi;
|
||||
|
||||
$_SESSION[' PMA_token '] = 'mismatch';
|
||||
|
||||
$responseRenderer = ResponseRenderer::getInstance();
|
||||
$responseRenderer->setAjax(true);
|
||||
|
||||
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user