Prooph\ServiceBus\Plugin\Guard\FinalizeGuard::onFinalize PHP Method

onFinalize() public method

public onFinalize ( Prooph\Common\Event\ActionEvent $actionEvent )
$actionEvent Prooph\Common\Event\ActionEvent
    public function onFinalize(ActionEvent $actionEvent)
    {
        $promise = $actionEvent->getParam(QueryBus::EVENT_PARAM_PROMISE);
        $messageName = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME);
        if ($promise instanceof Promise) {
            $newPromise = $promise->then(function ($result) use($actionEvent, $messageName) {
                if (!$this->authorizationService->isGranted($messageName, $result)) {
                    $actionEvent->stopPropagation(true);
                    if (!$this->exposeEventMessageName) {
                        $messageName = '';
                    }
                    throw new UnauthorizedException($messageName);
                }
            });
            $actionEvent->setParam(QueryBus::EVENT_PARAM_PROMISE, $newPromise);
        } elseif (!$this->authorizationService->isGranted($messageName)) {
            $actionEvent->stopPropagation(true);
            if (!$this->exposeEventMessageName) {
                $messageName = '';
            }
            throw new UnauthorizedException($messageName);
        }
    }

Usage Example

Example #1
0
 /**
  * @test
  * @expectedException \Prooph\ServiceBus\Plugin\Guard\UnauthorizedException
  * @expectedExceptionMessage You are not authorized to access the resource "test_event"
  */
 public function it_stops_propagation_and_throws_unauthorizedexception_when_authorization_service_denies_access_with_deferred_and_exposes_message_name()
 {
     $authorizationService = $this->prophesize(AuthorizationService::class);
     $authorizationService->isGranted('test_event', 'result')->willReturn(false);
     $deferred = new Deferred();
     $deferred->resolve('result');
     $actionEvent = new DefaultActionEvent(QueryBus::EVENT_FINALIZE);
     $actionEvent->setParam(QueryBus::EVENT_PARAM_PROMISE, $deferred->promise());
     $actionEvent->setParam(QueryBus::EVENT_PARAM_MESSAGE_NAME, 'test_event');
     $routeGuard = new FinalizeGuard($authorizationService->reveal(), true);
     $routeGuard->onFinalize($actionEvent);
     $this->assertTrue($actionEvent->propagationIsStopped());
     $actionEvent->getParam(QueryBus::EVENT_PARAM_PROMISE)->done();
 }
All Usage Examples Of Prooph\ServiceBus\Plugin\Guard\FinalizeGuard::onFinalize