From: Raphaƫl Gertz Date: Wed, 11 Dec 2019 04:39:50 +0000 (+0100) Subject: Add logout success handler that redirect on referer when possible X-Git-Tag: 0.1.1~26 X-Git-Url: https://git.rapsys.eu/.gitweb.cgi/airbundle/commitdiff_plain/7dc96408477ff4f0b4a965ef46b364e81faa97a9 Add logout success handler that redirect on referer when possible --- diff --git a/Security/LogoutSuccessHandler.php b/Security/LogoutSuccessHandler.php new file mode 100644 index 0000000..de4d960 --- /dev/null +++ b/Security/LogoutSuccessHandler.php @@ -0,0 +1,110 @@ +router = $router; + } + + /** + * {@inheritdoc} + */ + public function onLogoutSuccess(Request $request) { + //Retrieve logout route + $logout = $request->get('_route'); + + //Extract and process referer + if ($referer = $request->headers->get('referer')) { + //Create referer request instance + $req = Request::create($referer); + + //Get referer path + $path = $req->getPathInfo(); + + //Get referer query string + $query = $req->getQueryString(); + + //Remove script name + $path = str_replace($request->getScriptName(), '', $path); + + //Try with referer path + try { + //Retrieve route matching path + $route = $this->router->match($path); + + //Verify that it differ from current one + if (($name = $route['_route']) == $logout) { + throw new ResourceNotFoundException('Identical referer and logout route'); + } + + //Remove route and controller from route defaults + unset($route['_route'], $route['_controller']); + + //Generate url + $url = $this->router->generate($name, $route); + //No route matched + } catch(ResourceNotFoundException $e) { + //Unset referer to fallback to default route + unset($referer); + } + } + + //Referer empty or unusable + if (empty($referer)) { + //Try with / path + try { + //Retrieve route matching / + $route = $this->router->match('/'); + + //Verify that it differ from current one + if (($name = $route['_route']) == $logout) { + throw new ResourceNotFoundException('Identical referer and logout route'); + } + + //Remove route and controller from route defaults + unset($route['_route'], $route['_controller']); + + //Generate url + $url = $this->router->generate($name, $route); + //Get first route from route collection if / path was not matched + } catch(ResourceNotFoundException $e) { + //Fetch all routes + //XXX: this method regenerate the Routing cache making apps very slow + //XXX: see https://github.com/symfony/symfony-docs/issues/6710 + //XXX: it should be fine to call it without referer and a / route + foreach($this->router->getRouteCollection() as $name => $route) { + //Return on first public route excluding logout one + if (!empty($name) && $name[0] != '_' && $name != $logout) { + break; + } + } + + //Bail out if no route found + if (!isset($name) || !isset($route)) { + throw new \RuntimeException('Unable to retrieve default route'); + } + + //Retrieve route defaults + $defaults = $route->getDefaults(); + + //Remove route and controller from route defaults + unset($defaults['_route'], $defaults['_controller']); + + //Generate url + $url = $this->router->generate($name, $defaults); + } + } + + //Return redirect response + return new RedirectResponse($url, 302); + } +}