From 5d71d95865e95e6c69dddb7ed75dc815c0e283f1 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Mon, 2 Oct 2023 10:52:41 +0200 Subject: [PATCH] [Security] Make `impersonation_path()` argument mandatory and add `impersonation_url()` --- src/Symfony/Bridge/Twig/CHANGELOG.md | 1 + .../Bridge/Twig/Extension/SecurityExtension.php | 12 +++++++++++- .../Http/Impersonate/ImpersonateUrlGenerator.php | 11 ++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Twig/CHANGELOG.md b/src/Symfony/Bridge/Twig/CHANGELOG.md index 4e8ed13101b8..9bb7aa0c7f1f 100644 --- a/src/Symfony/Bridge/Twig/CHANGELOG.md +++ b/src/Symfony/Bridge/Twig/CHANGELOG.md @@ -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 --- diff --git a/src/Symfony/Bridge/Twig/Extension/SecurityExtension.php b/src/Symfony/Bridge/Twig/Extension/SecurityExtension.php index be222b056729..3c3881ad00d0 100644 --- a/src/Symfony/Bridge/Twig/Extension/SecurityExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/SecurityExtension.php @@ -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 ''; @@ -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(...)), ]; } diff --git a/src/Symfony/Component/Security/Http/Impersonate/ImpersonateUrlGenerator.php b/src/Symfony/Component/Security/Http/Impersonate/ImpersonateUrlGenerator.php index a99dd89f0225..f28d063ecbf8 100644 --- a/src/Symfony/Component/Security/Http/Impersonate/ImpersonateUrlGenerator.php +++ b/src/Symfony/Component/Security/Http/Impersonate/ImpersonateUrlGenerator.php @@ -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);