security - SonataUser - 不为 LIST 调用自定义选民

标签 security symfony acl sonata-admin

在 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;
}

现在,作为用户,我只能修改附属于我公司的用户(如预期的那样)
但我仍然在列表中看到所有用户。

Sonata User Admin Grid

trutrucmachin 来自同一家公司,所以我可以编辑它们。但我希望 chouchouette & truc 不要出现。除了选民之外,我是否必须覆盖管理类的 createQuery() 方法?

最后的问题是:如何使用ACL过滤sonata用户?

最佳答案

你不能

至少我没有找到使用 ACL 或 Voters 来过滤列表的方法。
我不得不覆盖管理员的 createQuery()

关于security - SonataUser - 不为 LIST 调用自定义选民,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17049035/

相关文章:

laravel - Sentry 2、修改密码功能

security - 在 Active Directory 中存储私有(private) "octet string";默认情况下什么是安全的?

acl - Controller 级别权限

javascript - JS/XSS : When assigning user-provided strings to variables; is it enough to replace <, > 和字符串分隔符?

objective-c - 如何在 Objective C 中混淆静态密码?

php - 使用 Symfony Mailer 将 Webpack Assets 嵌入 Twig 的模板化电子邮件中

javascript - 如何在 Symfony 上显示日期选择器?

Spring 安全性与 Apache Shiro

javascript - 如何使用javascript将数据写入文件?I

json - 如何在 Easy Admin Bundle 中编辑字段中的 json 数据?