/** * @param \Zend\ServiceManager\AbstractPluginManager|ServiceLocatorInterface $serviceLocator * @return RouteGuard */ public function createService(ServiceLocatorInterface $serviceLocator) { $parentLocator = $serviceLocator->getServiceLocator(); /* @var \ZfjRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $parentLocator->get('ZfjRbac\\Options\\ModuleOptions'); /* @var \ZfjRbac\Service\AuthorizationService $authorizationService */ $authorizationService = $parentLocator->get('ZfjRbac\\Service\\AuthorizationService'); $routeGuard = new RoutePermissionsGuard($authorizationService, $this->options); $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $routeGuard; }
public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $eventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface'); $eventManager->expects($this->once())->method('trigger')->with(MvcEvent::EVENT_DISPATCH_ERROR); $application = $this->getMock('Zend\\Mvc\\Application', [], [], '', false); $application->expects($this->once())->method('getEventManager')->will($this->returnValue($eventManager)); $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName('adminRoute'); $event = new MvcEvent(); $event->setRouteMatch($routeMatch); $event->setApplication($application); $authorizationService = $this->getMock('ZfjRbac\\Service\\AuthorizationServiceInterface', [], [], '', false); $authorizationService->expects($this->once())->method('isGranted')->with('post.edit')->will($this->returnValue(false)); $routeGuard = new RoutePermissionsGuard($authorizationService, ['adminRoute' => 'post.edit']); $routeGuard->onResult($event); $this->assertTrue($event->propagationIsStopped()); $this->assertEquals(RouteGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfjRbac\\Exception\\UnauthorizedException', $event->getParam('exception')); }