Skip to content

Commit e635148

Browse files
committed
minor #6714 Added some missing deprecations related to referrer URLs (javiereguiluz)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Added some missing deprecations related to referrer URLs While removing these features in 5.x branch (see #6712) I realized that we were missing some deprecation messages related to referrer URLs. Commits ------- cfd639c Added some missing deprecations related to referrer URLs
2 parents 026fbdf + cfd639c commit e635148

File tree

11 files changed

+85
-7
lines changed

11 files changed

+85
-7
lines changed

UPGRADE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
Upgrade between EasyAdmin 4.x versions
22
======================================
33

4+
EasyAdmin 4.22.0
5+
----------------
6+
7+
The `referrerUrl` property and the `getReferrerUrl()` method of `BatchActionDto`
8+
are deprecated. This is similar to the rest of deprecations of features related
9+
to the "referrer URL".
10+
11+
The referrer URL is now handled automatically inside EasyAdmin. In your own
12+
batch actions, you can redirect to a specific URL (built with the `AdminUrlGenerator`)
13+
or get the referrer URL from the HTTP headers provided by browsers:
14+
15+
```php
16+
// Before
17+
return $this->redirect($batchActionDto->getReferrer());
18+
19+
// After
20+
return $this->redirect($adminContext->getRequest()->headers->get('referer'));
21+
```
22+
423
EasyAdmin 4.20.0
524
----------------
625

doc/actions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ If you do that, EasyAdmin will inject a DTO with all the batch action data::
537537

538538
$entityManager->flush();
539539

540-
return $this->redirect($batchActionDto->getReferrerUrl());
540+
return $this->redirectToRoute('admin_user_index');
541541
}
542542
}
543543

src/ArgumentResolver/BatchActionDtoResolver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
4141
$context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
4242
$context->getRequest()->request->get(EA::ENTITY_FQCN),
4343
$this->getReferrerUrl($context, $request),
44-
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
44+
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN),
45+
false
4546
);
4647
}
4748

@@ -95,7 +96,8 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
9596
$context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
9697
$context->getRequest()->request->get(EA::ENTITY_FQCN),
9798
$this->getReferrerUrl($context, $request),
98-
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
99+
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN),
100+
false
99101
);
100102
}
101103

src/Contracts/Context/AdminContextInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ interface AdminContextInterface
1818
{
1919
public function getRequest(): Request;
2020

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

2326
public function getI18n(): I18nDto;

src/Dto/BatchActionDto.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,32 @@ class BatchActionDto
1313
private string $referrerUrl;
1414
private string $csrfToken;
1515

16-
public function __construct(string $name, array $entityIds, string $entityFqcn, string $referrerUrl, string $csrfToken)
16+
public function __construct(string $name, array $entityIds, string $entityFqcn, string $referrerUrl, string $csrfToken /* , bool $triggerDeprecation = true */)
1717
{
1818
$this->name = $name;
1919
$this->entityIds = $entityIds;
2020
$this->entityFqcn = $entityFqcn;
21-
$this->referrerUrl = $referrerUrl;
22-
$this->csrfToken = $csrfToken;
21+
22+
// the $referrerUrl argument is deprecated; instead of removing it, do this:
23+
// * if the user pass 5 arguments to the constructor, trigger a deprecation message
24+
// and assign the 4th argument to referrerUrl and the fifth to csrfToken;
25+
// * if the user passes 4 arguments, skip the referrer and assign the 4th to csrfToken
26+
if (\func_num_args() > 4) {
27+
// the sixth unofficial argument allows to skip deprecations when using this method by our own code
28+
if (5 === \func_num_args() || (6 === \func_num_args() && false !== func_get_arg(5))) {
29+
trigger_deprecation(
30+
'easycorp/easyadmin-bundle',
31+
'4.22.0',
32+
'Passing the referrer URL to the "%s" constructor is deprecated. The referrer URL will now be determined automatically based on the current request.',
33+
self::class
34+
);
35+
}
36+
37+
$this->referrerUrl = $referrerUrl;
38+
$this->csrfToken = $csrfToken;
39+
} else {
40+
$this->csrfToken = $referrerUrl;
41+
}
2342
}
2443

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

4059
public function getReferrerUrl(): string
4160
{
61+
trigger_deprecation(
62+
'easycorp/easyadmin-bundle',
63+
'4.22.0',
64+
'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.',
65+
);
66+
4267
return $this->referrerUrl;
4368
}
4469

src/Factory/ActionFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private function processAction(string $pageName, ActionDto $actionDto, ?EntityDt
134134

135135
if (Action::DELETE === $actionDto->getName()) {
136136
$actionDto->addHtmlAttributes([
137-
'formaction' => $this->adminUrlGenerator->setController($adminContext->getCrud()->getControllerFqcn())->setAction(Action::DELETE)->setEntityId($entityDto->getPrimaryKeyValue())->removeReferrer()->generateUrl(),
137+
'formaction' => $this->adminUrlGenerator->setController($adminContext->getCrud()->getControllerFqcn())->setAction(Action::DELETE)->setEntityId($entityDto->getPrimaryKeyValue())->generateUrl(),
138138
'data-bs-toggle' => 'modal',
139139
'data-bs-target' => '#modal-delete',
140140
]);

src/Provider/AdminContextProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ public function getRequest(): Request
5555

5656
public function getReferrer(): ?string
5757
{
58+
trigger_deprecation(
59+
'easycorp/easyadmin-bundle',
60+
'4.8.11',
61+
'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\').',
62+
__METHOD__,
63+
);
64+
5865
return $this->getContext(true)->getReferrer();
5966
}
6067

src/Router/AdminUrlGenerator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ public function includeReferrer(): AdminUrlGeneratorInterface
159159

160160
public function removeReferrer(): AdminUrlGeneratorInterface
161161
{
162+
trigger_deprecation(
163+
'easycorp/easyadmin-bundle',
164+
'4.9.0',
165+
'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.',
166+
);
167+
162168
if (false === $this->isInitialized) {
163169
$this->initialize();
164170
}

src/Router/AdminUrlGeneratorInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@ public function unsetAll(): self;
3232

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

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

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

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

4150
public function addSignature(bool $addSignature = true): self;

tests/Context/AdminContextTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
class AdminContextTest extends TestCase
1919
{
20+
/**
21+
* @group legacy
22+
*/
2023
public function testGetReferrerEmptyString()
2124
{
2225
$request = $this->createMock(Request::class);

0 commit comments

Comments
 (0)