在 Sonata User Admin 中,我想让用户能够仅列出/编辑属于同一公司的用户。
所以我实现了一个自定义投票器来管理用户管理中的这个特定安全规则:
public function supportsAttribute($attribute)
{
return in_array($attribute, array(
'EDIT',
'DELETE',
'VIEW',
'LIST',
));
}
public function supportsClass($class)
{
return in_array("FOS\UserBundle\Model\UserInterface"
, class_implements($class));
}
public function vote(TokenInterface $token, $object, array $attributes)
{
if ( !($this->supportsClass(get_class($object))) ) {
return VoterInterface::ACCESS_ABSTAIN;
}
foreach ($attributes as $attribute) {
if ( !$this->supportsAttribute($attribute) ) {
return VoterInterface::ACCESS_ABSTAIN;
}
}
$user = $token->getUser();
if ( !($user instanceof UserInterface) ) {
return VoterInterface::ACCESS_DENIED;
}
// check if the user has the same company
if ($user->getCompany() == $object->getCompany()) {
return VoterInterface::ACCESS_GRANTED;
}
return VoterInterface::ACCESS_DENIED;
}
现在,作为用户,我只能修改附属于我公司的用户(如预期的那样)
但我仍然在列表中看到所有用户。
trutruc 和 machin 来自同一家公司,所以我可以编辑它们。但我希望 chouchouette & truc 不要出现。除了选民之外,我是否必须覆盖管理类的 createQuery()
方法?
最后的问题是:如何使用ACL过滤sonata用户?
最佳答案
你不能
至少我没有找到使用 ACL 或 Voters 来过滤列表的方法。
我不得不覆盖管理员的 createQuery()
关于security - SonataUser - 不为 LIST 调用自定义选民,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17049035/