Пример #1
0
 /**
  * Checks if a valid user is logged in
  *
  * @throws \Exception
  * @return AccessControllerInterface::ACCESS
  */
 protected function checkAuthentication()
 {
     try {
         $isAuthenticated = $this->objectManager->getAuthenticationProvider()->authenticate();
     } catch (\Exception $exception) {
         Dispatcher::getSharedDispatcher()->logException($exception);
         throw $exception;
     }
     if ($isAuthenticated === FALSE) {
         return self::ACCESS_UNAUTHORIZED;
     }
     return self::ACCESS_ALLOW;
 }
Пример #2
0
 /**
  * Returns a query for objects of this repository
  *
  * @return QueryInterface
  * @api
  */
 public function createQuery()
 {
     /** @var QueryInterface $query */
     $query = $this->objectManager->get('Cundd\\Rest\\VirtualObject\\Persistence\\QueryInterface');
     $query->setConfiguration($this->getConfiguration());
     return $query;
 }
Пример #3
0
 /**
  * Returns the life time defined in the expires header
  *
  * @return int
  */
 public function getExpiresHeaderLifeTime()
 {
     if ($this->expiresHeaderLifeTime === NULL) {
         $readCacheLifeTime = $this->objectManager->getConfigurationProvider()->getSetting('expiresHeaderLifeTime');
         $this->expiresHeaderLifeTime = $readCacheLifeTime !== NULL ? intval($readCacheLifeTime) : $this->getCacheLifeTime();
     }
     return $this->expiresHeaderLifeTime;
 }
Пример #4
0
 /**
  * Returns the request
  *
  * @return \Cundd\Rest\Request
  */
 public function getRequest()
 {
     if (!$this->request) {
         $format = '';
         $uri = $this->getUri($format);
         /*
          * Transform Document URLs
          * @Todo: Make this better
          */
         if (substr($uri, 0, 9) === 'Document/') {
             $uri = 'Document-' . substr($uri, 9);
         }
         $this->request = new Request(NULL, $uri);
         $this->request->injectConfigurationProvider($this->objectManager->getConfigurationProvider());
         if ($format) {
             $this->request->format($format);
         }
     }
     return $this->request;
 }
Пример #5
0
 /**
  * Persist all changes to the database
  */
 public function persistAllChanges()
 {
     /** @var PersistenceManagerInterface $persistenceManager */
     $persistenceManager = $this->objectManager->get(ObjectManager::getPersistenceManagerClassName());
     $persistenceManager->persistAll();
 }
Пример #6
0
 /**
  * @test
  */
 public function getDefaultDataProviderForPathWithFormatTest()
 {
     $_GET['u'] = 'Vendor-NotExistingExt-MyModel/1.json';
     $dataProvider = $this->fixture->getDataProvider();
     $this->assertInstanceOf('\\Cundd\\Rest\\DataProvider\\DataProvider', $dataProvider);
 }
Пример #7
0
 /**
  * @test
  */
 public function getDataProviderForVirtualObjectWithPathWithFormatTest()
 {
     $_GET['u'] = 'VirtualObject-Page/1.json';
     $dataProvider = $this->fixture->getDataProvider();
     $this->assertInstanceOf('Cundd\\Rest\\DataProvider\\VirtualObjectDataProvider', $dataProvider);
 }
Пример #8
0
 /**
  * Returns the Data Provider
  *
  * @return DataProviderInterface
  */
 protected function getDataProvider()
 {
     return $this->objectManager->getDataProvider();
 }
Пример #9
0
 /**
  * Dispatch the request
  *
  * @param \Cundd\Rest\Request $request Overwrite the request
  * @param Response $responsePointer Reference to be filled with the response
  * @return boolean Returns if the request has been successfully dispatched
  */
 public function dispatch(Request $request = NULL, Response &$responsePointer = NULL)
 {
     if ($request) {
         $this->requestFactory->registerCurrentRequest($request);
         $this->objectManager->reassignRequest();
     } else {
         $request = $this->requestFactory->getRequest();
     }
     $requestPath = $request->path();
     if (!$requestPath) {
         return $this->greet();
     }
     // Checks if the request needs authentication
     switch ($this->objectManager->getAccessController()->getAccess()) {
         case AccessControllerInterface::ACCESS_ALLOW:
             break;
         case AccessControllerInterface::ACCESS_UNAUTHORIZED:
             echo $this->responseFactory->createErrorResponse('Unauthorized', 401);
             return FALSE;
         case AccessControllerInterface::ACCESS_DENY:
         default:
             echo $this->responseFactory->createErrorResponse('Forbidden', 403);
             return FALSE;
     }
     /** @var Cache $cache */
     $cache = $this->objectManager->getCache();
     $response = $cache->getCachedValueForRequest($request);
     $success = TRUE;
     // If no cached response exists
     if (!$response) {
         // If a path is given let the handler build up the routes
         if ($requestPath) {
             $this->logRequest(sprintf('path: "%s" method: "%s"', $requestPath, $request->method()));
             $this->objectManager->getHandler()->configureApiPaths();
         }
         // Let Bullet PHP do the hard work
         $response = $this->app->run($request);
         // Handle exceptions
         if ($response->content() instanceof \Exception) {
             $success = FALSE;
             $exception = $response->content();
             $this->logException($exception);
             $response = $this->exceptionToResponse($exception);
         } else {
             // Cache the response
             $cache->setCachedValueForRequest($request, $response);
         }
     }
     // Additional custom headers
     $additionalResponseHeaders = $this->objectManager->getConfigurationProvider()->getSetting('responseHeaders', array());
     if (is_array($additionalResponseHeaders) && count($additionalResponseHeaders)) {
         foreach ($additionalResponseHeaders as $responseHeaderType => $value) {
             if (is_string($value)) {
                 $response->header($responseHeaderType, $value);
             } elseif (is_array($value) && array_key_exists('userFunc', $value)) {
                 $response->header(rtrim($responseHeaderType, '.'), GeneralUtility::callUserFunction($value['userFunc'], $value, $this));
             }
         }
     }
     $responsePointer = $response;
     $responseString = (string) $response;
     $this->logResponse('response: ' . $response->status(), array('response' => '' . $responseString));
     echo $responseString;
     return $success;
 }