有没有办法在自定义安全选民中获得用@ApiProperty(security="is_granted('ATTRIBUTE')") 注释的属性名称?

问题描述

刚刚在 API 平台 2.6.4 中发现了新的现有功能,该功能有助于检查对资源(实体)的任何属性的访问。

class Book
{
    //...

    /**
     * @var string Property viewable and writable only by users with ROLE_ADMIN
     *
     * @ApiProperty(security="is_granted('ROLE_ADMIN')")
     */
    private $adminOnlyProperty;
}

所以我尝试制作一个自定义的安全投票器来检查该属性是否在允许的字段中。

class PropertyVoter extends Voter
{
    protected function supports(string $attribute,$subject): bool
    {
        return in_array($attribute,['ATTRIBUTE']);
    }

    protected function VoteOnAttribute(string $attribute,$subject,TokenInterface $token): bool
    {
        $user = $token->getUser();
        if (!$user instanceof UserInterface) {
            return false;
        }

        // Subject should be a name of the property
        if (!in_array($subject,$user->getAllowedProperties())) {
            return false;
        }

        return true;
    }
}

比上面的注释改成

class Book
{
    //...

    /**
     * @var string Property viewable and writable only by users with ROLE_ADMIN
     *
     * @ApiProperty(security="is_granted('ATTRIBUTE')")
     */
    private $adminOnlyProperty;
}

但遇到问题,我无法获得字段的名称,该名称是用@ApiProperty 注释进行注释的。

所以问题。有没有办法在自定义投票者中获取属性名称

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)