2016-08-01 121 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表内(该权限将给予角色的上级以及自动分层的方式),并对所有人都能正常工作。