Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some missing deprecations related to referrer URLs #6714

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
Upgrade between EasyAdmin 4.x versions
======================================

EasyAdmin 4.22.0
----------------

The `referrerUrl` property and the `getReferrerUrl()` method of `BatchActionDto`
are deprecated. This is similar to the rest of deprecations of features related
to the "referrer URL".

The referrer URL is now handled automatically inside EasyAdmin. In your own
batch actions, you can redirect to a specific URL (built with the `AdminUrlGenerator`)
or get the referrer URL from the HTTP headers provided by browsers:

```php
// Before
return $this->redirect($batchActionDto->getReferrer());

// After
return $this->redirect($adminContext->getRequest()->headers->get('referer'));
```

EasyAdmin 4.20.0
----------------

Expand Down
2 changes: 1 addition & 1 deletion doc/actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ If you do that, EasyAdmin will inject a DTO with all the batch action data::

$entityManager->flush();

return $this->redirect($batchActionDto->getReferrerUrl());
return $this->redirectToRoute('admin_user_index');
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/ArgumentResolver/BatchActionDtoResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
$context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
$context->getRequest()->request->get(EA::ENTITY_FQCN),
$this->getReferrerUrl($context, $request),
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN),
false
);
}

Expand Down Expand Up @@ -95,7 +96,8 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
$context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
$context->getRequest()->request->get(EA::ENTITY_FQCN),
$this->getReferrerUrl($context, $request),
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN),
false
);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Contracts/Context/AdminContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ interface AdminContextInterface
{
public function getRequest(): Request;

/**
* @deprecated since 4.8.11, will be removed in 5.0. Use $context->getRequest()->headers->get('referer') or redirect to some specific URL
*/
public function getReferrer(): ?string;

public function getI18n(): I18nDto;
Expand Down
31 changes: 28 additions & 3 deletions src/Dto/BatchActionDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,32 @@ class BatchActionDto
private string $referrerUrl;
private string $csrfToken;

public function __construct(string $name, array $entityIds, string $entityFqcn, string $referrerUrl, string $csrfToken)
public function __construct(string $name, array $entityIds, string $entityFqcn, string $referrerUrl, string $csrfToken /* , bool $triggerDeprecation = true */)
{
$this->name = $name;
$this->entityIds = $entityIds;
$this->entityFqcn = $entityFqcn;
$this->referrerUrl = $referrerUrl;
$this->csrfToken = $csrfToken;

// the $referrerUrl argument is deprecated; instead of removing it, do this:
// * if the user pass 5 arguments to the constructor, trigger a deprecation message
// and assign the 4th argument to referrerUrl and the fifth to csrfToken;
// * if the user passes 4 arguments, skip the referrer and assign the 4th to csrfToken
if (\func_num_args() > 4) {
// the sixth unofficial argument allows to skip deprecations when using this method by our own code
if (5 === \func_num_args() || (6 === \func_num_args() && false !== func_get_arg(5))) {
trigger_deprecation(
'easycorp/easyadmin-bundle',
'4.22.0',
'Passing the referrer URL to the "%s" constructor is deprecated. The referrer URL will now be determined automatically based on the current request.',
self::class
);
}

$this->referrerUrl = $referrerUrl;
$this->csrfToken = $csrfToken;
} else {
$this->csrfToken = $referrerUrl;
}
}

public function getName(): string
Expand All @@ -39,6 +58,12 @@ public function getEntityFqcn(): string

public function getReferrerUrl(): string
{
trigger_deprecation(
'easycorp/easyadmin-bundle',
'4.22.0',
'The referrer of batch action DTOs is deprecated and it will be removed in 5.0.0. Use $adminContext->getRequest()->headers->get(\'referer\') or redirect to some specific URL.',
);

return $this->referrerUrl;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Factory/ActionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private function processAction(string $pageName, ActionDto $actionDto, ?EntityDt

if (Action::DELETE === $actionDto->getName()) {
$actionDto->addHtmlAttributes([
'formaction' => $this->adminUrlGenerator->setController($adminContext->getCrud()->getControllerFqcn())->setAction(Action::DELETE)->setEntityId($entityDto->getPrimaryKeyValue())->removeReferrer()->generateUrl(),
'formaction' => $this->adminUrlGenerator->setController($adminContext->getCrud()->getControllerFqcn())->setAction(Action::DELETE)->setEntityId($entityDto->getPrimaryKeyValue())->generateUrl(),
'data-bs-toggle' => 'modal',
'data-bs-target' => '#modal-delete',
]);
Expand Down
7 changes: 7 additions & 0 deletions src/Provider/AdminContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public function getRequest(): Request

public function getReferrer(): ?string
{
trigger_deprecation(
'easycorp/easyadmin-bundle',
'4.8.11',
'EasyAdmin URLs no longer include the referrer URL. If you still need it, you can get the referrer provided by browsers via $context->getRequest()->headers->get(\'referer\').',
__METHOD__,
);

return $this->getContext(true)->getReferrer();
}

Expand Down
6 changes: 6 additions & 0 deletions src/Router/AdminUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ public function includeReferrer(): AdminUrlGeneratorInterface

public function removeReferrer(): AdminUrlGeneratorInterface
{
trigger_deprecation(
'easycorp/easyadmin-bundle',
'4.9.0',
'Removing the referrer argument in the admin URLs via the AdminUrlGenerator::removeReferrer() method is deprecated and it will be removed in 5.0.0. The referrer will now be determined automatically based on the current request.',
);

if (false === $this->isInitialized) {
$this->initialize();
}
Expand Down
9 changes: 9 additions & 0 deletions src/Router/AdminUrlGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ public function unsetAll(): self;

public function unsetAllExcept(string ...$namesOfParamsToKeep): self;

/**
* @deprecated since 4.9.0, will be removed in 5.0.0. The referrer will now be determined automatically based on the current request.
*/
public function includeReferrer(): self;

/**
* @deprecated since 4.9.0, will be removed in 5.0.0. The referrer will now be determined automatically based on the current request.
*/
public function removeReferrer(): self;

/**
* @deprecated since 4.9.0, will be removed in 5.0.0. The referrer will now be determined automatically based on the current request.
*/
public function setReferrer(string $referrer): self;

public function addSignature(bool $addSignature = true): self;
Expand Down
3 changes: 3 additions & 0 deletions tests/Context/AdminContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

class AdminContextTest extends TestCase
{
/**
* @group legacy
*/
public function testGetReferrerEmptyString()
{
$request = $this->createMock(Request::class);
Expand Down
4 changes: 4 additions & 0 deletions tests/Controller/CategoryCrudControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public static function new(): \Generator

/**
* @dataProvider edit
*
* @group legacy
*/
public function testEdit(?string $invalidCsrfToken, ?string $expectedErrorMessage)
{
Expand Down Expand Up @@ -131,6 +133,8 @@ public static function edit(): \Generator

/**
* @dataProvider delete
*
* @group legacy
*/
public function testDelete(?string $invalidCsrfToken, callable $expectedCategoriesCount)
{
Expand Down
Loading