晚上好, 当用户具有以下角色时,如何将用户“自动重定向”到/帐户区域? ROLE_USER,所以当他进行身份验证/已登录? (不是匿名的)
我想阻止用户访问“标准”fos userbundle页面:
“登录表单”,“注册表单”和“密码重置”表单,
他登录时 如果用户已经登录并且可以第二次登录或重置密码或再次注册,那对我来说没有意义..
处理这个问题的最佳方法是什么?
此致
答案 0 :(得分:1)
在评论中扩展我的答案。
我能想到的最佳方法是倾听kernel.controller
事件。然后在此事件中检查控制器是否在您列入黑名单的控制器列表中,以决定是否通过例外转发您的用户。
EventSubscriber
这将侦听kernel.controller
事件。然后,它将检查控制器是否是您要在用户登录时想要丢失的3个FOSUserBundle控制器之一。如果控制器是其中之一,则抛出异常然后被kernel.exception
事件捕获。如果异常是指定的异常,则将用户转发到您在重定向响应中声明的路由。
namespace Acme\UserBundle\EventSubscriber;
use Acme\UserBundle\Exception\FOSUserRedirectException;
use FOS\UserBundle\Controller\RegistrationController as FOSRegistrationController;
use FOS\UserBundle\Controller\ResettingController as FOSResettingController;
use FOS\UserBundle\Controller\SecurityController as FOSSecurityController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
class FOSUserRedirectSubscriber implements EventSubscriberInterface
{
protected $securityContext;
protected $router;
public function __construct(
SecurityContextInterface $securityContext,
UrlGeneratorInterface $router
) {
$this->securityContext = $securityContext;
$this->router = $router;
}
public static function getSubscribedEvents()
{
return array(
KernelEvents::CONTROLLER => 'onKernelController',
KernelEvents::EXCEPTION => 'onKernelException',
);
}
/**
* Check to see whether current user is logged in
* If controller is one of specified throw FOSUserRedirectException
*
* @param FilterControllerEvent $event
* @throws FOSUserRedirectException
*/
public function onKernelController(FilterControllerEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType() ||
!$this->securityContext->isGranted('ROLE_USER')
) {
return;
}
$controller = $event->getController();
if ($controller[0] instanceof FOSRegistrationController ||
$controller[0] instanceof FOSResettingController ||
$controller[0] instanceof FOSSecurityController
) {
throw new FOSUserRedirectException();
}
}
/**
* If user is logged in but has loaded one of the specified
* FOSUserBundle controllers
*
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if (!$exception instanceof FOSUserRedirectException) {
return;
}
$url = $this->router->generate('**THE_ROUTE_YOU_WISH_TO_REDIRECT_TO**');
$response = new RedirectResponse($url);
$event->setResponse($response);
}
}
异常
namespace Acme\UserBundle\Exception;
class FOSUserRedirectException extends \Exception
{
}
service.yml
parameters:
acme_user.subscriber.fos_redirect.class: Acme\UserBundle\EventSubscriber\FOSUserRedirectSubscriber
services:
acme_user.subscriber.fos_redirect:
class: %acme_user.subscriber.fos_redirect.class%
arguments:
- @security.context
- @router
tags:
- { name: kernel.event_subscriber }
答案 1 :(得分:0)
您可以创建一个侦听IntercativeLoginEvent的侦听器。当它发生时,您可以检查哪个角色具有经过身份验证的用户,并将其重定向到您希望将其重定向到的任何位置。
看看这些页面:
http://symfony.com/doc/current/components/security/authentication.html http://www.webtipblog.com/create-authentication-listener-symfony-2/
希望,这有帮助。