18
18
use Symfony \Component \Security \Http \Firewall \SwitchUserListener ;
19
19
20
20
/**
21
- * Provides generator functions for the impersonate url exit .
21
+ * Provides generator functions for the impersonation urls .
22
22
*
23
23
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
24
24
* @author Damien Fayet <damienf1521@gmail.com>
@@ -36,9 +36,14 @@ public function __construct(RequestStack $requestStack, FirewallMap $firewallMap
36
36
$ this ->firewallMap = $ firewallMap ;
37
37
}
38
38
39
+ public function generateImpersonationPath (string $ identifier = null ): string
40
+ {
41
+ return $ this ->buildPath (null , $ identifier );
42
+ }
43
+
39
44
public function generateExitPath (string $ targetUri = null ): string
40
45
{
41
- return $ this ->buildExitPath ($ targetUri );
46
+ return $ this ->buildPath ($ targetUri );
42
47
}
43
48
44
49
public function generateExitUrl (string $ targetUri = null ): string
@@ -47,27 +52,31 @@ public function generateExitUrl(string $targetUri = null): string
47
52
return '' ;
48
53
}
49
54
50
- return $ request ->getUriForPath ($ this ->buildExitPath ($ targetUri ));
55
+ return $ request ->getUriForPath ($ this ->buildPath ($ targetUri ));
51
56
}
52
57
53
58
private function isImpersonatedUser (): bool
54
59
{
55
60
return $ this ->tokenStorage ->getToken () instanceof SwitchUserToken;
56
61
}
57
62
58
- private function buildExitPath (string $ targetUri = null ): string
63
+ private function buildPath (string $ targetUri = null , string $ identifier = SwitchUserListener:: EXIT_VALUE ): string
59
64
{
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 ){
61
70
return '' ;
62
71
}
63
72
64
73
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. ' );
66
75
}
67
76
68
77
$ targetUri ??= $ request ->getRequestUri ();
69
78
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 ], '' , '& ' );
71
80
72
81
return $ targetUri ;
73
82
}
0 commit comments