Skip to content

Commit

Permalink
feature symfony#51804 [Security] Make impersonation_path() argument…
Browse files Browse the repository at this point in the history
… 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()`
  • Loading branch information
fabpot committed Oct 2, 2023
2 parents 639eaef + 5d71d95 commit e11ae31
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Allow an array to be passed as the first argument to the `importmap()` Twig function
* Add `TemplatedEmail::locale()` to set the locale for the email rendering
* Add `AppVariable::getEnabledLocales()` to retrieve the enabled locales
* Add `impersonation_path()` and `impersonation_url()` Twig functions

6.3
---
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Bridge/Twig/Extension/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ public function getImpersonateExitPath(string $exitTo = null): string
return $this->impersonateUrlGenerator->generateExitPath($exitTo);
}

public function getImpersonatePath(string $identifier = null): string
public function getImpersonateUrl(string $identifier): string
{
if (null === $this->impersonateUrlGenerator) {
return '';
}

return $this->impersonateUrlGenerator->generateImpersonationUrl($identifier);
}

public function getImpersonatePath(string $identifier): string
{
if (null === $this->impersonateUrlGenerator) {
return '';
Expand All @@ -84,6 +93,7 @@ public function getFunctions(): array
new TwigFunction('is_granted', $this->isGranted(...)),
new TwigFunction('impersonation_exit_url', $this->getImpersonateExitUrl(...)),
new TwigFunction('impersonation_exit_path', $this->getImpersonateExitPath(...)),
new TwigFunction('impersonation_url', $this->getImpersonateUrl(...)),
new TwigFunction('impersonation_path', $this->getImpersonatePath(...)),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,20 @@ public function __construct(RequestStack $requestStack, FirewallMap $firewallMap
$this->firewallMap = $firewallMap;
}

public function generateImpersonationPath(string $identifier = null): string
public function generateImpersonationPath(string $identifier): string
{
return $this->buildPath(null, $identifier);
}

public function generateImpersonationUrl(string $identifier): string
{
if (null === $request = $this->requestStack->getCurrentRequest()) {
return '';
}

return $request->getUriForPath($this->buildPath(null, $identifier));
}

public function generateExitPath(string $targetUri = null): string
{
return $this->buildPath($targetUri);
Expand Down

0 comments on commit e11ae31

Please sign in to comment.