/**
  * @dataProvider getDataForDefaultVarsCopy
  */
 public function testViewWithNoCopyDefaultVars($createAnnotation, $populateDefaultVars, $shouldCopy)
 {
     $request = new Request();
     $request->attributes->set('_template_default_vars', ['customer']);
     $request->attributes->set('customer', 'A person goes here');
     $view = View::create();
     if ($createAnnotation) {
         $viewAnnotation = new ViewAnnotation([]);
         $viewAnnotation->setPopulateDefaultVars($populateDefaultVars);
         $request->attributes->set('_view', $viewAnnotation);
     }
     $event = $this->getResponseEvent($request, $view);
     $this->viewHandler = new ViewHandler(['html' => true]);
     $this->viewHandler->setContainer($this->container);
     // This is why we avoid container dependencies!
     $that = $this;
     $this->container->expects($this->exactly(2))->method('get')->with($this->logicalOr('fos_rest.view_handler', 'fos_rest.templating'))->will($this->returnCallback(function ($service) use($that) {
         return $service === 'fos_rest.view_handler' ? $that->viewHandler : $that->templating;
     }));
     $this->listener->onKernelView($event);
     $data = $view->getData();
     if ($shouldCopy) {
         $this->assertArrayHasKey('customer', $data);
         $this->assertEquals('A person goes here', $data['customer']);
     } else {
         $this->assertNull($data);
     }
 }
 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event
  * @return array|mixed
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     if (!$view instanceof View) {
         if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
             return parent::onKernelView($event);
         }
         $view = new View($view);
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if (null === $view->getStatusCode() && $configuration->getStatusCode()) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         if ($configuration->getSerializerGroups()) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->setGroups($configuration->getSerializerGroups());
             $view->setSerializationContext($context);
         }
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     $viewHandler = $this->container->get('fos_rest.view_handler');
     if ($viewHandler->isFormatTemplating($view->getFormat())) {
         if (!empty($vars)) {
             $parameters = (array) $viewHandler->prepareTemplateParameters($view);
             foreach ($vars as $var) {
                 if (!array_key_exists($var, $parameters)) {
                     $parameters[$var] = $request->attributes->get($var);
                 }
             }
             $view->setData($parameters);
         }
         $template = $request->attributes->get('_template');
         if ($template) {
             if ($template instanceof TemplateReference) {
                 $template->set('format', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $viewHandler->handle($view, $request);
     $event->setResponse($response);
 }
Esempio n. 3
0
 /**
  * Handle authorization by HTTP Authorization header     *
  * <code>Authorization: BPI pk="public_key", token="token"</code>
  *
  * Or by query string
  * <code>http://example.com/page?_authorization[pk]=public_key&_authorization[token]=token</code>
  *
  * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
  * @throws AuthenticationException
  */
 public function handle(GetResponseEvent $event)
 {
     try {
         if ($this->container->get('kernel')->getEnvironment() == 'test_skip_auth') {
             return $this->skipAuthorization();
         }
         $request = $event->getRequest();
         $token = new PKUserToken();
         if ($request->isMethod('OPTIONS')) {
             return $this->skipAuthorization();
         }
         if ($request->headers->has('Auth')) {
             if (!preg_match('~BPI agency="(?<agency>[^"]+)", token="(?<token>[^"]+)"~i', $request->headers->get('Auth'), $matches)) {
                 throw new AuthenticationException('Authorization credintials required (HTTP Headers)');
             }
             $token->setUser($matches['agency']);
             $token->token = $matches['token'];
         } elseif ($request->query->has('_authorization')) {
             $auth = $request->query->get('_authorization');
             if (empty($auth['agency']) or empty($auth['token'])) {
                 throw new AuthenticationException('Authorization credintials required (GET)');
             }
             $token->setUser($auth['agency']);
             $token->token = $auth['token'];
         } else {
             throw new AuthenticationException('Authorization required (none)');
         }
         $authToken = $this->authenticationManager->authenticate($token);
         $this->securityContext->setToken($authToken);
     } catch (AuthenticationException $failed) {
         //simulate kernel.view event to be able REST response listener do their job
         $view = new \FOS\RestBundle\View\View($failed->getMessage(), 401);
         $controller_result_event = new GetResponseForControllerResultEvent($event->getKernel(), $request, $event->getRequestType(), $view);
         $view_listener = new ViewResponseListener($this->container);
         $view_listener->onKernelView($controller_result_event);
         $event->setResponse($controller_result_event->getResponse());
     }
 }
 /**
  * @dataProvider getDataForDefaultVarsCopy
  */
 public function testViewWithNoCopyDefaultVars($createAnnotation, $populateDefaultVars, $shouldCopy)
 {
     $this->createViewResponseListener(['html' => true]);
     $request = new Request();
     $request->attributes->set('_template_default_vars', ['customer']);
     $request->attributes->set('customer', 'A person goes here');
     $view = View::create();
     if ($createAnnotation) {
         $viewAnnotation = new ViewAnnotation([]);
         $viewAnnotation->setPopulateDefaultVars($populateDefaultVars);
         $request->attributes->set('_view', $viewAnnotation);
     }
     $event = $this->getResponseEvent($request, $view);
     $this->listener->onKernelView($event);
     $data = $view->getData();
     if ($shouldCopy) {
         $this->assertArrayHasKey('customer', $data);
         $this->assertEquals('A person goes here', $data['customer']);
     } else {
         $this->assertNull($data);
     }
 }
 /**
  * @expectedException \RuntimeException
  */
 public function testOnKernelViewWithNoViewNoDataWithForce()
 {
     $request = new Request();
     $request->attributes->set('_template_vars', true);
     $request->attributes->set('_template_default_vars', array('foo', 'halli'));
     $container = $this->getMockBuilder('\\Symfony\\Component\\DependencyInjection\\Container')->disableOriginalConstructor()->getMock();
     $container->expects($this->once())->method('getParameter')->with('fos_rest.view_response_listener.force_view')->will($this->returnValue(true));
     $listener = new ViewResponseListener($container);
     $event = $this->getMockBuilder('\\Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent')->disableOriginalConstructor()->getMock();
     $event->expects($this->once())->method('getRequest')->will($this->returnValue($request));
     $event->expects($this->once())->method('getControllerResult')->will($this->returnValue('foo'));
     $event->expects($this->never())->method('setResponse');
     $listener->onKernelView($event);
 }
 /**
  * onKernelView falls back to FrameworkExtraBundles' onKernelView
  * when fos_rest.view_response_listener.force_view is false
  */
 public function testOnKernelViewFallsBackToFrameworkExtraBundle()
 {
     $template = $this->getMockBuilder('\\Symfony\\Bundle\\FrameworkBundle\\Templating\\TemplateReference')->disableOriginalConstructor()->getMock();
     $request = new Request();
     $request->attributes->set('_template', $template);
     $templating = $this->getMock('\\Symfony\\Bundle\\FrameworkBundle\\Templating\\EngineInterface');
     $templating->expects($this->any())->method('renderResponse')->with($template, array())->will($this->returnValue(new Response('output')));
     $templating->expects($this->any())->method('render')->with($template, array())->will($this->returnValue('output'));
     $container = $this->getMockBuilder('\\Symfony\\Component\\DependencyInjection\\Container')->disableOriginalConstructor()->getMock();
     $container->expects($this->once())->method('get')->with('templating')->will($this->returnValue($templating));
     $listener = new ViewResponseListener($container);
     $event = $this->getMockBuilder('\\Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent')->disableOriginalConstructor()->getMock();
     $event->expects($this->any())->method('getRequest')->will($this->returnValue($request));
     $event->expects($this->any())->method('getControllerResult')->will($this->returnValue(array()));
     $response = null;
     $event->expects($this->once())->method('setResponse')->will($this->returnCallback(function ($r) use(&$response) {
         $response = $r;
     }));
     $listener->onKernelView($event);
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
     $this->assertSame('output', $response->getContent());
 }