【问题标题】:Use Constant in API Platform Security Annotation在 API 平台安全注解中使用常量
【发布时间】:2021-01-18 05:00:09
【问题描述】:

API Platform for Symfony 允许您使用 Voters 来授予或拒绝访问您的资源,如其docs 中所述。

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

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

我已经尝试使用 UserVoter 类的“使用”语句 App\Security\Voter\UserVoter::USER_EDIT 以及转义 App\\Security\\Voter\\UserVoter::USER_EDIT,但是 API 平台不接受任何内容

问题:如何在 API 平台中使用具有安全性的类常量?

【问题讨论】:

  • 我从未使用过ApiPlatform,但see this answer。你试过\\App\\Security\\Voter\\UserVoter::USER_EDITis_granted(constant('\\App\\Security\\Voter\\UserVoter::USER_EDIT'), object) 吗? (也许对于第二个例子,常量函数需要安装表达式语言组件,我不知道它是否包含在 ApiPlatform 包中
  • 嘿。你试过"security"="is_granted(permission(UserVoter::USER_EDIT), object)" 吗?另外,请使用主类注释及其常量显示此实体

标签: symfony api-platform.com symfony4-voter


【解决方案1】:

只是为了向任何想知道的人确认: is_granted(constant('\\App\\Security\\Voter\\UserVoter::USER_EDIT'), object) 是一种享受

【讨论】:

    猜你喜欢
    • 2022-06-24
    • 2021-02-19
    • 1970-01-01
    • 2020-03-10
    • 2021-04-15
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    相关资源
    最近更新 更多