ZfcRbac\Guard\RouteGuard::isGranted PHP Method

isGranted() public method

{@inheritDoc}
public isGranted ( MvcEvent $event )
$event Zend\Mvc\MvcEvent
    public function isGranted(MvcEvent $event)
    {
        $matchedRouteName = $event->getRouteMatch()->getMatchedRouteName();
        $allowedRoles = null;
        foreach (array_keys($this->rules) as $routeRule) {
            if (fnmatch($routeRule, $matchedRouteName, FNM_CASEFOLD)) {
                $allowedRoles = $this->rules[$routeRule];
                break;
            }
        }
        // If no rules apply, it is considered as granted or not based on the protection policy
        if (null === $allowedRoles) {
            return $this->protectionPolicy === self::POLICY_ALLOW;
        }
        if (in_array('*', $allowedRoles)) {
            return true;
        }
        return $this->roleService->matchIdentityRoles($allowedRoles);
    }

Usage Example

示例#1
0
 /**
  * @dataProvider routeDataProvider
  */
 public function testRouteGranted(array $rules, $matchedRouteName, array $rolesConfig, $identityRole, $isGranted, $protectionPolicy)
 {
     $event = new MvcEvent();
     $routeMatch = $this->createRouteMatch();
     $routeMatch->setMatchedRouteName($matchedRouteName);
     $event->setRouteMatch($routeMatch);
     $identity = $this->getMock('ZfcRbac\\Identity\\IdentityInterface');
     $identity->expects($this->any())->method('getRoles')->will($this->returnValue($identityRole));
     $identityProvider = $this->getMock('ZfcRbac\\Identity\\IdentityProviderInterface');
     $identityProvider->expects($this->any())->method('getIdentity')->will($this->returnValue($identity));
     $roleProvider = new InMemoryRoleProvider($rolesConfig);
     $roleService = new RoleService($identityProvider, $roleProvider, new RecursiveRoleIteratorStrategy());
     $routeGuard = new RouteGuard($roleService, $rules);
     $routeGuard->setProtectionPolicy($protectionPolicy);
     $this->assertEquals($isGranted, $routeGuard->isGranted($event));
 }