Пример #1
0
 /**
  * Handles the requests
  *
  * @param \React\Http\Request $request Request to handle
  * @param \React\Http\Response $response Prebuilt response object
  */
 public function serverCallback($request, $response)
 {
     // Currently the PHP server is readonly
     if (!in_array(strtoupper($request->getMethod()), array('GET', 'HEAD'))) {
         $response->writeHead(405, array('Content-type' => 'text/plain'));
         $response->end('Writing is currently not supported');
         return;
     }
     $requestPath = $this->sanitizePath($request->getPath());
     $requestPath = substr($requestPath, 5);
     /** @var Request $restRequest */
     $restRequest = new Request($request->getMethod(), $requestPath, $request->getQuery(), $request->getHeaders());
     $path = '' . strtok($requestPath, '/');
     $restRequest->initWithPathAndOriginalPath($path, $path);
     $this->setServerGlobals($request);
     /** @var \Bullet\Response $restResponse */
     $restResponse = NULL;
     ob_start();
     $this->dispatcher->dispatch($restRequest, $restResponse);
     $responseString = ob_get_clean();
     if (!$restResponse) {
         $response->writeHead(200);
         $response->end($responseString);
     } else {
         $response->writeHead($restResponse->status(), $this->getHeadersFromResponse($restResponse));
         $response->end($restResponse->content());
     }
     unset($restRequest);
     unset($restResponse);
 }
Пример #2
0
 /**
  * Resets the managed objects
  */
 public function reassignRequest()
 {
     $request = $this->dispatcher->getRequest();
     if ($this->authenticationProvider) {
         $this->authenticationProvider->setRequest($request);
     }
     if ($this->accessController) {
         $this->accessController->setRequest($request);
     }
 }
Пример #3
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;
 }
Пример #4
0
 /**
  * Handles the requests
  *
  * @param \React\Http\Request $request   Request to handle
  * @param \React\Http\Response $response Prebuilt response object
  */
 public function serverCallback($request, $response)
 {
     // Currently the PHP server is readonly
     if (!in_array(strtoupper($request->getMethod()), array('GET', 'HEAD'))) {
         $response->writeHead(405, array('Content-type' => 'text/plain'));
         $response->end('Writing is currently not supported');
         return;
     }
     /** @var \Cundd\Rest\Request $restRequest */
     $restRequest = new \Cundd\Rest\Request($request->getMethod(), $this->sanitizePath($request->getPath()));
     $this->setServerGlobals($request);
     /** @var \Bullet\Response $restResponse */
     $restResponse = NULL;
     ob_start();
     $this->app->dispatch($restRequest, $restResponse);
     ob_end_clean();
     $response->writeHead($restResponse->status(), $this->getHeadersFromResponse($restResponse));
     $response->end($restResponse->content());
     unset($restRequest);
     unset($restResponse);
 }
Пример #5
0
 /**
  * @test
  */
 public function createSuccessResponseTest()
 {
     $_GET['u'] = 'MyExt-MyModel/1.json';
     $response = $this->fixture->createSuccessResponse('Everything ok', 200);
     $this->assertEquals(200, $response->status());
     $this->assertEquals('{"message":"Everything ok"}', $response->content());
     $this->fixture->getRequest()->format('html');
     $response = $this->fixture->createSuccessResponse('HTML format is currently not supported', 200);
     $this->assertEquals(200, $response->status());
     $this->assertEquals('Unsupported format: html', $response->content());
     $this->fixture->getRequest()->format('blur');
     $response = $this->fixture->createSuccessResponse('This will default to JSON', 200);
     $this->assertEquals(200, $response->status());
     $this->assertEquals('{"message":"This will default to JSON"}', $response->content());
     $response = $this->fixture->createSuccessResponse(NULL, 200);
     $this->assertEquals(200, $response->status());
     $this->assertEquals('{"message":"OK"}', $response->content());
     // This will be an error
     $response = $this->fixture->createSuccessResponse(NULL, 404);
     $this->assertEquals(404, $response->status());
     $this->assertEquals('{"error":"Not Found"}', $response->content());
 }
Пример #6
0
 /**
  * Configure the API paths
  */
 public function configureApiPaths()
 {
     $dispatcher = Dispatcher::getSharedDispatcher();
     /** @var AuthHandler */
     $handler = $this;
     $dispatcher->registerPath($this->getRequest()->path(), function ($request) use($handler, $dispatcher) {
         $handler->setRequest($request);
         $dispatcher->registerPath('login', function ($request) use($handler, $dispatcher) {
             $getCallback = function ($request) use($handler) {
                 return $handler->getStatus();
             };
             $dispatcher->registerGetMethod($getCallback);
             /**
              * @param Request $request
              * @return array
              */
             $loginCallback = function ($request) use($handler) {
                 return $handler->checkLogin($request->getSentData());
             };
             $dispatcher->registerPostMethod($loginCallback);
         });
         $dispatcher->registerPath('logout', function ($request) use($handler, $dispatcher) {
             $postCallback = function ($request) use($handler) {
                 return $handler->logout();
             };
             $dispatcher->registerGetMethod($postCallback);
             $dispatcher->registerPostMethod($postCallback);
         });
     });
 }
