【问题标题】:SYMFONY2 : ACL, Role and ClassScopeSYMFONY2:ACL、角色和类作用域
【发布时间】:2012-01-25 09:46:32
【问题描述】:

我的 ACL 有问题:

我使用类作用域来授予角色权限。

这是我声明 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。投票拒绝访问。”

我可以通过改变 equals 函数来解决这个问题 角色安全身份

原来的功能是

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();
}

有效...

我使用自己的角色类,会不会有问题?

感谢您的回答,

【问题讨论】:

    标签: symfony acl role


    【解决方案1】:

    我遇到了类似的问题。在我自己的 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
    }
    

    找出在 equal 函数中检查的值类型,它必须是字符串,而不是对象。在我的例子中,它是角色对象。

    【讨论】:

    • 嗨@anithaly 那个角色类是学说实体类吗?我的意思是您是否将角色保存在数据库中?我在这里也有同样的问题,好吧,无论如何我都会尝试将我的角色保存到数据库中,但如果你能在这里帮助我,我会非常感谢。
    • 我想我找到了我自己的问题的答案,symfony 文档解释了如何将角色保存到数据库中,还解释了你必须扩展 Symfony\Component\Security\Core\Role\Role,这是链接Managing Roles in the Database