/** * {@inheritDoc} * @return ControllerGuard */ 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'); $controllerGuard = new ControllerGuard($roleService, $this->options); $controllerGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $controllerGuard; }
/** * @param ContainerInterface $container * @param string $requestedName * @param array|null $options * @return ControllerGuard */ 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'); $controllerGuard = new ControllerGuard($roleService, $options); $controllerGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $controllerGuard; }
public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); $routeMatch = $this->createRouteMatch(['controller' => 'MyController', 'action' => 'delete']); $application = $this->getMock('Zend\\Mvc\\Application', [], [], '', false); $eventManager = $this->getMock('Zend\\EventManager\\EventManager'); $application->expects($this->once())->method('getEventManager')->will($this->returnValue($eventManager)); $eventManager->expects($this->once())->method('triggerEvent')->with($event); $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']); $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('ZfcRbac\\Exception\\UnauthorizedException', $event->getParam('exception')); }