Symfony2 ACL - isGranted始终返回false

时间:2022-10-22 17:03:43

I've spent a few hours already trying to make Symfony's ACL working in my project but I'm simply not able to find a solution. This is my testing example:

我已经花了几个小时试图让Symfony的ACL在我的项目中工作,但我根本找不到解决方案。这是我的测试示例:

$competition = $this->getDoctrine()->getManager()
    ->getRepository('MyBundle:Competition')->find(158);

$objectIdentity = ObjectIdentity::fromDomainObject($competition);
$aclProvider = $this->get('security.acl.provider');
try {
    $acl = $aclProvider->findAcl($objectIdentity);
} catch (\Symfony\Component\Security\Acl\Exception\AclNotFoundException $e) {
    $acl = $aclProvider->createAcl($objectIdentity);
}

// retrieving the security identity of the currently logged-in user
$securityIdentity = UserSecurityIdentity::fromAccount(
        $this->get('security.context')->getToken()->getUser());

// grant owner access
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_EDIT);
$aclProvider->updateAcl($acl);

.

$competition = $this->getDoctrine()->getManager()
    ->getRepository('MyBundle:Competition')->find(158);

$foo = $this->get('security.context')->isGranted(MaskBuilder::MASK_EDIT, $competition);

var_dump($foo); // always bool(false)

I can see data showing up in the tables but isGranted always returns false. Any idea what am I doing wrong?

我可以看到表中显示的数据但isGranted总是返回false。知道我做错了什么吗?

acl_classes:

acl_classes:

id  class_type
2   Me\MyBundle\Entity\Competition

acl_entries:

acl_entries:

id  class_id    object_identity_id  security_identity_id    field_name  ace_order   mask    granting    granting_strategy   audit_success   audit_failure
1   2           3                   2                       NULL        0           4       1           all                 0               0

acl_object_identities:

acl_object_identities:

id  parent_object_identity_id   class_id    object_identifier   entries_inheriting
3   NULL                        2           158                 1

acl_object_identity_ancestors:

acl_object_identity_ancestors:

object_identity_id  ancestor_id
3                   3

acl_security_identities:

acl_security_identities:

id  identifier                  username
2   Me\MyBundle\Entity\User-Me  1

1 个解决方案

#1


0  

I had the same problem in my entity which implements ObjectIdentityInterface...

我在我的实体中遇到了同样的问题,它实现了ObjectIdentityInterface ...

My implementation of getType was:

我的getType实现是:

public function getType() {
      return __CLASS__;
}

this was returning the name of the class where it was declared(static), and fails in the comparisson

这是返回声明它的类的名称(静态),并在comparisson中失败

so i changed it to dinamically:

所以我改为dinamically:

public function getType() {
      return get_class($this);
} 

hope it helps

希望能帮助到你

#1


0  

I had the same problem in my entity which implements ObjectIdentityInterface...

我在我的实体中遇到了同样的问题,它实现了ObjectIdentityInterface ...

My implementation of getType was:

我的getType实现是:

public function getType() {
      return __CLASS__;
}

this was returning the name of the class where it was declared(static), and fails in the comparisson

这是返回声明它的类的名称(静态),并在comparisson中失败

so i changed it to dinamically:

所以我改为dinamically:

public function getType() {
      return get_class($this);
} 

hope it helps

希望能帮助到你