diff options
author | Bernhard Schussek <bernhard.schussek@symfony-project.com> | 2011-03-05 15:30:34 +0100 |
---|---|---|
committer | Bernhard Schussek <bernhard.schussek@symfony-project.com> | 2011-03-05 15:30:34 +0100 |
commit | c0b58aaf0672541eb7215d4018201b0e21ff957d (patch) | |
tree | 7a76588be3608fe885ed1a5d45c95419f64166fe | |
parent | a45d4a21c023980a2d652234d7068a477a20f6e8 (diff) | |
download | symfony-security-c0b58aaf0672541eb7215d4018201b0e21ff957d.zip symfony-security-c0b58aaf0672541eb7215d4018201b0e21ff957d.tar.gz symfony-security-c0b58aaf0672541eb7215d4018201b0e21ff957d.tar.bz2 |
Replaced EventDispatcher by Doctrine's EventManager implementation
Doctrine's EventManager implementation has several advantages over the
EventDispatcher implementation of Symfony2. Therefore I suggest that we
use their implementation.
Advantages:
* Event Listeners are objects, not callbacks. These objects have handler
methods that have the same name as the event. This helps a lot when
reading the code and makes the code for adding an event listener shorter.
* You can create Event Subscribers, which are event listeners with an
additional getSubscribedEvents() method. The benefit here is that the
code that registers the subscriber doesn't need to know about its
implementation.
* All events are defined in static Events classes, so users of IDEs benefit
of code completion
* The communication between the dispatching class of an event and all
listeners is done through a subclass of EventArgs. This subclass can be
tailored to the type of event. A constructor, setters and getters can be
implemented that verify the validity of the data set into the object.
See examples below.
* Because each event type corresponds to an EventArgs implementation,
developers of event listeners can look up the available EventArgs methods
and benefit of code completion.
* EventArgs::stopPropagation() is more flexible and (IMO) clearer to use
than notifyUntil(). Also, it is a concept that is also used in other
event implementations
Before:
class EventListener
{
public function handle(EventInterface $event, $data) { ... }
}
$dispatcher->connect('core.request', array($listener, 'handle'));
$dispatcher->notify('core.request', new Event(...));
After (with listeners):
final class Events
{
const onCoreRequest = 'onCoreRequest';
}
class EventListener
{
public function onCoreRequest(RequestEventArgs $eventArgs) { ... }
}
$evm->addEventListener(Events::onCoreRequest, $listener);
$evm->dispatchEvent(Events::onCoreRequest, new RequestEventArgs(...));
After (with subscribers):
class EventSubscriber
{
public function onCoreRequest(RequestEventArgs $eventArgs) { ... }
public function getSubscribedEvents()
{
return Events::onCoreRequest;
}
}
$evm->addEventSubscriber($subscriber);
$evm->dispatchEvent(Events::onCoreRequest, new RequestEventArgs(...));
26 files changed, 355 insertions, 261 deletions
diff --git a/Http/Authentication/AuthenticationFailureHandlerInterface.php b/Http/Authentication/AuthenticationFailureHandlerInterface.php index 8defef6..4e6c694 100644 --- a/Http/Authentication/AuthenticationFailureHandlerInterface.php +++ b/Http/Authentication/AuthenticationFailureHandlerInterface.php @@ -2,8 +2,8 @@ namespace Symfony\Component\Security\Http\Authentication; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; use Symfony\Component\Security\Core\Exception\AuthenticationException; -use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\HttpFoundation\Request; /** @@ -22,12 +22,12 @@ interface AuthenticationFailureHandlerInterface * called by authentication listeners inheriting from * AbstractAuthenticationListener. * - * @param EventInterface $event the "core.security" event, this event always + * @param RequestEventArgs $eventArgs the "onCoreSecurity" event, this event always * has the kernel as target * @param Request $request * @param AuthenticationException $exception * * @return Response the response to return */ - function onAuthenticationFailure(EventInterface $event, Request $request, AuthenticationException $exception); + function onAuthenticationFailure(RequestEventArgs $eventArgs, Request $request, AuthenticationException $exception); }
\ No newline at end of file diff --git a/Http/Authentication/AuthenticationSuccessHandlerInterface.php b/Http/Authentication/AuthenticationSuccessHandlerInterface.php index 235eb94..e781cbd 100644 --- a/Http/Authentication/AuthenticationSuccessHandlerInterface.php +++ b/Http/Authentication/AuthenticationSuccessHandlerInterface.php @@ -2,7 +2,7 @@ namespace Symfony\Component\Security\Http\Authentication; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\HttpFoundation\Request; @@ -22,12 +22,12 @@ interface AuthenticationSuccessHandlerInterface * is called by authentication listeners inheriting from * AbstractAuthenticationListener. * - * @param EventInterface $event the "core.security" event, this event always + * @param RequestEventArgs $eventArgs the "onCoreSecurity" event, this event always * has the kernel as target * @param Request $request * @param TokenInterface $token * * @return Response the response to return */ - function onAuthenticationSuccess(EventInterface $event, Request $request, TokenInterface $token); + function onAuthenticationSuccess(RequestEventArgs $eventArgs, Request $request, TokenInterface $token); }
\ No newline at end of file diff --git a/Http/Authorization/AccessDeniedHandlerInterface.php b/Http/Authorization/AccessDeniedHandlerInterface.php index 7a1bcf4..b2fb9ee 100644 --- a/Http/Authorization/AccessDeniedHandlerInterface.php +++ b/Http/Authorization/AccessDeniedHandlerInterface.php @@ -3,7 +3,7 @@ namespace Symfony\Component\Security\Http\Authorization; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\ExceptionEventArgs; use Symfony\Component\Security\Core\Exception\AccessDeniedException; /** @@ -17,11 +17,11 @@ interface AccessDeniedHandlerInterface /** * Handles an access denied failure. * - * @param EventInterface $event + * @param ExceptionEventArgs $eventArgs * @param Request $request * @param AccessDeniedException $accessDeniedException * * @return Response may return null */ - function handle(EventInterface $event, Request $request, AccessDeniedException $accessDeniedException); + function handle(ExceptionEventArgs $eventArgs, Request $request, AccessDeniedException $accessDeniedException); }
\ No newline at end of file diff --git a/Http/EntryPoint/AuthenticationEntryPointInterface.php b/Http/EntryPoint/AuthenticationEntryPointInterface.php index 98cbf28..ab0b220 100644 --- a/Http/EntryPoint/AuthenticationEntryPointInterface.php +++ b/Http/EntryPoint/AuthenticationEntryPointInterface.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Security\Http\EntryPoint; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\HttpFoundation\Request; @@ -26,9 +26,9 @@ interface AuthenticationEntryPointInterface /** * Starts the authentication scheme. * - * @param EventInterface $event The "core.security" event + * @param RequestEventArgs $eventArgs The "onCoreSecurity" event * @param object $request The request that resulted in an AuthenticationException * @param AuthenticationException $authException The exception that started the authentication process */ - function start(EventInterface $event, Request $request, AuthenticationException $authException = null); + function start(RequestEventArgs $eventArgs, Request $request, AuthenticationException $authException = null); } diff --git a/Http/EntryPoint/BasicAuthenticationEntryPoint.php b/Http/EntryPoint/BasicAuthenticationEntryPoint.php index 907301c..8a564e6 100644 --- a/Http/EntryPoint/BasicAuthenticationEntryPoint.php +++ b/Http/EntryPoint/BasicAuthenticationEntryPoint.php @@ -11,11 +11,11 @@ namespace Symfony\Component\Security\Http\EntryPoint; -use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; /** * BasicAuthenticationEntryPoint starts an HTTP Basic authentication. @@ -31,7 +31,7 @@ class BasicAuthenticationEntryPoint implements AuthenticationEntryPointInterface $this->realmName = $realmName; } - public function start(EventInterface $event, Request $request, AuthenticationException $authException = null) + public function start(RequestEventArgs $event, Request $request, AuthenticationException $authException = null) { $response = new Response(); $response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realmName)); diff --git a/Http/EntryPoint/DigestAuthenticationEntryPoint.php b/Http/EntryPoint/DigestAuthenticationEntryPoint.php index ecc6178..3392065 100644 --- a/Http/EntryPoint/DigestAuthenticationEntryPoint.php +++ b/Http/EntryPoint/DigestAuthenticationEntryPoint.php @@ -11,13 +11,13 @@ namespace Symfony\Component\Security\Http\EntryPoint; -use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\Security\Core\Exception\NonceExpiredException; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Log\LoggerInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; /** * DigestAuthenticationEntryPoint starts an HTTP Digest authentication. @@ -39,7 +39,7 @@ class DigestAuthenticationEntryPoint implements AuthenticationEntryPointInterfac $this->logger = $logger; } - public function start(EventInterface $event, Request $request, AuthenticationException $authException = null) + public function start(RequestEventArgs $eventArgs, Request $request, AuthenticationException $authException = null) { $expiryTime = microtime(true) + $this->nonceValiditySeconds * 1000; $signatureValue = md5($expiryTime.':'.$this->key); diff --git a/Http/EntryPoint/FormAuthenticationEntryPoint.php b/Http/EntryPoint/FormAuthenticationEntryPoint.php index 1f1cda7..b91d225 100644 --- a/Http/EntryPoint/FormAuthenticationEntryPoint.php +++ b/Http/EntryPoint/FormAuthenticationEntryPoint.php @@ -11,13 +11,13 @@ namespace Symfony\Component\Security\Http\EntryPoint; -use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; /** * FormAuthenticationEntryPoint starts an authentication via a login form. @@ -44,10 +44,10 @@ class FormAuthenticationEntryPoint implements AuthenticationEntryPointInterface /** * {@inheritdoc} */ - public function start(EventInterface $event, Request $request, AuthenticationException $authException = null) + public function start(RequestEventArgs $eventArgs, Request $request, AuthenticationException $authException = null) { if ($this->useForward) { - return $event->getSubject()->handle(Request::create($this->loginPath), HttpKernelInterface::SUB_REQUEST); + return $event->getKernel()->handle(Request::create($this->loginPath), HttpKernelInterface::SUB_REQUEST); } return new RedirectResponse(0 !== strpos($this->loginPath, 'http') ? $request->getUriForPath($this->loginPath) : $this->loginPath, 302); diff --git a/Http/EntryPoint/RetryAuthenticationEntryPoint.php b/Http/EntryPoint/RetryAuthenticationEntryPoint.php index cde65aa..1b5900b 100644 --- a/Http/EntryPoint/RetryAuthenticationEntryPoint.php +++ b/Http/EntryPoint/RetryAuthenticationEntryPoint.php @@ -11,12 +11,12 @@ namespace Symfony\Component\Security\Http\EntryPoint; -use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; /** * RetryAuthenticationEntryPoint redirects URL based on the configured scheme. @@ -36,7 +36,7 @@ class RetryAuthenticationEntryPoint implements AuthenticationEntryPointInterface $this->httpsPort = $httpsPort; } - public function start(EventInterface $event, Request $request, AuthenticationException $authException = null) + public function start(RequestEventArgs $eventArgs, Request $request, AuthenticationException $authException = null) { $scheme = $request->isSecure() ? 'http' : 'https'; if ('http' === $scheme && 80 != $this->httpPort) { diff --git a/Http/Event/InteractiveLoginEventArgs.php b/Http/Event/InteractiveLoginEventArgs.php new file mode 100644 index 0000000..7ca4f4e --- /dev/null +++ b/Http/Event/InteractiveLoginEventArgs.php @@ -0,0 +1,38 @@ +<?php + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Http\Event; + +use Symfony\Component\HttpFoundation\Request; +use Doctrine\Common\EventArgs; + +class InteractiveLoginEventArgs extends EventArgs +{ + private $request; + + private $authenticationToken; + + public function __construct(Request $request, $authenticationToken) + { + $this->request = $request; + $this->authenticationToken = $authenticationToken; + } + + public function getRequest() + { + return $this->request; + } + + public function getAuthenticationToken() + { + return $this->authenticationToken; + } +}
\ No newline at end of file diff --git a/Http/Event/SwitchUserEventArgs.php b/Http/Event/SwitchUserEventArgs.php new file mode 100644 index 0000000..be38036 --- /dev/null +++ b/Http/Event/SwitchUserEventArgs.php @@ -0,0 +1,39 @@ +<?php + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Http\Event; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Security\Core\User\AccountInterface; +use Doctrine\Common\EventArgs; + +class SwitchUserEventArgs extends EventArgs +{ + private $request; + + private $targetUser; + + public function __construct(Request $request, AccountInterface $targetUser) + { + $this->request = $request; + $this->targetUser = $targetUser; + } + + public function getRequest() + { + return $this->request; + } + + public function getTargetUser() + { + return $this->targetUser; + } +}
\ No newline at end of file diff --git a/Http/Events.php b/Http/Events.php new file mode 100644 index 0000000..ac4a1ca --- /dev/null +++ b/Http/Events.php @@ -0,0 +1,19 @@ +<?php + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Http; + +final class Events +{ + const onSecurityInteractiveLogin = 'onSecurityInteractiveLogin'; + + const onSecuritySwitchUser = 'onSecuritySwitchUser'; +}
\ No newline at end of file diff --git a/Http/Firewall.php b/Http/Firewall.php index f7fabbb..55d2b28 100644 --- a/Http/Firewall.php +++ b/Http/Firewall.php @@ -11,11 +11,11 @@ namespace Symfony\Component\Security\Http; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\Events; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; use Symfony\Component\HttpFoundation\Request; +use Doctrine\Common\EventManager; /** * Firewall uses a FirewallMap to register security listeners for the given @@ -25,14 +25,12 @@ use Symfony\Component\HttpFoundation\Request; * (a Basic authentication for the /api, and a web based authentication for * everything else for instance). * - * The handle method must be connected to the core.request event. - * * @author Fabien Potencier <fabien.potencier@symfony-project.com> */ class Firewall { protected $map; - protected $dispatcher; + protected $evm; protected $currentListeners; /** @@ -40,42 +38,42 @@ class Firewall * * @param FirewallMap $map A FirewallMap instance */ - public function __construct(FirewallMapInterface $map, EventDispatcherInterface $dispatcher) + public function __construct(FirewallMapInterface $map, EventManager $evm) { $this->map = $map; - $this->dispatcher = $dispatcher; + $this->evm = $evm; $this->currentListeners = array(); } /** * Handles security. * - * @param EventInterface $event An EventInterface instance + * @param RequestEventArgs $eventArgs An RequestEventArgs instance */ - public function handle(EventInterface $event) + public function onCoreRequest(RequestEventArgs $eventArgs) { - if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) { + if (HttpKernelInterface::MASTER_REQUEST !== $eventArgs->getRequestType()) { return; } - $request = $event->get('request'); + $request = $eventArgs->getRequest(); - // disconnect all listeners from core.security to avoid the overhead + // disconnect all listeners from onCoreSecurity to avoid the overhead // of most listeners having to do this manually - $this->dispatcher->disconnect('core.security'); + $this->evm->removeEventListeners(Events::onCoreSecurity); // ensure that listeners disconnect from wherever they have connected to foreach ($this->currentListeners as $listener) { - $listener->unregister($this->dispatcher); + $listener->unregister($this->evm); } // register listeners for this firewall list($listeners, $exception) = $this->map->getListeners($request); if (null !== $exception) { - $exception->register($this->dispatcher); + $exception->register($this->evm); } foreach ($listeners as $listener) { - $listener->register($this->dispatcher); + $listener->register($this->evm); } // save current listener instances @@ -85,11 +83,11 @@ class Firewall } // initiate the listener chain - $ret = $this->dispatcher->notifyUntil($securityEvent = new Event($request, 'core.security', array('request' => $request))); - if ($securityEvent->isProcessed()) { - $event->setProcessed(); + $securityEventArgs = new RequestEventArgs($eventArgs->getKernel(), $request, $eventArgs->getRequestType()); + $this->evm->dispatchEvent($securityEventArgs); - return $ret; + if ($securityEventArgs->hasResponse()) { + $eventArgs->setResponse($securityEventArgs->getResponse()); } } } diff --git a/Http/Firewall/AbstractAuthenticationListener.php b/Http/Firewall/AbstractAuthenticationListener.php index f992b98..088e9a7 100644 --- a/Http/Firewall/AbstractAuthenticationListener.php +++ b/Http/Firewall/AbstractAuthenticationListener.php @@ -11,22 +11,22 @@ namespace Symfony\Component\Security\Http\Firewall; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface; use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface; use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; -use Symfony\Component\HttpKernel\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; -use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Log\LoggerInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\Events as KernelEvents; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Doctrine\Common\EventManager; /** * The AbstractAuthenticationListener is the preferred base class for all @@ -51,7 +51,7 @@ abstract class AbstractAuthenticationListener implements ListenerInterface protected $authenticationManager; protected $sessionStrategy; protected $providerKey; - protected $eventDispatcher; + protected $evm; protected $options; protected $successHandler; protected $failureHandler; @@ -102,22 +102,21 @@ abstract class AbstractAuthenticationListener implements ListenerInterface } /** - * Subscribe to the core.security event + * Subscribe to the onCoreSecurity event * - * @param EventDispatcher $dispatcher An EventDispatcher instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'handle'), 0); + $evm->addEventListener(KernelEvents::onCoreSecurity, $this); - $this->eventDispatcher = $dispatcher; + $this->evm = $evm; } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { } @@ -126,9 +125,9 @@ abstract class AbstractAuthenticationListener implements ListenerInterface * * @param Event $event An Event instance */ - public function handle(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { - $request = $event->get('request'); + $request = $eventArgs->getRequest(); if (!$this->requiresAuthentication($request)) { return; @@ -142,19 +141,17 @@ abstract class AbstractAuthenticationListener implements ListenerInterface if ($returnValue instanceof TokenInterface) { $this->sessionStrategy->onAuthentication($request, $returnValue); - $response = $this->onSuccess($event, $request, $returnValue); + $response = $this->onSuccess($eventArgs, $request, $returnValue); } else if ($returnValue instanceof Response) { $response = $returnValue; } else { throw new \RuntimeException('attemptAuthentication() must either return a Response, an implementation of TokenInterface, or null.'); } - } catch (AuthenticationException $failed) { - $response = $this->onFailure($event, $request, $failed); + } catch (AuthenticationException $e) { + $response = $this->onFailure($eventArgs, $request, $e); } - $event->setProcessed(); - - return $response; + $eventArgs->setResponse($response); } /** @@ -173,7 +170,7 @@ abstract class AbstractAuthenticationListener implements ListenerInterface return $this->options['check_path'] === $request->getPathInfo(); } - protected function onFailure($event, Request $request, AuthenticationException $failed) + protected function onFailure(RequestEventArgs $eventArgs, Request $request, AuthenticationException $failed) { if (null !== $this->logger) { $this->logger->debug(sprintf('Authentication request failed: %s', $failed->getMessage())); @@ -182,7 +179,7 @@ abstract class AbstractAuthenticationListener implements ListenerInterface $this->securityContext->setToken(null); if (null !== $this->failureHandler) { - return $this->failureHandler->onAuthenticationFailure($event, $request, $failed); + return $this->failureHandler->onAuthenticationFailure($eventArgs, $request, $failed); } if (null === $this->options['failure_path']) { @@ -197,7 +194,7 @@ abstract class AbstractAuthenticationListener implements ListenerInterface $subRequest = Request::create($this->options['failure_path']); $subRequest->attributes->set(SecurityContextInterface::AUTHENTICATION_ERROR, $failed); - return $event->getSubject()->handle($subRequest, HttpKernelInterface::SUB_REQUEST); + return $eventArgs->getSubject()->handle($subRequest, HttpKernelInterface::SUB_REQUEST); } if (null !== $this->logger) { @@ -209,7 +206,7 @@ abstract class AbstractAuthenticationListener implements ListenerInterface return new RedirectResponse(0 !== strpos($this->options['failure_path'], 'http') ? $request->getUriForPath($this->options['failure_path']) : $this->options['failure_path'], 302); } - protected function onSuccess(EventInterface $event, Request $request, TokenInterface $token) + protected function onSuccess(RequestEventArgs $eventArgs, Request $request, TokenInterface $token) { if (null !== $this->logger) { $this->logger->debug('User has been authenticated successfully'); @@ -221,12 +218,13 @@ abstract class AbstractAuthenticationListener implements ListenerInterface $session->remove(SecurityContextInterface::AUTHENTICATION_ERROR); $session->remove(SecurityContextInterface::LAST_USERNAME); - if (null !== $this->eventDispatcher) { - $this->eventDispatcher->notify(new Event($this, 'security.interactive_login', array('request' => $request, 'token' => $token))); + if (null !== $this->evm) { + $loginEventArgs = new InteractiveLoginEventArgs($request, $token); + $this->evm->dispatchEvent(Events::onSecurityInteractiveLogin, $loginEventArgs); } if (null !== $this->successHandler) { - $response = $this->successHandler->onAuthenticationSuccess($event, $request, $token); + $response = $this->successHandler->onAuthenticationSuccess($eventArgs, $request, $token); } else { $path = $this->determineTargetUrl($request); $response = new RedirectResponse(0 !== strpos($path, 'http') ? $request->getUriForPath($path) : $path, 302); diff --git a/Http/Firewall/AbstractPreAuthenticatedListener.php b/Http/Firewall/AbstractPreAuthenticatedListener.php index 555dadb..35992db 100644 --- a/Http/Firewall/AbstractPreAuthenticatedListener.php +++ b/Http/Firewall/AbstractPreAuthenticatedListener.php @@ -11,15 +11,17 @@ namespace Symfony\Component\Security\Http\Firewall; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; -use Symfony\Component\HttpKernel\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; use Symfony\Component\Security\Core\Exception\AuthenticationException; +use Symfony\Component\Security\Http\Event\InteractiveLoginEventArgs; +use Symfony\Component\Security\Http\Events; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; +use Symfony\Component\HttpKernel\Events as KernelEvents; +use Symfony\Component\HttpKernel\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Request; +use Doctrine\Common\EventManager; /** * AbstractPreAuthenticatedListener is the base class for all listener that @@ -34,7 +36,7 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface protected $authenticationManager; protected $providerKey; protected $logger; - protected $eventDispatcher; + protected $evm; public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, LoggerInterface $logger = null) { @@ -47,20 +49,19 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface /** * * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'handle'), 0); + $evm->addEventListener(KernelEvents::onCoreSecurity, $this); - $this->eventDispatcher = $dispatcher; + $this->evm = $evm; } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { } @@ -69,9 +70,9 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface * * @param EventInterface $event An EventInterface instance */ - public function handle(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { - $request = $event->get('request'); + $request = $eventArgs->getRequest(); if (null !== $this->logger) { $this->logger->debug(sprintf('Checking secure context token: %s', $this->securityContext->getToken())); @@ -101,8 +102,9 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface } $this->securityContext->setToken($token); - if (null !== $this->eventDispatcher) { - $this->eventDispatcher->notify(new Event($this, 'security.interactive_login', array('request' => $request, 'token' => $token))); + if (null !== $this->evm) { + $loginEventArgs = new InteractiveLoginEventArgs($request, $token); + $this->evm->notify(Events::onSecurityInteractiveLogin, $loginEventArgs); } } catch (AuthenticationException $failed) { $this->securityContext->setToken(null); diff --git a/Http/Firewall/AccessListener.php b/Http/Firewall/AccessListener.php index 4aa1c55..43213a5 100644 --- a/Http/Firewall/AccessListener.php +++ b/Http/Firewall/AccessListener.php @@ -16,10 +16,11 @@ use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface use Symfony\Component\Security\Http\AccessMap; use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; use Symfony\Component\HttpKernel\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; +use Symfony\Component\HttpKernel\Events; use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; use Symfony\Component\Security\Core\Exception\AccessDeniedException; +use Doctrine\Common\EventManager; /** * AccessListener enforces access control rules. @@ -44,35 +45,34 @@ class AccessListener implements ListenerInterface } /** - * Registers a core.security listener to enforce authorization rules. + * Registers a onCoreSecurity listener to enforce authorization rules. * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'handle'), 0); + $evm->addEventListener(Events::onCoreSecurity, $this); } - + /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { } /** * Handles access authorization. * - * @param EventInterface $event An EventInterface instance + * @param RequestEventArgs $eventArgs A RequestEventArgs instance */ - public function handle(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { if (null === $token = $this->context->getToken()) { throw new AuthenticationCredentialsNotFoundException('A Token was not found in the SecurityContext.'); } - $request = $event->get('request'); + $request = $eventArgs->getRequest(); list($attributes, $channel) = $this->map->getPatterns($request); diff --git a/Http/Firewall/AnonymousAuthenticationListener.php b/Http/Firewall/AnonymousAuthenticationListener.php index 05d2301..7901b20 100644 --- a/Http/Firewall/AnonymousAuthenticationListener.php +++ b/Http/Firewall/AnonymousAuthenticationListener.php @@ -13,9 +13,10 @@ namespace Symfony\Component\Security\Http\Firewall; use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\HttpKernel\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; +use Symfony\Component\HttpKernel\Events; use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; +use Doctrine\Common\EventManager; /** * AnonymousAuthenticationListener automatically addds a Token if none is @@ -37,30 +38,29 @@ class AnonymousAuthenticationListener implements ListenerInterface } /** - * Registers a core.security listener to load the SecurityContext from the + * Registers a onCoreSecurity listener to load the SecurityContext from the * session. * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'handle'), 0); + $evm->addEventListener(Events::onCoreSecurity, $this); } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { } /** * Handles anonymous authentication. * - * @param EventInterface $event An EventInterface instance + * @param RequestEventArgs $eventArgs A RequestEventArgs instance */ - public function handle(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { if (null !== $this->context->getToken()) { return; diff --git a/Http/Firewall/BasicAuthenticationListener.php b/Http/Firewall/BasicAuthenticationListener.php index 972d081..9410a9e 100644 --- a/Http/Firewall/BasicAuthenticationListener.php +++ b/Http/Firewall/BasicAuthenticationListener.php @@ -15,10 +15,11 @@ use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\HttpKernel\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; +use Symfony\Component\HttpKernel\Events; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Exception\AuthenticationException; +use Doctrine\Common\EventManager; /** * BasicAuthenticationListener implements Basic HTTP authentication. @@ -51,27 +52,26 @@ class BasicAuthenticationListener implements ListenerInterface /** * * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'handle'), 0); + $evm->addEventListener(Events::onCoreSecurity, $this); } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { } /** * Handles basic authentication. * - * @param EventInterface $event An EventInterface instance + * @param RequestEventArgs $eventArgs A RequestEventArgs instance */ - public function handle(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { $request = $event->get('request'); diff --git a/Http/Firewall/ChannelListener.php b/Http/Firewall/ChannelListener.php index 49cef7e..2e6be82 100644 --- a/Http/Firewall/ChannelListener.php +++ b/Http/Firewall/ChannelListener.php @@ -14,8 +14,9 @@ namespace Symfony\Component\Security\Http\Firewall; use Symfony\Component\Security\Http\AccessMap; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\HttpKernel\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; +use Symfony\Component\HttpKernel\Events; +use Doctrine\Common\EventManager; /** * ChannelListener switches the HTTP protocol based on the access control @@ -39,29 +40,28 @@ class ChannelListener implements ListenerInterface /** * * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'handle'), 0); + $evm->addEventListener(Events::onCoreSecurity, $this); } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { } /** * Handles channel management. * - * @param EventInterface $event An EventInterface instance + * @param RequestEventArgs $eventArgs A RequestEventArgs instance */ - public function handle(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { - $request = $event->get('request'); + $request = $eventArgs->getRequest(); list($attributes, $channel) = $this->map->getPatterns($request); @@ -70,9 +70,11 @@ class ChannelListener implements ListenerInterface $this->logger->debug('Redirecting to HTTPS'); } - $event->setProcessed(); + $response = $this->authenticationEntryPoint->start($eventArgs, $request); - return $this->authenticationEntryPoint->start($event, $request); + $eventArgs->setResponse($response); + + return; } if ('http' === $channel && $request->isSecure()) { @@ -80,9 +82,9 @@ class ChannelListener implements ListenerInterface $this->logger->debug('Redirecting to HTTP'); } - $event->setProcessed(); + $response = $this->authenticationEntryPoint->start($eventArgs, $request); - return $this->authenticationEntryPoint->start($event, $request); + $eventArgs->setResponse($response); } } } diff --git a/Http/Firewall/ContextListener.php b/Http/Firewall/ContextListener.php index edc2f8c..d8b837f 100644 --- a/Http/Firewall/ContextListener.php +++ b/Http/Firewall/ContextListener.php @@ -11,17 +11,17 @@ namespace Symfony\Component\Security\Http\Firewall; -use Symfony\Component\EventDispatcher\EventInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Log\LoggerInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\Exception\UnsupportedAccountException; use Symfony\Component\Security\Core\SecurityContext; use Symfony\Component\Security\Core\User\AccountInterface; +use Doctrine\Common\EventManager; /** * ContextListener manages the SecurityContext persistence through a session. @@ -49,34 +49,35 @@ class ContextListener implements ListenerInterface } /** - * Registers a core.security listener to load the SecurityContext from the + * Registers a onCoreSecurity listener to load the SecurityContext from the * session. * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'read'), 0); - $dispatcher->connect('core.response', array($this, 'write'), 0); + $evm->addEventListener( + array(Events::onCoreSecurity, Events::filterCoreResponse), + $this + ); } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { - $dispatcher->disconnect('core.response', array($this, 'write')); + $evm->removeEventListener(Events::filterCoreResponse, $this); } /** * Reads the SecurityContext from the session. * - * @param EventInterface $event An EventInterface instance + * @param RequestEventArgs $eventArgs A RequestEventArgs instance */ - public function read(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { - $request = $event->get('request'); + $request = $eventArgs->getRequest(); $session = $request->hasSession() ? $request->getSession() : null; @@ -102,27 +103,25 @@ class ContextListener implements ListenerInterface * * @param EventInterface $event An EventInterface instance */ - public function write(EventInterface $event, Response $response) + public function filterCoreResponse(RequestEventArgs $eventArgs) { - if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) { - return $response; + if (HttpKernelInterface::MASTER_REQUEST !== $eventArgs->getRequestType()) { + return; } if (null === $token = $this->context->getToken()) { - return $response; + return; } if (null === $token || $token instanceof AnonymousToken) { - return $response; + return; } if (null !== $this->logger) { $this->logger->debug('Write SecurityContext in the session'); } - $event->get('request')->getSession()->set('_security_'.$this->contextKey, serialize($token)); - - return $response; + $eventArgs->getRequest()->getSession()->set('_security_'.$this->contextKey, serialize($token)); } /** diff --git a/Http/Firewall/DigestAuthenticationListener.php b/Http/Firewall/DigestAuthenticationListener.php index 490e409..a4f17ef 100644 --- a/Http/Firewall/DigestAuthenticationListener.php +++ b/Http/Firewall/DigestAuthenticationListener.php @@ -15,8 +15,8 @@ use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Http\EntryPoint\DigestAuthenticationEntryPoint; use Symfony\Component\HttpKernel\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; +use Symfony\Component\HttpKernel\Events; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Exception\BadCredentialsException; use Symfony\Component\Security\Core\Exception\AuthenticationServiceException; @@ -24,6 +24,7 @@ use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\Exception\NonceExpiredException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Exception\AuthenticationException; +use Doctrine\Common\EventManager; /** * DigestAuthenticationListener implements Digest HTTP authentication. @@ -54,29 +55,28 @@ class DigestAuthenticationListener implements ListenerInterface /** * * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'handle'), 0); + $evm->addEventListener(Events::onCoreSecurity, $this); } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { } /** * Handles digest authentication. * - * @param EventInterface $event An EventInterface instance + * @param RequestEventArgs $eventArgs A RequestEventArgs instance */ - public function handle(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { - $request = $event->get('request'); + $request = $eventArgs->getRequest(); if (!$header = $request->server->get('PHP_AUTH_DIGEST')) { return; diff --git a/Http/Firewall/ExceptionListener.php b/Http/Firewall/ExceptionListener.php index 350b029..478147e 100644 --- a/Http/Firewall/ExceptionListener.php +++ b/Http/Firewall/ExceptionListener.php @@ -17,14 +17,14 @@ use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\HttpKernel\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\Events; +use Doctrine\Common\EventManager; /** * ExceptionListener catches authentication exception and converts them to @@ -52,33 +52,32 @@ class ExceptionListener implements ListenerInterface } /** - * Registers a core.exception listener to take care of security exceptions. + * Registers a onCoreException listener to take care of security exceptions. * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.exception', array($this, 'handleException'), 0); + $evm->connect(Events::onCoreException, $this); } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { - $dispatcher->disconnect('core.exception', array($this, 'handleException')); + $evm->disconnect(Events::onCoreException, $this); } /** * Handles security related exceptions. * - * @param EventInterface $event An EventInterface instance + * @param ExceptionEventArgs $event An ExceptionEventArgs instance */ - public function handleException(EventInterface $event) + public function onCoreException(ExceptionEventArgs $eventArgs) { - $exception = $event->get('exception'); - $request = $event->get('request'); + $exception = $eventArgs->getException(); + $request = $eventArgs->getRequest(); if ($exception instanceof AuthenticationException) { if (null !== $this->logger) { @@ -86,9 +85,9 @@ class ExceptionListener implements ListenerInterface } try { - $response = $this->startAuthentication($event, $request, $exception); + $response = $this->startAuthentication($eventArgs, $request, $exception); } catch (\Exception $e) { - $event->set('exception', $e); + $eventArgs->set('exception', $e); return; } @@ -100,9 +99,9 @@ class ExceptionListener implements ListenerInterface } try { - $response = $this->startAuthentication($event, $request, new InsufficientAuthenticationException('Full authentication is required to access this resource.', $token, 0, $exception)); + $response = $this->startAuthentication($eventArgs, $request, new InsufficientAuthenticationException('Full authentication is required to access this resource.', $token, 0, $exception)); } catch (\Exception $e) { - $event->set('exception', $e); + $eventArgs->set('exception', $e); return; } @@ -113,7 +112,7 @@ class ExceptionListener implements ListenerInterface try { if (null !== $this->accessDeniedHandler) { - $response = $this->accessDeniedHandler->handle($event, $request, $exception); + $response = $this->accessDeniedHandler->handle($eventArgs, $request, $exception); if (!$response instanceof Response) { return; @@ -126,7 +125,7 @@ class ExceptionListener implements ListenerInterface $subRequest = Request::create($this->errorPage); $subRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $exception->getMessage()); - $response = $event->getSubject()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true); + $response = $eventArgs->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true); $response->setStatusCode(403); } } catch (\Exception $e) { @@ -134,7 +133,7 @@ class ExceptionListener implements ListenerInterface $this->logger->err(sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage())); } - $event->set('exception', new \RuntimeException('Exception thrown when handling an exception.', 0, $e)); + $eventArgs->setException(new \RuntimeException('Exception thrown when handling an exception.', 0, $e)); return; } @@ -143,12 +142,11 @@ class ExceptionListener implements ListenerInterface return; } - $event->setProcessed(); - - return $response; + $eventArgs->setHandled(true); + $eventArgs->setResponse($response); } - protected function startAuthentication(EventInterface $event, Request $request, AuthenticationException $authException) + protected function startAuthentication(ExceptionEventArgs $eventArgs, Request $request, AuthenticationException $authException) { $this->context->setToken(null); @@ -162,6 +160,6 @@ class ExceptionListener implements ListenerInterface $request->getSession()->set('_security.target_path', $request->getUri()); - return $this->authenticationEntryPoint->start($event, $request, $authException); + return $this->authenticationEntryPoint->start($eventArgs, $request, $authException); } } diff --git a/Http/Firewall/ListenerInterface.php b/Http/Firewall/ListenerInterface.php index afb2d9f..58f5a5d 100644 --- a/Http/Firewall/ListenerInterface.php +++ b/Http/Firewall/ListenerInterface.php @@ -11,11 +11,11 @@ namespace Symfony\Component\Security\Http\Firewall; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Doctrine\Common\EventManager; /** * Interface that must be implemented by firewall listeners - * + * * @author Johannes M. Schmitt <schmittjoh@gmail.com> */ interface ListenerInterface @@ -23,20 +23,20 @@ interface ListenerInterface /** * The implementation must connect this listener to all necessary events. * - * Typical events are: "core.security", and "core.response" + * Typical events are: "onCoreSecurity", and "filterCoreResponse" * - * @param EventDispatcherInterface $dispatcher + * @param EventManager $evm */ - function register(EventDispatcherInterface $dispatcher); + function register(EventManager $evm); /** * The implementation must remove this listener from any events that it had * connected to in register(). * - * It may remove this listener from "core.security", but this is ensured by + * It may remove this listener from "onCoreSecurity", but this is ensured by * the firewall anyway. * - * @param EventDispatcherInterface $dispatcher + * @param EventManager $evm */ - function unregister(EventDispatcherInterface $dispatcher); + function unregister(EventManager $evm); }
\ No newline at end of file diff --git a/Http/Firewall/LogoutListener.php b/Http/Firewall/LogoutListener.php index 1f5bc45..87abc30 100644 --- a/Http/Firewall/LogoutListener.php +++ b/Http/Firewall/LogoutListener.php @@ -15,10 +15,11 @@ 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; -use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\Kernel\Event\RequestEventArgs; +use Symfony\Component\Kernel\Events; +use Doctrine\Common\EventManager; /** * LogoutListener logout users. @@ -61,38 +62,37 @@ class LogoutListener implements ListenerInterface } /** - * Registers a core.security listener. + * Registers a onCoreSecurity listener. * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'handle'), 0); + $evm->addEventListener(Events::onCoreSecurity, $this); } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { } /** * Performs the logout if requested * - * @param EventInterface $event An EventInterface instance + * @param RequestEventArgs $eventArgs A RequestEventArgs instance */ - public function handle(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { - $request = $event->get('request'); + $request = $eventArgs->getRequest(); if ($this->logoutPath !== $request->getPathInfo()) { return; } if (null !== $this->successHandler) { - $response = $this->successHandler->onLogoutSuccess($event, $request); + $response = $this->successHandler->onLogoutSuccess($eventArgs, $request); if (!$response instanceof Response) { throw new \RuntimeException('Logout Success Handler did not return a Response.'); @@ -110,8 +110,6 @@ class LogoutListener implements ListenerInterface $this->securityContext->setToken(null); - $event->setProcessed(); - - return $response; + $event->setResponse($response); } } diff --git a/Http/Firewall/RememberMeListener.php b/Http/Firewall/RememberMeListener.php index db9b623..ed93792 100644 --- a/Http/Firewall/RememberMeListener.php +++ b/Http/Firewall/RememberMeListener.php @@ -2,18 +2,20 @@ namespace Symfony\Component\Security\Http\Firewall; -use Symfony\Component\EventDispatcher\Event; -use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; -use Symfony\Component\Security\Core\Exception\AuthenticationException; -use Symfony\Component\Security\Core\Exception\CookieTheftException; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Log\LoggerInterface; -use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; +use Symfony\Component\HttpKernel\Events as KernelEvents; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; +use Symfony\Component\Security\Core\Exception\AuthenticationException; +use Symfony\Component\Security\Core\Exception\CookieTheftException; use Symfony\Component\Security\Core\SecurityContext; use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface; +use Symfony\Component\Security\Http\Event\InteractiveLoginEventArgs; +use Symfony\Component\Security\Http\Events; +use Doctrine\Common\EventManager; /* * This file is part of the Symfony framework. @@ -36,7 +38,7 @@ class RememberMeListener implements ListenerInterface protected $authenticationManager; protected $logger; protected $lastState; - protected $eventDispatcher; + protected $evm; /** * Constructor @@ -55,33 +57,34 @@ class RememberMeListener implements ListenerInterface } /** - * Listen to core.security, and core.response event + * Listen to onCoreSecurity and filterCoreResponse event * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'checkCookies'), 0); - $dispatcher->connect('core.response', array($this, 'updateCookies'), 0); + $evm->addEventListener( + array(KernelEvents::onCoreSecurity, KernelEvents::filterCoreResponse), + $this + ); - $this->eventDispatcher = $dispatcher; + $this->evm = $evm; } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { - $dispatcher->disconnect('core.response', array($this, 'updateCookies')); + $evm->removeEventListener(KernelEvents::onCoreSecurity, $this); } /** * Handles remember-me cookie based authentication. * - * @param Event $event An Event instance + * @param RequestEventArgs $eventArgs A RequestEventArgs instance */ - public function checkCookies(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { $this->lastState = null; @@ -90,7 +93,7 @@ class RememberMeListener implements ListenerInterface } try { - if (null === $token = $this->rememberMeServices->autoLogin($event->get('request'))) { + if (null === $token = $this->rememberMeServices->autoLogin($eventArgs->getRequest())) { return; } @@ -101,8 +104,9 @@ class RememberMeListener implements ListenerInterface $this->securityContext->setToken($token); - if (null !== $this->eventDispatcher) { - $this->eventDispatcher->notify(new Event($this, 'security.interactive_login', array('request' => $event->get('request'), 'token' => $token))); + if (null !== $this->evm) { + $loginEventArgs = new InteractiveLoginEventArgs($eventArgs->getRequest(), $token); + $this->evm->dispatchEvent(Events::onSecurityInteractiveLogin, $loginEventArgs); } if (null !== $this->logger) { @@ -139,18 +143,16 @@ class RememberMeListener implements ListenerInterface * Update cookies * @param Event $event */ - public function updateCookies(EventInterface $event, Response $response) + public function filterCoreResponse(RequestEventArgs $eventArgs) { - if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) { - return $response; + if (HttpKernelInterface::MASTER_REQUEST !== $eventArgs->getRequestType()) { + return; } if ($this->lastState instanceof TokenInterface) { - $this->rememberMeServices->loginSuccess($event->get('request'), $response, $this->lastState); + $this->rememberMeServices->loginSuccess($eventArgs->getRequest(), $eventArgs->getResponse(), $this->lastState); } else if ($this->lastState instanceof AuthenticationException) { - $this->rememberMeServices->loginFail($event->get('request'), $response); + $this->rememberMeServices->loginFail($eventArgs->getRequest(), $eventArgs->getResponse()); } - - return $response; } }
\ No newline at end of file diff --git a/Http/Firewall/SwitchUserListener.php b/Http/Firewall/SwitchUserListener.php index 3adc1be..689bebf 100644 --- a/Http/Firewall/SwitchUserListener.php +++ b/Http/Firewall/SwitchUserListener.php @@ -11,14 +11,13 @@ namespace Symfony\Component\Security\Http\Firewall; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Core\User\AccountCheckerInterface; use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; use Symfony\Component\HttpKernel\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; +use Symfony\Component\HttpKernel\Events; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -27,6 +26,9 @@ use Symfony\Component\Security\Core\Role\SwitchUserRole; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Symfony\Component\Security\Http\Event\SwitchUserEventArgs; +use Symfony\Component\Security\Http\Events; +use Doctrine\Common\EventManager; /** * SwitchUserListener allows a user to impersonate another one temporarily @@ -44,7 +46,7 @@ class SwitchUserListener implements ListenerInterface protected $usernameParameter; protected $role; protected $logger; - protected $eventDispatcher; + protected $evm; /** * Constructor. @@ -68,31 +70,30 @@ class SwitchUserListener implements ListenerInterface /** * * - * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance - * @param integer $priority The priority + * @param EventManager $evm An EventManager instance */ - public function register(EventDispatcherInterface $dispatcher) + public function register(EventManager $evm) { - $dispatcher->connect('core.security', array($this, 'handle'), 0); + $evm->addEventListener(Events::onCoreSecurity, $this); - $this->eventDispatcher = $dispatcher; + $this->evm = $evm; } /** * {@inheritDoc} */ - public function unregister(EventDispatcherInterface $dispatcher) + public function unregister(EventManager $evm) { } /** * Handles digest authentication. * - * @param EventInterface $event An EventInterface instance + * @param RequestEventArgs $eventArgs A RequestEventArgs instance */ - public function handle(EventInterface $event) + public function onCoreSecurity(RequestEventArgs $eventArgs) { - $request = $event->get('request'); + $request = $eventArgs->getRequest(); if (!$request->get($this->usernameParameter)) { return; @@ -113,9 +114,7 @@ class SwitchUserListener implements ListenerInterface $request->server->set('QUERY_STRING', ''); $response = new RedirectResponse($request->getUri(), 302); - $event->setProcessed(); - - return $response; + $event->setResponse($response); } /** @@ -149,8 +148,9 @@ class SwitchUserListener implements ListenerInterface $token = new UsernamePasswordToken($user, $user->getPassword(), $this->providerKey, $roles); $token->setImmutable(true); - if (null !== $this->eventDispatcher) { - $this->eventDispatcher->notify(new Event($this, 'security.switch_user', array('request' => $request, 'target_user' => $token->getUser()))); + if (null !== $this->evm) { + $switchEventArgs = new SwitchUserEventArgs($request, $token->getUser()); + $this->evm->dispatchEvent(Events::onSecuritySwitchUser, $switchEventArgs); } return $token; @@ -169,8 +169,9 @@ class SwitchUserListener implements ListenerInterface throw new AuthenticationCredentialsNotFoundException(sprintf('Could not find original Token object.')); } - if (null !== $this->eventDispatcher) { - $this->eventDispatcher->notify(new Event($this, 'security.switch_user', array('request' => $request, 'target_user' => $original->getUser()))); + if (null !== $this->evm) { + $switchEventArgs = new SwitchUserEventArgs($request, $original->getUser()); + $this->evm->notify(Events::onSecuritySwitchUser, $switchEventArgs); } return $original; diff --git a/Http/Logout/LogoutSuccessHandlerInterface.php b/Http/Logout/LogoutSuccessHandlerInterface.php index 346784b..87153e7 100644 --- a/Http/Logout/LogoutSuccessHandlerInterface.php +++ b/Http/Logout/LogoutSuccessHandlerInterface.php @@ -3,7 +3,7 @@ namespace Symfony\Component\Security\Http\Logout; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\EventDispatcher\EventInterface; +use Symfony\Component\HttpKernel\Event\RequestEventArgs; /** * LogoutSuccesshandlerInterface. @@ -21,9 +21,9 @@ interface LogoutSuccessHandlerInterface /** * Creates a Response object to send upon a successful logout. * - * @param EventInterface $event + * @param RequestEventArgs $eventArgs * @param Request $request * @return Response never null */ - function onLogoutSuccess(EventInterface $event, Request $request); + function onLogoutSuccess(RequestEventArgs $eventArgs, Request $request); }
\ No newline at end of file |