Browse Source

[Security] removed core.security event

Johannes Schmitt 14 years ago
parent
commit
d8022e34eb

+ 4 - 0
src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml

@@ -80,6 +80,7 @@
             <argument type="collection"></argument>
             <argument /> <!-- Provider Key -->
             <argument type="service" id="logger" on-invalid="null" />
+            <argument type="service" id="event_dispatcher" on-invalid="null"/>
         </service>
 
         <service id="security.logout_listener" class="%security.logout_listener.class%" public="false" abstract="true">
@@ -102,6 +103,7 @@
             <argument type="service" id="security.authentication.success_handler" on-invalid="null" />
             <argument type="service" id="security.authentication.failure_handler" on-invalid="null" />
             <argument type="service" id="logger" on-invalid="null" />
+            <argument type="service" id="event_dispatcher" on-invalid="null"/>
         </service>
 
         <service id="security.authentication.listener.form" 
@@ -117,6 +119,7 @@
             <argument>%security.authentication.x509.user%</argument>
             <argument>%security.authentication.x509.credentials%</argument>
             <argument type="service" id="logger" on-invalid="null" />
+            <argument type="service" id="event_dispatcher" on-invalid="null"/>
         </service>
 
         <service id="security.authentication.listener.basic" class="%security.authentication.listener.basic.class%" public="false" abstract="true">
@@ -163,6 +166,7 @@
             <argument /> <!--  Provider Key -->
             <argument type="service" id="security.access.decision_manager" />
             <argument type="service" id="logger" on-invalid="null" />
+            <argument type="service" id="event_dispatcher" on-invalid="null"/>
         </service>
 
         <service id="security.access_listener" class="%security.access_listener.class%" public="false">

+ 7 - 29
src/Symfony/Component/Security/Http/Firewall.php

@@ -32,19 +32,16 @@ use Symfony\Component\HttpFoundation\Request;
 class Firewall
 {
     private $map;
-    private $dispatcher;
-    private $currentListeners;
 
     /**
      * Constructor.
      *
      * @param FirewallMap $map A FirewallMap instance
      */
-    public function __construct(FirewallMapInterface $map, EventDispatcherInterface $dispatcher)
+    public function __construct(FirewallMapInterface $map)
     {
         $this->map = $map;
         $this->dispatcher = $dispatcher;
-        $this->currentListeners = array();
     }
 
     /**
@@ -58,38 +55,19 @@ class Firewall
             return;
         }
 
-        $request = $event->get('request');
-
-        // disconnect all listeners from core.security to avoid the overhead
-        // of most listeners having to do this manually
-        $this->dispatcher->disconnect('core.security');
-
-        // ensure that listeners disconnect from wherever they have connected to
-        foreach ($this->currentListeners as $listener) {
-            $listener->unregister($this->dispatcher);
-        }
-
         // register listeners for this firewall
-        list($listeners, $exception) = $this->map->getListeners($request);
+        list($listeners, $exception) = $this->map->getListeners($event->get('request'));
         if (null !== $exception) {
             $exception->register($this->dispatcher);
         }
-        foreach ($listeners as $listener) {
-            $listener->register($this->dispatcher);
-        }
-
-        // save current listener instances
-        $this->currentListeners = $listeners;
-        if (null !== $exception) {
-            $this->currentListeners[] = $exception;
-        }
 
         // initiate the listener chain
-        $ret = $this->dispatcher->notifyUntil($securityEvent = new Event($request, 'core.security', array('request' => $request)));
-        if ($securityEvent->isProcessed()) {
-            $event->setProcessed();
+        foreach ($listeners as $listener) {
+            $response = $listener->handle($event);
 
-            return $ret;
+            if ($event->isProcessed()) {
+                return $response;
+            }
         }
     }
 }

+ 2 - 21
src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php

@@ -66,7 +66,7 @@ abstract class AbstractAuthenticationListener implements ListenerInterface
      * @param array                          $options               An array of options for the processing of a successful, or failed authentication attempt
      * @param LoggerInterface                $logger                A LoggerInterface instance
      */
-    public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, $providerKey, array $options = array(), AuthenticationSuccessHandlerInterface $successHandler = null, AuthenticationFailureHandlerInterface $failureHandler = null, LoggerInterface $logger = null)
+    public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, $providerKey, array $options = array(), AuthenticationSuccessHandlerInterface $successHandler = null, AuthenticationFailureHandlerInterface $failureHandler = null, LoggerInterface $logger = null, EventDispatcherInterface $eventDispatcher = null)
     {
         if (empty($providerKey)) {
             throw new \InvalidArgumentException('$providerKey must not be empty.');
@@ -89,6 +89,7 @@ abstract class AbstractAuthenticationListener implements ListenerInterface
             'failure_forward'                => false,
         ), $options);
         $this->logger = $logger;
