Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
[Security] fixed automatic registration of the response listener when…
Browse files Browse the repository at this point in the history
… creating the listener

This is not a problem with Symfony, but when using the component
standalone (Silex for instance), the context listener might be
instantiated even if the firewall does not need to be fired. In that
case, the handle() method is not called, but the response listener is
called, which means that en empty token is stored in the session.

For Silex, it means that when authenticated, if you visit a 404 page,
you would be disconnected automatically.
  • Loading branch information
fabpot committed Jul 6, 2012
1 parent 2f1d9c0 commit b0257cd
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Http/Firewall/ContextListener.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ContextListener implements ListenerInterface
private $contextKey; private $contextKey;
private $logger; private $logger;
private $userProviders; private $userProviders;
private $dispatcher;


public function __construct(SecurityContextInterface $context, array $userProviders, $contextKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null) public function __construct(SecurityContextInterface $context, array $userProviders, $contextKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null)
{ {
Expand All @@ -54,10 +55,7 @@ public function __construct(SecurityContextInterface $context, array $userProvid
$this->userProviders = $userProviders; $this->userProviders = $userProviders;
$this->contextKey = $contextKey; $this->contextKey = $contextKey;
$this->logger = $logger; $this->logger = $logger;

$this->dispatcher = $dispatcher;
if (null !== $dispatcher) {
$dispatcher->addListener(KernelEvents::RESPONSE, array($this, 'onKernelResponse'));
}
} }


/** /**
Expand All @@ -67,6 +65,10 @@ public function __construct(SecurityContextInterface $context, array $userProvid
*/ */
public function handle(GetResponseEvent $event) public function handle(GetResponseEvent $event)
{ {
if (null !== $this->dispatcher && HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$this->dispatcher->addListener(KernelEvents::RESPONSE, array($this, 'onKernelResponse'));
}

$request = $event->getRequest(); $request = $event->getRequest();


$session = $request->hasPreviousSession() ? $request->getSession() : null; $session = $request->hasPreviousSession() ? $request->getSession() : null;
Expand Down

0 comments on commit b0257cd

Please sign in to comment.