Skip to content

Commit 8f180d2

Browse files
committed
Add impersonation_path twig function to generate impersonation path
1 parent 7c3ce06 commit 8f180d2

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

Impersonate/ImpersonateUrlGenerator.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
1919

2020
/**
21-
* Provides generator functions for the impersonate url exit.
21+
* Provides generator functions for the impersonation urls.
2222
*
2323
* @author Amrouche Hamza <[email protected]>
2424
* @author Damien Fayet <[email protected]>
@@ -36,9 +36,14 @@ public function __construct(RequestStack $requestStack, FirewallMap $firewallMap
3636
$this->firewallMap = $firewallMap;
3737
}
3838

39+
public function generateImpersonationPath(string $identifier = null): string
40+
{
41+
return $this->buildPath(null, $identifier);
42+
}
43+
3944
public function generateExitPath(string $targetUri = null): string
4045
{
41-
return $this->buildExitPath($targetUri);
46+
return $this->buildPath($targetUri);
4247
}
4348

4449
public function generateExitUrl(string $targetUri = null): string
@@ -47,27 +52,31 @@ public function generateExitUrl(string $targetUri = null): string
4752
return '';
4853
}
4954

50-
return $request->getUriForPath($this->buildExitPath($targetUri));
55+
return $request->getUriForPath($this->buildPath($targetUri));
5156
}
5257

5358
private function isImpersonatedUser(): bool
5459
{
5560
return $this->tokenStorage->getToken() instanceof SwitchUserToken;
5661
}
5762

58-
private function buildExitPath(string $targetUri = null): string
63+
private function buildPath(string $targetUri = null, string $identifier = SwitchUserListener::EXIT_VALUE): string
5964
{
60-
if (null === ($request = $this->requestStack->getCurrentRequest()) || !$this->isImpersonatedUser()) {
65+
if (null === ($request = $this->requestStack->getCurrentRequest())) {
66+
return '';
67+
}
68+
69+
if (!$this->isImpersonatedUser() && $identifier == SwitchUserListener::EXIT_VALUE){
6170
return '';
6271
}
6372

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

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

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

7281
return $targetUri;
7382
}

0 commit comments

Comments
 (0)