+        $this->eventDispatcher = $eventDispatcher;
     }
 
     /**
@@ -101,26 +102,6 @@ abstract class AbstractAuthenticationListener implements ListenerInterface
         $this->rememberMeServices = $rememberMeServices;
     }
 
-    /**
-     * Subscribe to the core.security event
-     *
-     * @param EventDispatcher $dispatcher An EventDispatcher instance
-     * @param integer         $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'handle'), 0);
-
-        $this->eventDispatcher = $dispatcher;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
-    }
-
     /**
      * Handles form based authentication.
      *

+ 2 - 21
src/Symfony/Component/Security/Http/Firewall/AbstractPreAuthenticatedListener.php

@@ -36,32 +36,13 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface
     private $providerKey;
     private $eventDispatcher;
 
-    public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, LoggerInterface $logger = null)
+    public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, LoggerInterface $logger = null, EventDispatcherInterface $eventDispatcher = null)
     {
         $this->securityContext = $securityContext;
         $this->authenticationManager = $authenticationManager;
         $this->providerKey = $providerKey;
         $this->logger = $logger;
-    }
-
-    /**
-     *
-     *
-     * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
-     * @param integer                  $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'handle'), 0);
-
-        $this->eventDispatcher = $dispatcher;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
+        $this->eventDispatcher = $eventDispatcher;
     }
 
     /**

+ 0 - 18
src/Symfony/Component/Security/Http/Firewall/AccessListener.php

@@ -43,24 +43,6 @@ class AccessListener implements ListenerInterface
         $this->logger = $logger;
     }
 
-    /**
-     * Registers a core.security listener to enforce authorization rules.
-     *
-     * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
-     * @param integer                  $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'handle'), 0);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
-    }
-
     /**
      * Handles access authorization.
      *

+ 0 - 19
src/Symfony/Component/Security/Http/Firewall/AnonymousAuthenticationListener.php

@@ -36,25 +36,6 @@ class AnonymousAuthenticationListener implements ListenerInterface
         $this->logger  = $logger;
     }
 
-    /**
-     * Registers a core.security listener to load the SecurityContext from the
-     * session.
-     *
-     * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
-     * @param integer                  $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'handle'), 0);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
-    }
-
     /**
      * Handles anonymous authentication.
      *

+ 0 - 18
src/Symfony/Component/Security/Http/Firewall/BasicAuthenticationListener.php

@@ -48,24 +48,6 @@ class BasicAuthenticationListener implements ListenerInterface
         $this->ignoreFailure = false;
     }
 
-    /**
-     *
-     *
-     * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
-     * @param integer                  $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'handle'), 0);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
-    }
-
     /**
      * Handles basic authentication.
      *

+ 0 - 18
src/Symfony/Component/Security/Http/Firewall/ChannelListener.php

@@ -36,24 +36,6 @@ class ChannelListener implements ListenerInterface
         $this->logger = $logger;
     }
 
-    /**
-     *
-     *
-     * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
-     * @param integer                  $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'handle'), 0);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
-    }
-
     /**
      * Handles channel management.
      *

+ 5 - 23
src/Symfony/Component/Security/Http/Firewall/ContextListener.php

@@ -36,7 +36,7 @@ class ContextListener implements ListenerInterface
     private $logger;
     private $userProviders;
 
-    public function __construct(SecurityContext $context, array $userProviders, $contextKey, LoggerInterface $logger = null)
+    public function __construct(SecurityContext $context, array $userProviders, $contextKey, LoggerInterface $logger = null, EventDispatcherInterface $eventDispatcher = null)
     {
         if (empty($contextKey)) {
             throw new \InvalidArgumentException('$contextKey must not be empty.');
@@ -45,28 +45,10 @@ class ContextListener implements ListenerInterface
         $this->context = $context;
         $this->userProviders = $userProviders;
         $this->contextKey = $contextKey;
-        $this->logger = $logger;
-    }
 
-    /**
-     * Registers a core.security listener to load the SecurityContext from the
-     * session.
-     *
-     * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
-     * @param integer                  $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'read'), 0);
-        $dispatcher->connect('core.response', array($this, 'write'), 0);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->disconnect('core.response', array($this, 'write'));
+        if (null !== $this->eventDispatcher) {
+            $this->eventDispatcher->connect('core.response', array($this, 'write'), 0);
+        }
     }
 
     /**
@@ -74,7 +56,7 @@ class ContextListener implements ListenerInterface
      *
      * @param EventInterface $event An EventInterface instance
      */
