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

This commit is contained in:
Johannes M. Schmitt 2011-01-26 13:12:18 +01:00 committed by Fabien Potencier
parent 00d3d8c3bc
commit 8ccb8eb8c2
4 changed files with 28 additions and 1 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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.');
}

View File

@ -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;
}