可以为symfony中的不同实体创建多个自定义Voter类吗?

问题描述

我正在检查当前用户是否有权在PostController中删除Post和在ProductController中删除Product。我看到了https://symfony.com/doc/current/security/voters.htmlhttps://medium.com/@galopintitouan/using-symfony-security-voters-to-check-user-permissions-with-ease-9a48e2d45540。然后,我创建2个自定义投票者类扩展,Symfony\Component\Security\Core\Authorization\Voter\VoterPostVoterProductVoter。 两个投票者类在函数VoteOnAttribute($attribute,$subject,TokenInterface $token)中具有相同的主体。它们仅在功能supports($attribute,$subject)上有所不同。

在PostVoter中:

public function supports($attribute,$subject)
    {
        return $subject instanceof Post && in_array($attribute,['view','edit','delete']);
    }

在ProductVoter中:

public function supports($attribute,$subject)
    {
        return $subject instanceof Product && in_array($attribute,'delete']);
    }

如果在PostController中,则deleteAction,我这样写:

$post = new Post();
if (!$this->isGranted('delete',$post))
  {
     return false;
  }

它按预期运行良好。 但是如果我写:

$post = new Product();
if (!$this->isGranted('delete',$post))
  {
     return false;
  }

它没有通过PostVoter,但是它通过了ProductVoter。我该如何处理此问题?

解决方法

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

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

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