API平台安全注解中使用常量

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

Symfony 的 API 平台允许您使用投票者来授予或拒绝对您的资源的访问,如其 docs 中所述。

但是所有示例都使用魔法值,我宁愿使用类常量。例如

/**
 * @ApiResource(
 *     itemOperations={
 *          "put"={"security"="is_granted(UserVoter::USER_EDIT, object)"},
 *     }
 * )
 */

我已经尝试使用

UserVoter
类的“use”语句,
App\Security\Voter\UserVoter::USER_EDIT
以及转义的
App\\Security\\Voter\\UserVoter::USER_EDIT
,但是 API 平台不接受任何内容

如何在 API Platform 中安全地使用类常量?

symfony api-platform.com symfony-voter
1个回答
4
投票

只是为了向任何想知道的人确认:

is_granted(constant('\\App\\Security\\Voter\\UserVoter::USER_EDIT'), object)
是一种享受

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