【问题标题】:Symfony route redirection based on rolesSymfony 基于角色的路由重定向
【发布时间】:2020-09-17 16:36:52
【问题描述】:

我正在尝试在注册/登录后根据角色重定向路由。注册后,我将路由重定向到这个secure_area,根据用户角色进行路由。

/**
 * @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。如何让它根据用户角色重定向路由?

security.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 }

【问题讨论】:

  • 可以添加`Security.yaml`文件的代码吗?
  • 取决于您的角色层次结构。 ROLE_USER1 在 ROLE_USER2 下吗?如果是,您需要先检查 ROLE_USER2。您应该按照 Dhia 的说明发布您的 security.yaml,否则我们将无能为力。更好的做法是在登录侦听器中执行此操作。

标签: symfony roles


【解决方案1】:
<?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")));
        }  
    }
}

您可以像这样实现LoginListener 来处理基于角色的重定向。

【讨论】:

  • 感谢您的回复。但是这个解决方案在 Symfony 4 /5 下不起作用。我在使用 use FOS\UserBundle\Model\UserManagerInterface; use Symfony\Component\Security\Core\SecurityContext; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; 时遇到问题
  • 这意味着你没有使用FOSUserBudndle。然后你可以参考这个链接symfonycasts.com/screencast/symfony-security/…
猜你喜欢
  • 2021-09-20
  • 1970-01-01
  • 2016-06-12
  • 2012-01-01
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
相关资源
最近更新 更多