【发布时间】: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,否则我们将无能为力。更好的做法是在登录侦听器中执行此操作。