Пример #7
0
 /**
  * Returns the current request path
  *
  * @return string
  */
 protected function getPath()
 {
     return $this->getRequest() ? $this->getRequest()->path() : Dispatcher::getSharedDispatcher()->getPath();
 }
Пример #8
0
 /**
  * Configure the API paths
  */
 public function configureApiPaths()
 {
     $dispatcher = Dispatcher::getSharedDispatcher();
     /** @var App $app */
     $app = $dispatcher->getApp();
     /** @var AuthHandler */
     $handler = $this;
     $app->path($dispatcher->getPath(), function ($request) use($handler, $app) {
         $handler->setRequest($request);
         $app->path('login', function ($request) use($handler, $app) {
             $getCallback = function ($request) use($handler) {
                 return $handler->getStatus();
             };
             $app->get($getCallback);
             $loginCallback = function ($request) use($handler) {
                 $dispatcher = Dispatcher::getSharedDispatcher();
                 return $handler->checkLogin($dispatcher->getSentData());
             };
             $app->post($loginCallback);
         });
         $app->path('logout', function ($request) use($handler, $app) {
             $getCallback = function ($request) use($handler) {
                 return $handler->logout();
             };
             $app->get($getCallback);
         });
     });
 }
Пример #9
0
 /**
  * Returns the Authentication Provider
  * @return \Cundd\Rest\Authentication\AuthenticationProviderInterface
  */
 public function getAuthenticationProvider()
 {
     if (!$this->authenticationProvider) {
         /** @var Dispatcher $dispatcher */
         $dispatcher = $this->dispatcher ? $this->dispatcher : Dispatcher::getSharedDispatcher();
         list($vendor, $extension, ) = Utility::getClassNamePartsForPath($dispatcher->getPath());
         // Check if an extension provides a Authentication Provider
         $authenticationProviderClass = 'Tx_' . $extension . '_Rest_AuthenticationProvider';
         if (!class_exists($authenticationProviderClass)) {
             $authenticationProviderClass = ($vendor ? $vendor . '\\' : '') . $extension . '\\Rest\\AuthenticationProvider';
         }
         // Use the found Authentication Provider
         if (class_exists($authenticationProviderClass)) {
             $this->authenticationProvider = $this->get($authenticationProviderClass);
         } else {
             // Use the default Authentication Provider
             #$authenticationProviderClass = 'Cundd\\Rest\\Authentication\\BasicAuthenticationProvider';
             $this->authenticationProvider = $this->get('Cundd\\Rest\\Authentication\\AuthenticationProviderCollection', array($this->get('Cundd\\Rest\\Authentication\\BasicAuthenticationProvider'), $this->get('Cundd\\Rest\\Authentication\\CredentialsAuthenticationProvider')));
         }
         $this->authenticationProvider->setRequest($dispatcher->getRequest());
     }
     return $this->authenticationProvider;
 }
Пример #10
0
 /**
  * Configure the API paths
  */
 public function configureApiPaths()
 {
     $dispatcher = Dispatcher::getSharedDispatcher();
     /** @var HandlerInterface */
     $handler = $this;
     $dispatcher->registerPath($this->getPath(), function ($request) use($handler, $dispatcher) {
         $handler->setRequest($request);
         /*
          * Handle a specific Model
          */
         $dispatcher->registerParameter('slug', function ($request, $identifier) use($handler, $dispatcher) {
             $handler->setIdentifier($identifier);
             /*
              * Get single property
              */
             $getPropertyCallback = function ($request, $propertyKey) use($handler) {
                 return $handler->getProperty($propertyKey);
             };
             $dispatcher->registerParameter('slug', $getPropertyCallback);
             /*
              * Show a single Model
              */
             $getCallback = function ($request) use($handler) {
                 return $handler->show();
             };
             $dispatcher->registerGetMethod($getCallback);
             /*
              * Replace a Model
              */
             $replaceCallback = function ($request) use($handler) {
                 return $handler->replace();
             };
             $dispatcher->registerPutMethod($replaceCallback);
             $dispatcher->registerPostMethod($replaceCallback);
             /*
              * Update a Model
              */
             $updateCallback = function ($request) use($handler) {
                 return $handler->update();
             };
             $dispatcher->registerPatchMethod($updateCallback);
             /*
              * Delete a Model
              */
             $deleteCallback = function ($request) use($handler) {
                 return $handler->delete();
             };
             $dispatcher->registerDeleteMethod($deleteCallback);
         });
         /*
          * Create a Model
          */
         $createCallback = function ($request) use($handler) {
             return $handler->create();
         };
         $dispatcher->registerPostMethod($createCallback);
         /*
          * List all Models
          */
         $listCallback = function ($request) use($handler) {
             return $handler->listAll();
         };
         $dispatcher->registerGetMethod($listCallback);
     });
 }