Symfony 5:方法注释上的@IsGranted 不适用于继承的角色

Symfony 5 : @IsGranted on method annotation doesn't work on inherited role

我(显然)搜索了 similar problems,但我的 none 没有。

这是我的案例:

这是我的控制器:

//src/Controller/UserController.php

/**
 * @Route("/users")
 * @IsGranted("ROLE_USER")
 */
class UserController extends AbstractController
{
    private $security;
    private $mailer;


    public function __construct(Security $security, MailerInterface $mailer)
    {
        $this->security = $security;
        $this->mailer = $mailer;
    }

    /**
     * @Route("/page/{!page}", name="user_index", requirements={"page"="\d+"}, defaults={"page":1})
     * @IsGranted({"ROLE_ADMIN", "ROLE_RESPONSIBLE"})
     */
    public function index(Request $request, UserRepository $userRepository, int $page = 1): Response
    {
[....]
}

还有我的自定义角色层次结构

config/packages/security.yaml

security:
    role_hierarchy:
        ROLE_RESPONSIBLE: ROLE_USER
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

我仔细检查了每个字符以防万一我有错字,我不...

我认为这就像一个 && 评估(用户必须获得 ROLE_ADMIN ROLE_RESPONSIBLE)。

编辑: 这就是问题所在,默认行为是对数组中每个角色的 && 评估,我需要使用 * @Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_RESPONSIBLE')")

我只尝试了 @IsGranted("ROLE_USER"),它起作用了,但 @IsGranted("ROLE_ADMIN") 不起作用,它是一个继承的角色

I (still) can't embed an image so take my word on that or see my proof here

提前致谢,星期一是星期一,你知道...

我认为您的管理员也需要 ROLE_RESPONSIBLE,因为您在 @isGranted() 注释中提供了一组角色。 /** * @Route("/page/{!page}", name="user_index", requirements={"page"="\d+"}, defaults={"page":1}) * @IsGranted({"ROLE_ADMIN", "ROLE_RESPONSIBLE"}) */ public function ...

您要求用户被授予两个角色。更改您的层次结构以使 ROLE_SUPER_ADMIN 继承 ROLE_RESPONSIBLE 以便您可以删除它或将注释更改为 :

/**
 * @Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_RESPONSIBLE')")
 */