【问题标题】:Why to implement the supportsClass & supportsAttribute methods when implementing a voter?为什么在实现选民时要实现supportsClass和supportsAttribute方法?
【发布时间】:2012-04-29 18:38:19
【问题描述】:

正如您在以下链接中看到的,当我们调用 isGranted() 时,supportsClass 和 supportsAttribute 方法不会被调用:

这些方法是否在其他地方被调用?

为什么它们是界面的一部分?

【问题讨论】:

  • 我也注意到了。欢迎解释

标签: security symfony


【解决方案1】:

听起来这是正常行为,因为选民必须检查班级。

这是一个检查类的解决方案:

function vote(TokenInterface $token, $object, array $attributes)
{   
    $user = $token->getUser();

    //sometime the object is a class or a instance of Request
    if($object instanceOf \Symfony\Component\HttpFoundation\Request ){
        return VoterInterface::ACCESS_ABSTAIN;   
    }else{
        $class = $object->getType();
        if($class == 'Vendor\\MyBundle\\Entity\\MyEntity'){
           return VoterInterface::ACCESS_GRANTED; 
        }
    }

    return VoterInterface::ACCESS_DENIED;   
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 2013-10-14
    • 1970-01-01
    • 2023-03-11
    • 2015-11-22
    • 2021-06-09
    • 2010-09-15
    • 2013-08-20
    相关资源
    最近更新 更多