【问题标题】:Symfony Voter supports method receiving ROLE and Request instead attribute and entitySymfony Voter 支持接收 ROLE 和 Request 的方法,而不是属性和实体
【发布时间】:2017-08-21 11:29:16
【问题描述】:

Voter 似乎可以在我的整个应用程序上运行...除了在这个控制器上:

 $entity = $em->getReference('AppBundle:Offer',$id);
 $this->denyAccessUnlessGranted('overview', $entity);

此 Voter 方法接收错误参数的地方 ....

支持($attribute, $subject)

dump($attribute)-> ROLE_USER // instead 'overview'
dump($subject)-> Request Object // instead $entity

选民配置是:

app_voter:
    class:      AppBundle\Security\Authorization\AppVoter
    public:     true
    strategy: affirmative
    arguments: ['@role_hierarchy', '@security.token_storage']
    tags:
        - { name: security.voter }

如果我在控制器代码上写“view”而不是“overview”,问题就会消失。

【问题讨论】:

  • supports 方法实际上可以在处理周期内多次调用,以确定最终调用哪个 voteOnAttribute。因此,如果 $subject 不是要约,则支持应该只返回 false。

标签: symfony symfony2-voter


【解决方案1】:

我忘记在“支持”方法中添加“概述”:

  protected function supports($attribute, $subject) {
        // if the attribute isn't one we support, return false
        if (!in_array($attribute, array(self::OVERVIEW, self::VIEW, self::EDIT))) {
            return false;
        }

        // bypass if the entity is not supported
        if (!$this->isSupportedClass($subject)) {
            return true;
        }
        return true;
    }

【讨论】:

  • 您好,同样的问题。 $subject 在 voteOnAttribute 上很好,但在支持上不好,Subject 是一个请求......当我调用 isGrented 时,我给出了一个由 Controller 直接接收的参数转换器对象......也许这是一个建议。
猜你喜欢
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
相关资源
最近更新 更多