added authentication success/failure events

This commit is contained in:
Johannes Schmitt 2011-07-07 21:28:39 +02:00 committed by Dariusz Górecki
parent ec30dcb31d
commit cf09c2db55
5 changed files with 113 additions and 0 deletions

View File

@ -54,6 +54,9 @@
<service id="security.authentication.manager" class="%security.authentication.manager.class%" public="false">
<argument type="collection" />
<argument>%security.authentication.manager.erase_credentials%</argument>
<call method="setEventDispatcher">
<argument type="service" id="event_dispatcher" />
</call>
</service>
<service id="security.authentication.trust_resolver" class="%security.authentication.trust_resolver.class%" public="false">

View File

@ -11,6 +11,10 @@
namespace Symfony\Component\Security\Core\Authentication;
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Exception\AccountStatusException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\ProviderNotFoundException;
@ -22,11 +26,13 @@ use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
* instances to authenticate a Token.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class AuthenticationProviderManager implements AuthenticationManagerInterface
{
private $providers;
private $eraseCredentials;
private $eventDispatcher;
/**
* Constructor.
@ -44,6 +50,11 @@ class AuthenticationProviderManager implements AuthenticationManagerInterface
$this->eraseCredentials = (Boolean) $eraseCredentials;
}
public function setEventDispatcher(EventDispatcherInterface $dispatcher)
{
$this->eventDispatcher = $dispatcher;
}
/**
* {@inheritdoc}
*/
@ -77,6 +88,10 @@ class AuthenticationProviderManager implements AuthenticationManagerInterface
$result->eraseCredentials();
}
if (null !== $this->eventDispatcher) {
$this->eventDispatcher->dispatch(AuthenticationEvents::AUTHENTICATION_SUCCESS, new AuthenticationEvent($result));
}
return $result;
}
@ -84,6 +99,10 @@ class AuthenticationProviderManager implements AuthenticationManagerInterface
$lastException = new ProviderNotFoundException(sprintf('No Authentication Provider found for token of class "%s".', get_class($token)));
}
if (null !== $this->eventDispatcher) {
$this->eventDispatcher->dispatch(AuthenticationEvents::AUTHENTICATION_FAILURE, new AuthenticationFailureEvent($token, $lastException));
}
$lastException->setExtraInformation($token);
throw $lastException;

View File

@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core;
final class AuthenticationEvents
{
const AUTHENTICATION_SUCCESS = 'security.authentication.success';
const AUTHENTICATION_FAILURE = 'security.authentication.failure';
}

View File

@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Event;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\EventDispatcher\Event;
/**
* This is a general purpose authentication event.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class AuthenticationEvent extends Event
{
private $authenticationToken;
public function __construct(TokenInterface $token)
{
$this->authenticationToken = $token;
}
public function getAuthenticationToken()
{
return $this->authenticationToken;
}
}

View File

@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Event;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
/**
* This event is dispatched on authentication failure.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class AuthenticationFailureEvent extends AuthenticationEvent
{
private $authenticationException;
public function __construct(TokenInterface $token, AuthenticationException $ex)
{
parent::__construct($token);
$this->authenticationException = $ex;
}
public function getAuthenticationException()
{
return $this->authenticationException;
}
}