2012-01-25 10 views
3

ACLに問題があります。SYMFONY2:ACL、ロール、ClassScope

私は、クラスのスコープを使用してロールの権限を与えます。

これはClassAceを宣言するために私のコードです:私は、ログ内のメッセージで、AccessDeniedExeptionを受け取る

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

:「いいえ

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

そして、これは、アクセスをチェックするために私のコードです オブジェクトIDの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(); 
} 

ことによってそれを変更する場合、それは動作します...

自分の役割クラスを使用していますm?

回答ありがとうございます。

答えて

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を拡張する必要があることも説明しています。データベース](http://symfony.com/doc/current/cookbook/security/entity_provider.html#managing-roles-in-the-database) – metalvarez

関連する問題