ログインイベントをリッスンするには、イベントリスナーが必要です。次に、役割に基づいてクライアントを別のページにルーティングできます。
services.yml:
services:
login_listener:
class: Acme\UserBundle\Listener\LoginListener
arguments: [@security.context, @doctrine]
tags:
- { name: kernel.event_listener, event: security.interactive_login }
LoginListener:
<?php
namespace Acme\UserBundle\Listener;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Security\Core\SecurityContext;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine; // for Symfony 2.1.x
// use Symfony\Bundle\DoctrineBundle\Registry as Doctrine; // for Symfony 2.0.x
/**
* Custom login listener.
*/
class LoginListener
{
/** @var \Symfony\Component\Security\Core\SecurityContext */
private $securityContext;
/** @var \Doctrine\ORM\EntityManager */
private $em;
/**
* Constructor
*
* @param SecurityContext $securityContext
* @param Doctrine $doctrine
*/
public function __construct(SecurityContext $securityContext, Doctrine $doctrine)
{
$this->securityContext = $securityContext;
$this->em = $doctrine->getEntityManager();
}
/**
* Do the magic.
*
* @param Event $event
*/
public function onSecurityInteractiveLogin(Event $event)
{
if ($this->securityContext->isGranted('ROLE_1')) {
// redirect 1
}
if ($this->securityContext->isGranted('ROLE_2')) {
// redirect 2
}
// do some other magic here
$user = $this->securityContext->getToken()->getUser();
// ...
}
}
差出人:http ://www.metod.si/login-event-listener-in-symfony2/