diff options
-rw-r--r-- | Http/Firewall/LogoutListener.php | 18 | ||||
-rw-r--r-- | Http/Logout/LogoutSuccessHandlerInterface.php | 29 |
2 files changed, 44 insertions, 3 deletions
diff --git a/Http/Firewall/LogoutListener.php b/Http/Firewall/LogoutListener.php index 681ed68..4f809b5 100644 --- a/Http/Firewall/LogoutListener.php +++ b/Http/Firewall/LogoutListener.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Security\Http\Firewall; +use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface; + use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface; use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -28,6 +30,7 @@ class LogoutListener implements ListenerInterface protected $logoutPath; protected $targetUrl; protected $handlers; + protected $successHandler; /** * Constructor @@ -36,11 +39,12 @@ class LogoutListener implements ListenerInterface * @param string $logoutPath The path that starts the logout process * @param string $targetUrl The URL to redirect to after logout */ - public function __construct(SecurityContextInterface $securityContext, $logoutPath, $targetUrl = '/') + public function __construct(SecurityContextInterface $securityContext, $logoutPath, $targetUrl = '/', LogoutSuccessHandlerInterface $successHandler = null) { $this->securityContext = $securityContext; $this->logoutPath = $logoutPath; $this->targetUrl = $targetUrl; + $this->successHandler = $successHandler; $this->handlers = array(); } @@ -86,8 +90,16 @@ class LogoutListener implements ListenerInterface return; } - $response = new Response(); - $response->setRedirect(0 !== strpos($this->targetUrl, 'http') ? $request->getUriForPath($this->targetUrl) : $this->targetUrl, 302); + if (null !== $this->successHandler) { + $response = $this->successHandler->onLogoutSuccess($event, $request); + + if (!$response instanceof Response) { + throw new \RuntimeException('Logout Success Handler did not return a Response.'); + } + } else { + $response = new Response(); + $response->setRedirect(0 !== strpos($this->targetUrl, 'http') ? $request->getUriForPath($this->targetUrl) : $this->targetUrl, 302); + } // handle multiple logout attempts gracefully if ($token = $this->securityContext->getToken()) { diff --git a/Http/Logout/LogoutSuccessHandlerInterface.php b/Http/Logout/LogoutSuccessHandlerInterface.php new file mode 100644 index 0000000..346784b --- /dev/null +++ b/Http/Logout/LogoutSuccessHandlerInterface.php @@ -0,0 +1,29 @@ +<?php + +namespace Symfony\Component\Security\Http\Logout; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\EventDispatcher\EventInterface; + +/** + * LogoutSuccesshandlerInterface. + * + * In contrast to the LogoutHandlerInterface, this interface can return a response + * which is then used instead of the default behavior. + * + * If you want to only perform some logout related clean-up task, use the + * LogoutHandlerInterface instead. + * + * @author Johannes M. Schmitt <schmittjoh@gmail.com> + */ +interface LogoutSuccessHandlerInterface +{ + /** + * Creates a Response object to send upon a successful logout. + * + * @param EventInterface $event + * @param Request $request + * @return Response never null + */ + function onLogoutSuccess(EventInterface $event, Request $request); +}
\ No newline at end of file |