-    public function read(EventInterface $event)
+    public function handle(EventInterface $event)
     {
         $request = $event->get('request');
 

+ 0 - 18
src/Symfony/Component/Security/Http/Firewall/DigestAuthenticationListener.php

@@ -51,24 +51,6 @@ class DigestAuthenticationListener implements ListenerInterface
         $this->logger = $logger;
     }
 
-    /**
-     *
-     *
-     * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
-     * @param integer                  $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'handle'), 0);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
-    }
-
     /**
      * Handles digest authentication.
      *

+ 0 - 8
src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php

@@ -62,14 +62,6 @@ class ExceptionListener implements ListenerInterface
         $dispatcher->connect('core.exception', array($this, 'handleException'), 0);
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->disconnect('core.exception', array($this, 'handleException'));
-    }
-
     /**
      * Handles security related exceptions.
      *

+ 6 - 17
src/Symfony/Component/Security/Http/Firewall/ListenerInterface.php

@@ -11,32 +11,21 @@
 
 namespace Symfony\Component\Security\Http\Firewall;
 
+use Symfony\Component\EventDispatcher\EventInterface;
+
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 /**
  * Interface that must be implemented by firewall listeners
- * 
+ *
  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  */
 interface ListenerInterface
 {
     /**
-     * The implementation must connect this listener to all necessary events.
-     *
-     * Typical events are: "core.security", and "core.response"
-     *
-     * @param EventDispatcherInterface $dispatcher
-     */
-    function register(EventDispatcherInterface $dispatcher);
-
-    /**
-     * 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
-     * the firewall anyway.
+     * This interface must be implemented by firewall listeners.
      *
-     * @param EventDispatcherInterface $dispatcher
+     * @param EventInterface $dispatcher
      */
-    function unregister(EventDispatcherInterface $dispatcher);
+    function handle(EventInterface $event);
 }

+ 0 - 18
src/Symfony/Component/Security/Http/Firewall/LogoutListener.php

@@ -60,24 +60,6 @@ class LogoutListener implements ListenerInterface
         $this->handlers[] = $handler;
     }
 
-    /**
-     * Registers a core.security listener.
-     *
-     * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
-     * @param integer                  $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'handle'), 0);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
-    }
-
     /**
      * Performs the logout if requested
      *

+ 3 - 22
src/Symfony/Component/Security/Http/Firewall/RememberMeListener.php

@@ -45,32 +45,13 @@ class RememberMeListener implements ListenerInterface
      * @param AuthenticationManagerInterface $authenticationManager
      * @param LoggerInterface $logger
      */
-    public function __construct(SecurityContext $securityContext, RememberMeServicesInterface $rememberMeServices, AuthenticationManagerInterface $authenticationManager, LoggerInterface $logger = null)
+    public function __construct(SecurityContext $securityContext, RememberMeServicesInterface $rememberMeServices, AuthenticationManagerInterface $authenticationManager, LoggerInterface $logger = null, EventDispatcherInterface $eventDispatcher = null)
     {
         $this->securityContext = $securityContext;
         $this->rememberMeServices = $rememberMeServices;
         $this->authenticationManager = $authenticationManager;
         $this->logger = $logger;
-    }
-
-    /**
-     * Listen to core.security, and core.response event
-     *
-     * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
-     * @param integer         $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'checkCookies'), 0);
-
-        $this->eventDispatcher = $dispatcher;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
+        $this->eventDispatcher = $eventDispatcher;
     }
 
     /**
@@ -78,7 +59,7 @@ class RememberMeListener implements ListenerInterface
      *
      * @param Event $event An Event instance
      */
