3 namespace Rapsys\UserBundle\Handler
;
5 use Symfony\Component\DependencyInjection\ContainerInterface
;
6 use Symfony\Component\HttpFoundation\RedirectResponse
;
7 use Symfony\Component\HttpFoundation\Request
;
8 use Symfony\Component\Routing\Exception\ResourceNotFoundException
;
9 use Symfony\Component\Routing\RequestContext
;
10 use Symfony\Component\Routing\RouterInterface
;
11 use Symfony\Component\Security\Http\Logout\DefaultLogoutSuccessHandler
;
13 use Rapsys\UserBundle\RapsysUserBundle
;
18 class LogoutSuccessHandler
extends DefaultLogoutSuccessHandler
{
35 * @xxx Second argument will be replaced by security.firewalls.main.logout.target
36 * @see vendor/symfony/security-bundle/DependencyInjection/SecurityExtension.php +360
40 public function __construct(ContainerInterface
$container, string $targetUrl = '/', RouterInterface
$router) {
42 $this->config
= $container->getParameter(self
::getAlias());
45 $this->targetUrl
= $targetUrl;
48 $this->router
= $router;
54 public function onLogoutSuccess(Request
$request) {
55 //Retrieve logout route
56 $logout = $request->get('_route');
58 //Extract and process referer
59 if (($referer = $request->headers
->get('referer'))) {
60 //Create referer request instance
61 $req = Request
::create($referer);
64 $path = $req->getPathInfo();
66 //Get referer query string
67 $query = $req->getQueryString();
70 $path = str_replace($request->getScriptName(), '', $path);
72 //Try with referer path
75 $oldContext = $this->router
->getContext();
78 //XXX: prevent MethodNotAllowedException on GET only routes because our context method is POST
79 //XXX: see vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php +42
80 $this->router
->setContext(new RequestContext());
82 //Retrieve route matching path
83 $route = $this->router
->match($path);
86 $this->router
->setContext($oldContext);
91 //Without logout route name
92 if (($name = $route['_route']) != $logout) {
93 //Remove route and controller from route defaults
94 unset($route['_route'], $route['_controller'], $route['_canonical_route']);
97 $url = $this->router
->generate($name, $route);
99 //Return generated route
100 return new RedirectResponse($url, 302);
101 //With logout route name
103 //Unset referer and route
104 unset($referer, $route);
107 } catch (ResourceNotFoundException
$e) {
108 //Unset referer and route
109 unset($referer, $route);
113 //With index route from config
114 if (!empty($name = $this->config
['route']['index']['name']) && is_array($context = $this->config
['route']['index']['context'])) {
115 //Without logout route name
116 if (($name = $route['_route']) != $logout) {
120 $url = $this->router
->generate($name, $context);
122 //Return generated route
123 return new RedirectResponse($url, 302);
125 } catch (ResourceNotFoundException
$e) {
126 //Unset name and context
127 unset($name, $context);
129 //With logout route name
131 //Unset name and context
132 unset($name, $context);
139 $oldContext = $this->router
->getContext();
141 //Force clean context
142 //XXX: prevent MethodNotAllowedException on GET only routes because our context method is POST
143 //XXX: see vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php +42
144 $this->router
->setContext(new RequestContext());
146 //Retrieve route matching target url
147 $route = $this->router
->match($this->targetUrl
);
150 $this->router
->setContext($oldContext);
155 //Without logout route name
156 if (($name = $route['_route']) != $logout) {
157 //Remove route and controller from route defaults
158 unset($route['_route'], $route['_controller'], $route['_canonical_route']);
161 $url = $this->router
->generate($name, $route);
163 //Return generated route
164 return new RedirectResponse($url, 302);
165 //With logout route name
167 //Unset name and route
168 unset($name, $route);
170 //Get first route from route collection if / path was not matched
171 } catch (ResourceNotFoundException
$e) {
172 //Unset name and route
173 unset($name, $route);
177 throw new \
RuntimeException('You must provide a valid logout target url or route name');
183 public function getAlias(): string {
184 return RapsysUserBundle
::getAlias();