基于角色的Symfony路由重定向
我正在尝试根据注册/登录后的角色重定向路由。注册后,我将路由重定向到此安全区域,以根据用户角色进行路由基于角色的Symfony路由重定向,symfony,roles,Symfony,Roles,我正在尝试根据注册/登录后的角色重定向路由。注册后,我将路由重定向到此安全区域,以根据用户角色进行路由 /** * @Route("/secure", name="secure_area") * * @throws \Exception */ public function indexAction() { if ($this->isGranted('ROLE_USER1')) { return $this->re
/**
* @Route("/secure", name="secure_area")
*
* @throws \Exception
*/
public function indexAction()
{
if ($this->isGranted('ROLE_USER1')) {
return $this->redirectToRoute('user1');
}
if ($this->isGranted('ROLE_USER2')) {
return $this->redirectToRoute('user2');
}
throw new \Exception(AccessDeniedException::class);
}
在这两种情况下,我都会到达路径user1。如何使其根据用户角色重定向路由
安全.yaml
role_hierarchy:
ROLE_ADMIN: ROLE_USER2
ROLE_USER2: ROLE_USER1
ROLE_USER1: ROLE_USER1
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/user2, roles: ROLE_USER2 }
- { path: ^/user1, roles: ROLE_USER1 }
您可以像这样实现LoginListener,以处理基于角色的重定向
您可以像这样实现LoginListener,以处理基于角色的重定向。能否添加'Security.yaml'文件的代码?取决于您的角色层次结构。角色\u USER1是否在角色\u USER2下?如果是,您需要先检查角色\u USER2。您应该发布security.yaml,正如Dhia所指出的,否则我们将无法提供太多帮助。更好的做法是在登录侦听器中执行此操作。能否添加'Security.yaml'文件的代码?取决于您的角色层次结构。角色\u USER1是否在角色\u USER2下?如果是,您需要先检查角色\u USER2。您应该发布security.yaml,正如Dhia所指出的,否则我们将无法提供太多帮助。更好的做法是在登录侦听器中执行此操作。感谢您的回复。但此解决方案在Symfony 4/5下不起作用。我在使用use FOS\UserBundle\Model\UserManagerInterface时遇到问题;使用Symfony\Component\Security\Core\SecurityContext;使用Symfony\Component\HttpKernel\Event\FilterResponseEvent;这意味着您没有使用fosuserbundle。然后你可以参考这个链接。谢谢你的回复。但此解决方案在Symfony 4/5下不起作用。我在使用use FOS\UserBundle\Model\UserManagerInterface时遇到问题;使用Symfony\Component\Security\Core\SecurityContext;使用Symfony\Component\HttpKernel\Event\FilterResponseEvent;这意味着您没有使用fosuserbundle。然后你可以参考这个链接。
<?php
// Change the namespace according to the location of this class in your bundle
namespace AppBundle\Listeners;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
class LoginListener
{
protected $userManager;
protected $router;
protected $security;
protected $dispatcher;
public function __construct(UserManagerInterface $userManager, Router $router, SecurityContext $security, EventDispatcher $dispatcher)
{
$this->userManager = $userManager;
$this->router = $router;
$this->security = $security;
$this->dispatcher = $dispatcher;
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$this->dispatcher->addListener(KernelEvents::RESPONSE, array($this, 'onKernelResponse'));
}
public function onKernelResponse(FilterResponseEvent $event)
{
// Important: redirect according to user Role
if ($this->security->isGranted('ROLE_ADMIN')) {
$event->setResponse(new RedirectResponse($this->router->generate("admin_homepage")));
} elseif ($this->security->isGranted('ROLE_MANAGER')) {
$event->setResponse(new RedirectResponse($this->router->generate("manager_homepage")));
} else {
$event->setResponse(new RedirectResponse($this->router->generate("default_homepage")));
}
}
}