Skip to content

Commit e11ae31

Browse files
committed
feature symfony#51804 [Security] Make impersonation_path() argument mandatory and add impersonation_url() (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [Security] Make `impersonation_path()` argument mandatory and add `impersonation_url()` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT Follow-up of symfony#50030 When documenting this function, I found out that the`identifier` argument was optional, which seemed weird to me given the function purpose. I then had a look at the implementation, and I saw that `ImpersonateUrlGenerator::generateImpersonationPath()` accepts a nullable string. However, the underlying call to `ImpersonateUrlGenerator::buildPath()` doesn't accept a nullable string. I propose to make the `identifier` argument mandatory, which makes more sense here. I also added the missing Changelog line and `impersonation_url()` Commits ------- 5d71d95 [Security] Make `impersonation_path()` argument mandatory and add `impersonation_url()`
2 parents 639eaef + 5d71d95 commit e11ae31

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Allow an array to be passed as the first argument to the `importmap()` Twig function
88
* Add `TemplatedEmail::locale()` to set the locale for the email rendering
99
* Add `AppVariable::getEnabledLocales()` to retrieve the enabled locales
10+
* Add `impersonation_path()` and `impersonation_url()` Twig functions
1011

1112
6.3
1213
---

src/Symfony/Bridge/Twig/Extension/SecurityExtension.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ public function getImpersonateExitPath(string $exitTo = null): string
6969
return $this->impersonateUrlGenerator->generateExitPath($exitTo);
7070
}
7171

72-
public function getImpersonatePath(string $identifier = null): string
72+
public function getImpersonateUrl(string $identifier): string
73+
{
74+
if (null === $this->impersonateUrlGenerator) {
75+
return '';
76+
}
77+
78+
return $this->impersonateUrlGenerator->generateImpersonationUrl($identifier);
79+
}
80+
81+
public function getImpersonatePath(string $identifier): string
7382
{
7483
if (null === $this->impersonateUrlGenerator) {
7584
return '';
@@ -84,6 +93,7 @@ public function getFunctions(): array
8493
new TwigFunction('is_granted', $this->isGranted(...)),
8594
new TwigFunction('impersonation_exit_url', $this->getImpersonateExitUrl(...)),
8695
new TwigFunction('impersonation_exit_path', $this->getImpersonateExitPath(...)),
96+
new TwigFunction('impersonation_url', $this->getImpersonateUrl(...)),
8797
new TwigFunction('impersonation_path', $this->getImpersonatePath(...)),
8898
];
8999
}

src/Symfony/Component/Security/Http/Impersonate/ImpersonateUrlGenerator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,20 @@ public function __construct(RequestStack $requestStack, FirewallMap $firewallMap
3636
$this->firewallMap = $firewallMap;
3737
}
3838

39-
public function generateImpersonationPath(string $identifier = null): string
39+
public function generateImpersonationPath(string $identifier): string
4040
{
4141
return $this->buildPath(null, $identifier);
4242
}
4343

44+
public function generateImpersonationUrl(string $identifier): string
45+
{
46+
if (null === $request = $this->requestStack->getCurrentRequest()) {
47+
return '';
48+
}
49+
50+
return $request->getUriForPath($this->buildPath(null, $identifier));
51+
}
52+
4453
public function generateExitPath(string $targetUri = null): string
4554
{
4655
return $this->buildPath($targetUri);

0 commit comments

Comments
 (0)