Author: Michaël Gallego (mic.gallego@gmail.com)
Inheritance: extends AbstractGuard, use trait ProtectionPolicyTrait
コード例 #1
0
 /**
  * {@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;
 }
コード例 #2
0
 /**
  * @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;
 }
コード例 #3
0
 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'));
 }