2016-08-01 74 views
0

我已經添加下面的代碼裏面zfc_rbac.global.php第二個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!'); 
} 

但其拋出異常,即使我return true從斷言方法。但如果我用isAuthorizedToAddUser替換isBranchOrOrgIdPresentIfNotAdmin,它的工作正常。這裏可能是錯的。第二個斷言類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.'); 
     } 
    } 
} 

我錯過了第二個斷言根本不工作的東西。

回答

0

就發現,isBranchOrOrgIdPresentIfNotAdmin條目是內部權限表,和那些與權限分配給角色的較低水平hierarchicalrole_permission表內(該權限將給予角色的上級以及自動分層的方式),並對所有人都能正常工作。