2012-01-25 72 views
3

我對ACL有問題:SYMFONY2:ACL,角色和ClassScope

我使用類作用域來授予對角色的權限。

這是我的代碼申報ClassAce:

$objectIdentity = new \Symfony\Component\Security\Acl\Domain\ObjectIdentity('class', 'Complete\\Class\\Name'); 
try 
{ 
    $acl = $aclProvider->findAcl($objectIdentity); 
} 
catch (\Symfony\Component\Security\Acl\Exception\Exception $e) 
{ 
    $acl = $aclProvider->createAcl($objectIdentity); 
} 
// retrieving the security identity of the currently role 
$securityIdentity = new \Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity($role); 
// grant owner access 
$acl->insertClassAce($securityIdentity, \Symfony\Component\Security\Acl\Permission\MaskBuilder::MASK_OWNER); 
$aclProvider->updateAcl($acl); 

這是我的代碼檢查訪問:

$securityContext = $this->get('security.context'); 
$oid = new \Symfony\Component\Security\Acl\Domain\ObjectIdentity('class', 'Complete\\Class\\Name'); 
if (false === $securityContext->isGranted('EDIT', $oid)) 
{ 
    throw new \Symfony\Component\Security\Core\Exception\AccessDeniedException(); 
} 

我收到AccessDeniedExeption,在日誌中的消息:「沒有 爲對象標識找到ACL。投票拒絕訪問。「

我可以通過改變 RoleSecurityIdentity

原有功能的平等功能解決這個

public function equals(SecurityIdentityInterface $sid) 
{ 
    if (!$sid instanceof RoleSecurityIdentity) { 
     return false; 
    } 

    return $this->role === $sid->getRole(); 
} 

但是,如果我通過

public function equals(SecurityIdentityInterface $sid) 
{ 
    if (!$sid instanceof RoleSecurityIdentity) { 
     return false; 
    } 

    return $this->role == $sid->getRole(); 
} 

改變它的工作原理...

我使用自己的角色類,它可能是一個問題米?

感謝您的回答,

回答

6

我有類似的問題。在我自己的Role類中擴展Symfony \ Component \ Security \ Core \ Role \ Role解決了這個問題。

use Symfony\Component\Security\Core\Role\Role as CoreRole; 

class Role extends CoreRole{ // or extends CoreRole implements RoleInterface 
// my custom Role class 
} 

找出什麼類型的值在同等函數中檢查,它必須是字符串,而不是對象。在我的情況下,它是角色對象。

+0

嗨@anithaly是那個角色類的學說實體類?我的意思是你在你的數據庫中保存你的角色?我在這裏也遇到了同樣的問題,好吧,我會嘗試將我的角色保存到數據庫中,但是如果您可以幫助我,請在這裏謝謝。 – metalvarez

+0

我想我找到了自己問題的答案,symfony文檔解釋瞭如何將角色保存到數據庫中,並解釋說您必須擴展Symfony \ Component \ Security \ Core \ Role \ Role,這裏是鏈接[Managing Roles在數據庫中](http://symfony.com/doc/current/cookbook/security/entity_provider.html#managing-roles-in-the-database) – metalvarez