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

Add new twig bridge function to generate impersonation path #50030

Merged
merged 1 commit into from Oct 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/SecurityExtension.php
Expand Up @@ -69,12 +69,22 @@ public function getImpersonateExitPath(string $exitTo = null): string
return $this->impersonateUrlGenerator->generateExitPath($exitTo);
}

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

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

public function getFunctions(): array
{
return [
new TwigFunction('is_granted', $this->isGranted(...)),
new TwigFunction('impersonation_exit_url', $this->getImpersonateExitUrl(...)),
new TwigFunction('impersonation_exit_path', $this->getImpersonateExitPath(...)),
new TwigFunction('impersonation_path', $this->getImpersonatePath(...)),
];
}
}
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Security\Http\Firewall\SwitchUserListener;

/**
* Provides generator functions for the impersonate url exit.
* Provides generator functions for the impersonation urls.
*
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
* @author Damien Fayet <damienf1521@gmail.com>
Expand All @@ -36,9 +36,14 @@ public function __construct(RequestStack $requestStack, FirewallMap $firewallMap
$this->firewallMap = $firewallMap;
}

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

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

public function generateExitUrl(string $targetUri = null): string
Expand All @@ -47,27 +52,31 @@ public function generateExitUrl(string $targetUri = null): string
return '';
}

return $request->getUriForPath($this->buildExitPath($targetUri));
return $request->getUriForPath($this->buildPath($targetUri));
}

private function isImpersonatedUser(): bool
{
return $this->tokenStorage->getToken() instanceof SwitchUserToken;
}

private function buildExitPath(string $targetUri = null): string
private function buildPath(string $targetUri = null, string $identifier = SwitchUserListener::EXIT_VALUE): string
{
if (null === ($request = $this->requestStack->getCurrentRequest()) || !$this->isImpersonatedUser()) {
if (null === ($request = $this->requestStack->getCurrentRequest())) {
return '';
}

if (!$this->isImpersonatedUser() && $identifier == SwitchUserListener::EXIT_VALUE){
return '';
}

if (null === $switchUserConfig = $this->firewallMap->getFirewallConfig($request)->getSwitchUser()) {
throw new \LogicException('Unable to generate the impersonate exit URL without a firewall configured for the user switch.');
throw new \LogicException('Unable to generate the impersonate URLs without a firewall configured for the user switch.');
}

$targetUri ??= $request->getRequestUri();

$targetUri .= (parse_url($targetUri, \PHP_URL_QUERY) ? '&' : '?').http_build_query([$switchUserConfig['parameter'] => SwitchUserListener::EXIT_VALUE], '', '&');
$targetUri .= (parse_url($targetUri, \PHP_URL_QUERY) ? '&' : '?').http_build_query([$switchUserConfig['parameter'] => $identifier], '', '&');

return $targetUri;
}
Expand Down