/** * {@inheritDoc} * @return RouteGuard */ public function createService(ServiceLocatorInterface $serviceLocator) { $parentLocator = $serviceLocator->getServiceLocator(); /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $parentLocator->get('ZfcRbac\\Options\\ModuleOptions'); /* @var \ZfcRbac\Service\RoleService $roleService */ $roleService = $parentLocator->get('ZfcRbac\\Service\\RoleService'); $routeGuard = new RouteGuard($roleService, $this->options); $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $routeGuard; }
/** * @param ContainerInterface $container * @param string $requestedName * @param array|null $options * @return RouteGuard */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { if (null === $options) { $options = []; } /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\\Options\\ModuleOptions'); /* @var \ZfcRbac\Service\RoleService $roleService */ $roleService = $container->get('ZfcRbac\\Service\\RoleService'); $routeGuard = new RouteGuard($roleService, $options); $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $routeGuard; }
public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); $routeMatch = $this->createRouteMatch(); $application = $this->getMock('Zend\\Mvc\\Application', [], [], '', false); $eventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface'); $application->expects($this->once())->method('getEventManager')->will($this->returnValue($eventManager)); if (method_exists($eventManager, 'triggerEvent')) { $eventManager->expects($this->once())->method('triggerEvent')->with($event); } else { $eventManager->expects($this->once())->method('trigger')->with(MvcEvent::EVENT_DISPATCH_ERROR); } $routeMatch->setMatchedRouteName('adminRoute'); $event->setRouteMatch($routeMatch); $event->setApplication($application); $identityProvider = $this->getMock('ZfcRbac\\Identity\\IdentityProviderInterface'); $identityProvider->expects($this->any())->method('getIdentityRoles')->will($this->returnValue('member')); $roleProvider = new InMemoryRoleProvider(['member', 'guest']); $roleService = new RoleService($identityProvider, $roleProvider, new RecursiveRoleIteratorStrategy()); $routeGuard = new RouteGuard($roleService, ['adminRoute' => 'guest']); $routeGuard->onResult($event); $this->assertTrue($event->propagationIsStopped()); $this->assertEquals(RouteGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\\Exception\\UnauthorizedException', $event->getParam('exception')); }