/** * Create an object * * @param ContainerInterface $container * @param string $requestedName * @param null|array $options * @return object * @throws ServiceNotFoundException if unable to resolve the service. * @throws ServiceNotCreatedException if an exception is raised when * creating a service. * @throws ContainerException if any other error occurs */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var \ZfjRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfjRbac\\Options\\ModuleOptions'); $this->setCreationOptions($options); /* @var \ZfjRbac\Service\RoleService $roleService */ $roleService = $container->get('ZfjRbac\\Service\\RoleService'); $routeGuard = new RouteGuard($roleService, $this->options); $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $routeGuard; }
public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); $routeMatch = new RouteMatch([]); $application = $this->getMock('Zend\\Mvc\\Application', [], [], '', false); $eventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface'); $application->expects($this->once())->method('getEventManager')->will($this->returnValue($eventManager)); $eventManager->expects($this->once())->method('trigger')->with(MvcEvent::EVENT_DISPATCH_ERROR); $routeMatch->setMatchedRouteName('adminRoute'); $event->setRouteMatch($routeMatch); $event->setApplication($application); $identityProvider = $this->getMock('ZfjRbac\\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('ZfjRbac\\Exception\\UnauthorizedException', $event->getParam('exception')); }