저자: Michaël Gallego (mic.gallego@gmail.com)
저자: JM Leroux (jmleroux.pro@gmail.com)
상속: extends AbstractGuard, use trait ProtectionPolicyTrait
 /**
  * @param \Zend\ServiceManager\AbstractPluginManager|ServiceLocatorInterface $serviceLocator
  * @return RouteGuard
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $parentLocator = $serviceLocator->getServiceLocator();
     /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */
     $moduleOptions = $parentLocator->get('ZfcRbac\\Options\\ModuleOptions');
     /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */
     $authorizationService = $parentLocator->get('ZfcRbac\\Service\\AuthorizationService');
     $routeGuard = new RoutePermissionsGuard($authorizationService, $this->options);
     $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy());
     return $routeGuard;
 }
 /**
  * @param ContainerInterface $container
  * @param string $requestedName
  * @param array|null $options
  * @return RoutePermissionsGuard
  */
 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\AuthorizationService $authorizationService */
     $authorizationService = $container->get('ZfcRbac\\Service\\AuthorizationService');
     $routeGuard = new RoutePermissionsGuard($authorizationService, $options);
     $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy());
     return $routeGuard;
 }
예제 #3
0
 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('ZfcRbac\\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('ZfcRbac\\Exception\\UnauthorizedException', $event->getParam('exception'));
 }