2016-08-01 10 views
0

私はzfc_rbac.global.php内のコードの下に追加しました:2番目のZfcRbacアサーションが機能していません| ZF2

return [ 
'zfc_rbac' => [ 
    'assertion_map' => [ 
     'isAuthorizedToAddUser' => 'Application\Assertions\WhoCanAddUser', 
     'isBranchOrOrgIdPresentIfNotAdmin' => 'Application\Assertions\BranchOrOrgIdPresentIfNotAdmin' 
    ] 
]] 

、以下のようなコントローラの内部でそれを使用:

if (! $this->authorizationService->isGranted('isBranchOrOrgIdPresentIfNotAdmin')) { 
    throw new UnauthorizedException('You are not authorized to add this aaa!'); 
} 

が、そのは、Assertメソッドからも、私return true場合に例外をスロー。しかし、私がisBranchOrOrgIdPresentIfNotAdminisAuthorizedToAddUserに置き換えた場合は、正常に動作します。ここで何が間違っているかもしれない。 2番目のアサーションクラスBranchOrOrgIdPresentIfNotAdminは、WhoCanAddUserクラスのレプリカです。以下は私のWhoCanAddUserアサーションクラスです。

namespace Application\Assertions; 

use ZfcRbac\Assertion\AssertionInterface; 
use ZfcRbac\Service\AuthorizationService; 
use ZfcRbac\Exception\UnauthorizedException; 
use Zend\Session\Container; 

class WhoCanAddUser implements AssertionInterface 
{ 
    protected $notAuthorizedMessage = 'You are not authorized to add this user!'; 

    public function __construct() 
    { 
     $this->org_session = new Container('org'); 
    } 

    /** 
    * Check if this assertion is true 
    * 
    * @param AuthorizationService $authorization    
    * @param mixed $role    
    * 
    * @return bool 
    */ 
    public function assert(AuthorizationService $authorization, $role = null) 
    { 
     return true; //added this for testing if true is working and it worked, but second assertion is not working! 
     switch($authorization->getIdentity()->getRole()->getName()){ 
      case 'admin': 
       return true; 
      break; 
      case 'owner': 
       if($role != 'member'){ 
        throw new UnauthorizedException($this->notAuthorizedMessage); 
       } 
       return true; 
      break; 
      default: 
       throw new UnauthorizedException($this->notAuthorizedMessage); 
      break; 
     } 

     if($authorization->getIdentity()->getRole()->getName() != 'admin' && !$this->org_session->offsetExists('branchId')){ 
      throw new \Zend\Session\Exception\RuntimeException('You need to be connected to an Organisation's branch before you can add members. Contact your Organisation Owner.'); 
     } 
    } 
} 

2番目のアサーションがまったく機能しないことがありますか?

答えて

0

ちょうどisBranchOrOrgIdPresentIfNotAdminエントリが許可テーブルの内部であるとhierarchicalrole_permissionテーブル内部役割の低いレベルにその許可を割り当てる有していなければならない、ことを見出した(すなわち許可を自動的に階層的な方法でも役割の上位に説明する)とすべての人にとってうまくいくでしょう。

関連する問題