2013-09-05 76 views
0

我已經花了幾個小時試圖讓Symfony的ACL在我的項目中工作,但我根本無法找到解決方案。這是我的測試示例:Symfony2 ACL - isGranted總是返回false

$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) 

我可以看到數據顯示在表格中,但isGranted始終返回false。任何想法我做錯了什麼?

acl_classes

id class_type 
2 Me\MyBundle\Entity\Competition 

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

id parent_object_identity_id class_id object_identifier entries_inheriting 
3 NULL      2   158     1 

acl_object_identity_ancestors

object_identity_id ancestor_id 
3     3 

acl_security_identities

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

可以嗎嘗試通過''編輯'而不是'MaskBuilder :: MASK_EDIT'? – sergekv

+0

我的意思是'$ this-> get('security.context') - > isGranted('EDIT',$ competition);' – sergekv

回答

0

我在我的實體,它實現ObjectIdentityInterface同樣的問題...

我的getType的實施是:

public function getType() { 
     return __CLASS__; 
} 

這回它被宣告(靜態)類的名稱,並且不能在comparisson

,所以我改成了dinamically:

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

希望它有助於