浏览代码

added two events "security.interactive_login", and "security.switch_user"

Johannes M. Schmitt 14 年之前
父节点
当前提交
8ccb8eb8c2

+ 3 - 1
src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php

@@ -224,7 +224,9 @@ abstract class AbstractAuthenticationListener implements ListenerInterface
         $session->remove(SecurityContext::AUTHENTICATION_ERROR);
         $session->remove(SecurityContext::LAST_USERNAME);
 
-        $this->eventDispatcher->notify(new Event($this, 'security.login_success', array('request' => $request, 'token' => $token)));
+        if (null !== $this->eventDispatcher) {
+            $this->eventDispatcher->notify(new Event($this, 'security.interactive_login', array('request' => $request, 'token' => $token)));
+        }
 
         if (null !== $this->successHandler) {
             $response = $this->successHandler->onAuthenticationSuccess($request, $token);

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

@@ -33,6 +33,7 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface
     protected $authenticationManager;
     protected $providerKey;
     protected $logger;
+    protected $eventDispatcher;
 
     public function __construct(SecurityContext $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, LoggerInterface $logger = null)
     {
@@ -51,6 +52,8 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface
     public function register(EventDispatcherInterface $dispatcher)
     {
         $dispatcher->connect('core.security', array($this, 'handle'), 0);
+
+        $this->eventDispatcher = $dispatcher;
     }
 
     /**
@@ -96,6 +99,10 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface
                 $this->logger->debug(sprintf('Authentication success: %s', $token));
             }
             $this->securityContext->setToken($token);
+
+            if (null !== $this->eventDispatcher) {
+                $this->eventDispatcher->notify(new Event($this, 'security.interactive_login', array('request' => $request, 'token' => $token)));
+            }
         } catch (AuthenticationException $failed) {
             $this->securityContext->setToken(null);
 

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

@@ -35,6 +35,7 @@ class RememberMeListener implements ListenerInterface
     protected $authenticationManager;
     protected $logger;
     protected $lastState;
+    protected $eventDispatcher;
 
     /**
      * Constructor
@@ -62,6 +63,8 @@ class RememberMeListener implements ListenerInterface
     {
         $dispatcher->connect('core.security', array($this, 'checkCookies'), 0);
         $dispatcher->connect('core.response', array($this, 'updateCookies'), 0);
+
+        $this->eventDispatcher = $dispatcher;
     }
 
     /**
@@ -97,6 +100,10 @@ class RememberMeListener 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->logger) {
                     $this->logger->debug('SecurityContext populated with remember-me token.');
                 }

+ 11 - 0
src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php

@@ -42,6 +42,7 @@ class SwitchUserListener implements ListenerInterface
     protected $usernameParameter;
     protected $role;
     protected $logger;
+    protected $eventDispatcher;
 
     /**
      * Constructor.
@@ -71,6 +72,8 @@ class SwitchUserListener implements ListenerInterface
     public function register(EventDispatcherInterface $dispatcher)
     {
         $dispatcher->connect('core.security', array($this, 'handle'), 0);
+
+        $this->eventDispatcher = $dispatcher;
     }
 
     /**
@@ -145,6 +148,10 @@ 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())));
+        }
+
         return $token;
     }
 
@@ -161,6 +168,10 @@ 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())));
+        }
+
         return $original;
     }