Decision - Extension of default DefaultAuthenticationSuccessHandler Castom hendler LoginSuccessHandler.php:namespace AppBundle\Handler;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\HttpUtils;
class LoginSuccessHandler extends DefaultAuthenticationSuccessHandler
{
protected $router;
protected $authorizationChecker;
public function __construct(Router $router, AuthorizationChecker $authorizationChecker, HttpUtils $httpUtils, array $options = array())
{
$this->router = $router;
$this->authorizationChecker = $authorizationChecker;
parent::__construct($httpUtils, $options);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$response = null;
if ($this->authorizationChecker->isGranted('ROLE_ADMIN'))
{
$response = new RedirectResponse($this->router->generate('admin.books'));
}
else if ($this->authorizationChecker->isGranted('ROLE_USER'))
{
$response = new RedirectResponse($this->router->generate('catalog.books.list'));
}
return $response;
}
}
Fragmentation serviсes.ymlauthentication.handler.login_success_handler:
class: AppBundle\Handler\LoginSuccessHandler
arguments: ['@router', '@security.authorization_checker', '@security.http_utils', {}]
Firewall security.yml: firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
success_handler: authentication.handler.login_success_handler
# if you are using Symfony < 2.8, use the following config instead:
# csrf_provider: form.csrf_provider
logout:
path: /logout
target: /login
anonymous: true
Parent Hendler (for general painting) DefaultAuthenticationSuccessHandler.phpnamespace Symfony\Component\Security\Http\Authentication;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Security\Http\ParameterBagUtils;
/**
Class with the default authentication success handling logic.
@author Fabien Potencier <fabien@symfony.com>
@author Johannes M. Schmitt <schmittjoh@gmail.com>
@author Alexander <iam.asm89@gmail.com>
*/
class DefaultAuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
{
protected $httpUtils;
protected $options;
protected $providerKey;
protected $defaultOptions = array(
'always_use_default_target_path' => false,
'default_target_path' => '/',
'login_path' => '/login',
'target_path_parameter' => '_target_path',
'use_referer' => false,
);
/**
Constructor.
@param HttpUtils $httpUtils
@param array $options Options for processing a successful authentication attempt
*/
public function __construct(HttpUtils $httpUtils, array $options = array())
{
$this->httpUtils = $httpUtils;
$this->setOptions($options);
}
/**
{@inheritdoc}
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request));
}
//...
}