1
我正在使用FOSUser Bundle for Symfony ...我的問題是; 我有兩個不同組的用戶....例如;教師和學生,它們在註冊系統時設置。 (使用FOSUser Bundle的用戶表)FOSUser Bundle,Symfony登陸頁面
成功登錄後,我想讓用戶轉到正確的登錄頁面。所以 如果用戶是老師,我希望用戶去/老師和爲學生/學生。
解決此問題的最佳方法是什麼?
感謝
我正在使用FOSUser Bundle for Symfony ...我的問題是; 我有兩個不同組的用戶....例如;教師和學生,它們在註冊系統時設置。 (使用FOSUser Bundle的用戶表)FOSUser Bundle,Symfony登陸頁面
成功登錄後,我想讓用戶轉到正確的登錄頁面。所以 如果用戶是老師,我希望用戶去/老師和爲學生/學生。
解決此問題的最佳方法是什麼?
感謝
您需要一個事件偵聽器來偵聽的登錄事件。然後,您可以根據其角色將客戶端路由到不同的頁面。
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();
// ...
}
}