Symfony 5:方法注释上的@IsGranted在继承的角色上不起作用

问题描述 投票:0回答:1

我(显然)搜索了similar problems,但我都不是。

这是我的情况:

  • 我做了一个自定义角色:ROLE_SUPER_ADMIN
  • 我的实际用户是admin,他的唯一角色是ROLE_SUPER_ADMIN
  • 角色ROLE_SUPER_ADMIN继承了ROLE_ADMIN(请参见下文)
  • 我正在尝试访问/users/page/1
  • 我有Access Denied by controller annotation @IsGranted(["ROLE_ADMIN", "ROLE_RESPONSIBLE"])

这是我的控制人:

//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),但事实并非如此。

我仅尝试使用@IsGranted("ROLE_USER"),它可以工作,但是@IsGranted("ROLE_ADMIN")不能,这是继承的角色

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

谢谢,星期一是您知道的星期一...

php symfony security annotations roles
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.