2013-06-11 34 views
2

在Sonata User Admin中,我想讓用戶只能列出/編輯屬於同一公司的用戶。SonataUser - 自定義選民不需要LIST

所以,我實現了自定義的選民來管理用戶管理特定的安全規則:現在

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

trutruc & MACHIN都來自同一家公司,所以我可以對其進行編輯。但是我想chouchouette & truc不會出現。除了投票人之外,我是否必須重寫管理員類別createQuery()方法?

最後問題是:如何使用ACL過濾sonata用戶?

回答

2

當我看到活躍的社區流動回答,讓我給我的:

不能

至少我沒有找到一種方法,使用ACL也不選民過濾名單。
我不得不重寫管理員的createQuery()

+0

除了你的問題上https://github.com/sonata-project/SonataAdminBundle/issues/1398這裏是一個如何在SecurityQuery()方法中注入SecurityContext的示例:http://stackoverflow.com/questions/12843677/sonata-admin-only-allow-show-what-logged-in-user-has創建 – webDEVILopers