/**
  * {@inheritDoc}
  * @return ControllerGuard
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $parentLocator = $serviceLocator->getServiceLocator();
     /* @var \ZfjRbac\Options\ModuleOptions $moduleOptions */
     $moduleOptions = $parentLocator->get('ZfjRbac\\Options\\ModuleOptions');
     /* @var \ZfjRbac\Service\RoleService $roleService */
     $roleService = $parentLocator->get('ZfjRbac\\Service\\RoleService');
     $controllerGuard = new ControllerGuard($roleService, $this->options);
     $controllerGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy());
     return $controllerGuard;
 }
Example #2
0
 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->setParam('controller', 'MyController');
     $routeMatch->setParam('action', 'delete');
     $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']);
     $roleService = new RoleService($identityProvider, $roleProvider, new RecursiveRoleIteratorStrategy());
     $routeGuard = new ControllerGuard($roleService, [['controller' => 'MyController', 'actions' => 'edit', 'roles' => 'member']]);
     $routeGuard->onResult($event);
     $this->assertTrue($event->propagationIsStopped());
     $this->assertEquals(ControllerGuard::GUARD_UNAUTHORIZED, $event->getError());
     $this->assertInstanceOf('ZfjRbac\\Exception\\UnauthorizedException', $event->getParam('exception'));
 }