2016-08-01 8 views
0
ZF2

Ich habe unten Code in zfc_rbac.global.php hinzugefügt:Zweite ZfcRbac Assertion funktioniert nicht |

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

und benutzen es im Inneren Controller wie unten:

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

aber sein wirft die Ausnahme, auch wenn ich return true von Assertion-Methode. Aber wenn ich isBranchOrOrgIdPresentIfNotAdmin durch isAuthorizedToAddUser ersetze, funktioniert es gut. Was könnte hier falsch sein? Die zweite Assertion-Klasse BranchOrOrgIdPresentIfNotAdmin ist nur die Replik der Klasse WhoCanAddUser. Unten ist meine Assertion-Klasse 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.'); 
     } 
    } 
} 

Ich vermisse etwas, dass zweite Behauptung überhaupt nicht funktioniert.

Antwort

0

gefunden Nur, dass, isBranchOrOrgIdPresentIfNotAdmin Eintrag innerhalb Permission-Tabelle sein und muß, dass die Erlaubnis zur unteren Ebene der Rolle zuweisen innerhalb hierarchicalrole_permission Tabelle (die Erlaubnis wird auch hierarchisch in automatisch zum nächsthöheren Niveau der Rolle gegeben werden) und wird für alle von ihnen gut funktionieren.

Verwandte Themen