From 8ccb8eb8c2c8a1aec2aa042366353476ca8af728 Mon Sep 17 00:00:00 2001 From: "Johannes M. Schmitt" Date: Wed, 26 Jan 2011 13:12:18 +0100 Subject: [PATCH] added two events "security.interactive_login", and "security.switch_user" --- .../Http/Firewall/AbstractAuthenticationListener.php | 4 +++- .../Firewall/AbstractPreAuthenticatedListener.php | 7 +++++++ .../Security/Http/Firewall/RememberMeListener.php | 7 +++++++ .../Security/Http/Firewall/SwitchUserListener.php | 11 +++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php b/src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php index 4bb205ffcd..5d50df3020 100644 --- a/src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php +++ b/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); diff --git a/src/Symfony/Component/Security/Http/Firewall/AbstractPreAuthenticatedListener.php b/src/Symfony/Component/Security/Http/Firewall/AbstractPreAuthenticatedListener.php index 1b1e8f8366..205a13f305 100644 --- a/src/Symfony/Component/Security/Http/Firewall/AbstractPreAuthenticatedListener.php +++ b/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); diff --git a/src/Symfony/Component/Security/Http/Firewall/RememberMeListener.php b/src/Symfony/Component/Security/Http/Firewall/RememberMeListener.php index e083492afd..bc741523f5 100644 --- a/src/Symfony/Component/Security/Http/Firewall/RememberMeListener.php +++ b/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.'); } diff --git a/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php b/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php index e80f0e6d01..2f18df91c7 100644 --- a/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php +++ b/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; }