2012-01-25 71 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