public function onKernelRequest(GetResponseEvent $event) { $this->logger->debug('Entity builder listener: catch kernel.request event'); // If this is not a master request, skip handling if (!$event->isMasterRequest()) { $this->logger->debug('Entity builder listener: this is not master request, skip'); return; } // If content already prepared if ($event->hasResponse()) { $this->logger->debug('Entity builder listener: event already has response, skip'); return; } // Getting request $request = $event->getRequest(); // Getting action $apiServerAction = $event->getRequest()->attributes->get('apiAction'); /* @var $apiServerAction ApiServerAction */ // Something wrong if (!$apiServerAction) { $this->logger->error('Request parser listener: request has no apiAction attribute, throwing access denied exception'); throw new AccessDeniedHttpException(); } // Creating request data entity try { $apiEntity = $apiServerAction->getRequestedEntity($request->attributes->get('apiData')); } catch (\Exception $e) { $this->logger->notice(sprintf('Request parser listener: unable to convert apiData to entity ("%s"), apiEntity set tu null', $e->getMessage())); $apiEntity = null; } // Setting request attributes $request->attributes->set('requestData', $apiEntity); // Cleaning request attributes $request->attributes->remove('apiData'); }
public function onKernelRequest(GetResponseEvent $event) { $this->logger->debug('Request parser listener: catch kernel.request event'); // If this is not a master request, skip handling if (!$event->isMasterRequest()) { $this->logger->debug('Request parser listener: this is not master request, skip'); return; } // If content already prepared if ($event->hasResponse()) { $this->logger->debug('Request parser listener: event already has response, skip'); return; } // getting request $request = $event->getRequest(); // getting content $content = $request->getContent(); $this->logger->debug(sprintf('Request parser listener: request content >>> %s', $content)); $content = @json_decode($content, true); if (!is_array($content)) { $this->logger->notice('Request parser listener: request content is not in the JSON format, throwing access denied exception'); throw new AccessDeniedHttpException(); } // creating ApiRequest $apiRequest = new ApiRequest($content); $this->logger->debug(sprintf('Request parser listener: request params. Token: "%s", user token: "%s", action: "%s"', $apiRequest->getToken(), $apiRequest->getUserToken(), $apiRequest->getAction())); if (!$apiRequest->getToken()) { $this->logger->notice('Request parser listener: request has no token, throwing access denied exception'); throw new AccessDeniedHttpException(); } // Calculating interface name $host = strtolower($request->server->get('SERVER_NAME')); $interfaceName = array_key_exists($host, $this->interfacesHosts) ? $this->interfacesHosts[$host] : $this->defaultInterface; if (!$interfaceName) { $this->logger->notice(sprintf('Request parser listener: interface for hostname "%s" not detected, throwing access denied exception', $host)); throw new AccessDeniedHttpException(); } $this->logger->debug(sprintf('Request parser listener: "%s" interface selected for handling connection from "%s"', $interfaceName, $host)); // Building interface $apiServerInterface = $this->apiServerInterfaceFactory->buildInterface($interfaceName); // Building action $apiServerAction = $apiServerInterface->buildAction($apiRequest->getAction()); $this->logger->debug(sprintf('Request parser listener: action route selected >>> %s', $apiServerAction->getActionRoute())); // Setting request attributes $request->attributes->set('apiClientToken', $apiRequest->getToken()); $request->attributes->set('apiUserToken', $apiRequest->getUserToken()); $request->attributes->set('apiAction', $apiServerAction); $request->attributes->set('apiData', $apiRequest->getData()); // Setting route $request->attributes->set('_controller', $apiServerAction->getActionRoute()); }
public function testOnKernelRequestUserHash() { $hash = '123abc'; $this->hashGenerator->expects($this->once())->method('generate')->will($this->returnValue($hash)); $this->request->headers->add(array('X-HTTP-Override' => 'AUTHENTICATE', 'Accept' => Kernel::USER_HASH_ACCEPT_HEADER)); $this->request->expects($this->once())->method('hasSession')->will($this->returnValue(true)); $this->assertNull($this->requestEventListener->onKernelRequestUserHash($this->event)); $this->assertTrue($this->event->isPropagationStopped()); $this->assertTrue($this->event->hasResponse()); $response = $this->event->getResponse(); $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response); $this->assertTrue($response->headers->has('X-User-Hash')); $this->assertSame($hash, $response->headers->get('X-User-Hash')); }