/** * 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; }
/** * 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); }); }); }
/** * Returns the current request path * * @return string */ protected function getPath() { return $this->getRequest() ? $this->getRequest()->path() : Dispatcher::getSharedDispatcher()->getPath(); }
/** * 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); }); }); }
/** * 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; }
/** * 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); }); }