-    public function checkCookies(EventInterface $event)
+    public function handle(EventInterface $event)
     {
         if (null !== $this->securityContext->getToken()) {
             return;

+ 2 - 21
src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php

@@ -49,7 +49,7 @@ class SwitchUserListener implements ListenerInterface
     /**
      * Constructor.
      */
-    public function __construct(SecurityContextInterface $securityContext, UserProviderInterface $provider, UserCheckerInterface $userChecker, $providerKey, AccessDecisionManagerInterface $accessDecisionManager, LoggerInterface $logger = null, $usernameParameter = '_switch_user', $role = 'ROLE_ALLOWED_TO_SWITCH')
+    public function __construct(SecurityContextInterface $securityContext, UserProviderInterface $provider, UserCheckerInterface $userChecker, $providerKey, AccessDecisionManagerInterface $accessDecisionManager, LoggerInterface $logger = null, $usernameParameter = '_switch_user', $role = 'ROLE_ALLOWED_TO_SWITCH', EventDispatcherInterface $eventDispatcher = null)
     {
         if (empty($providerKey)) {
             throw new \InvalidArgumentException('$providerKey must not be empty.');
@@ -63,26 +63,7 @@ class SwitchUserListener implements ListenerInterface
         $this->usernameParameter = $usernameParameter;
         $this->role = $role;
         $this->logger = $logger;
-    }
-
-    /**
-     *
-     *
-     * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
-     * @param integer                  $priority   The priority
-     */
-    public function register(EventDispatcherInterface $dispatcher)
-    {
-        $dispatcher->connect('core.security', array($this, 'handle'), 0);
-
-        $this->eventDispatcher = $dispatcher;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function unregister(EventDispatcherInterface $dispatcher)
-    {
+        $this->eventDispatcher = $eventDispatcher;
     }
 
     /**

+ 5 - 4
src/Symfony/Component/Security/Http/Firewall/UsernamePasswordFormAuthenticationListener.php

@@ -11,6 +11,8 @@
 
 namespace Symfony\Component\Security\Http\Firewall;
 
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+
 use Symfony\Component\Form\CsrfProvider\CsrfProviderInterface;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpKernel\Log\LoggerInterface;
@@ -35,7 +37,7 @@ class UsernamePasswordFormAuthenticationListener extends AbstractAuthenticationL
     /**
      * {@inheritdoc}
      */
-    public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, $providerKey, array $options = array(), AuthenticationSuccessHandlerInterface $successHandler = null, AuthenticationFailureHandlerInterface $failureHandler = null, LoggerInterface $logger = null, CsrfProviderInterface $csrfProvider = null)
+    public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, $providerKey, array $options = array(), AuthenticationSuccessHandlerInterface $successHandler = null, AuthenticationFailureHandlerInterface $failureHandler = null, LoggerInterface $logger = null, EventDispatcherInterface $eventDispatcher = null, CsrfProviderInterface $csrfProvider = null)
     {
         parent::__construct($securityContext, $authenticationManager, $sessionStrategy, $providerKey, array_merge(array(
             'username_parameter' => '_username',
@@ -43,7 +45,7 @@ class UsernamePasswordFormAuthenticationListener extends AbstractAuthenticationL
             'csrf_parameter'     => '_csrf_token',
             'csrf_page_id'       => 'form_login',
             'post_only'          => true,
-        ), $options), $successHandler, $failureHandler, $logger);
+        ), $options), $successHandler, $failureHandler, $logger, $eventDispatcher);
 
         $this->csrfProvider = $csrfProvider;
     }
@@ -76,5 +78,4 @@ class UsernamePasswordFormAuthenticationListener extends AbstractAuthenticationL
 
         return $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $password, $this->providerKey));
     }
-}
-
+}

+ 3 - 2
src/Symfony/Component/Security/Http/Firewall/X509AuthenticationListener.php

@@ -11,6 +11,7 @@
 
 namespace Symfony\Component\Security\Http\Firewall;
 
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Symfony\Component\Security\Core\SecurityContextInterface;
 use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
 use Symfony\Component\HttpKernel\Log\LoggerInterface;
@@ -27,9 +28,9 @@ class X509AuthenticationListener extends AbstractPreAuthenticatedListener
     protected $userKey;
     protected $credentialKey;
 
-    public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, $userKey = 'SSL_CLIENT_S_DN_Email', $credentialKey = 'SSL_CLIENT_S_DN', LoggerInterface $logger = null)
+    public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, $userKey = 'SSL_CLIENT_S_DN_Email', $credentialKey = 'SSL_CLIENT_S_DN', LoggerInterface $logger = null, EventDispatcherInterface $eventDispatcher = null)
     {
-        parent::__construct($securityContext, $authenticationManager, $providerKey, $logger);
+        parent::__construct($securityContext, $authenticationManager, $providerKey, $logger, $eventDispatcher);
 
         $this->userKey = $userKey;
         $this->credentialKey = $credentialKey;