injectableFactory = $injectableFactory; $this->entryPointManager = $entryPointManager; $this->entityManager = $entityManager; $this->clientManager = $clientManager; $this->applicationUser = $applicationUser; } public function run(?StdClass $params = null) { $params = $params ?? (object) []; $entryPoint = $params->entryPoint ?? $_GET['entryPoint']; $final = $params->final ?? false; $data = $params->data ?? null; if (!$entryPoint) { throw new Error(); } $authRequired = $this->entryPointManager->checkAuthRequired($entryPoint); $authNotStrict = $this->entryPointManager->checkNotStrictAuth($entryPoint); if ($authRequired && !$authNotStrict && !$final) { $portalId = $this->detectPortalId(); if ($portalId) { $this->runThroughPortal($portalId, $entryPoint, $data); return; } } $slim = SlimAppFactory::create(); $slim->setBasePath(Route::detectBasePath()); $slim->add( function (Psr7Request $request, Psr7RequestHandler $handler) use ( $entryPoint, $data, $authRequired, $authNotStrict, $slim ) : Psr7Response { $requestWrapped = new RequestWrapper($request, $slim->getBasePath()); $responseWrapped = new ResponseWrapper($handler->handle($request)); $this->processRequest($entryPoint, $requestWrapped, $responseWrapped, $data, $authRequired, $authNotStrict); return $responseWrapped->getResponse(); } ); $slim->get('/', function (Psr7Request $request, Psr7Response $response) : Psr7Response { return $response; }); $slim->run(); } protected function processRequest( string $entryPoint, RequestWrapper $requestWrapped, ResponseWrapper $responseWrapped, ?StdClass $data, bool $authRequired, bool $authNotStrict ) { try { $authentication = $this->injectableFactory->createWith(Authentication::class, [ 'allowAnyAccess' => $authNotStrict, ]); $apiAuth = ApiAuth::getBuilder() ->setAuthentication($authentication) ->setAuthRequired($authRequired) ->forEntryPoint() ->build(); $apiAuth->process($requestWrapped, $responseWrapped); if (!$apiAuth->isResolved()) { return; } if ($apiAuth->isResolvedUseNoAuth()) { $this->applicationUser->setupSystemUser(); } ob_start(); $this->entryPointManager->run($entryPoint, $requestWrapped, $responseWrapped, $data); $contents = ob_get_clean(); if ($contents) { $responseWrapped->writeBody($contents); } } catch (Exception $e) { (new ApiErrorOutput($requestWrapped))->process($responseWrapped, $e, true); } } protected function detectPortalId() : ?string { if (!empty($_GET['portalId'])) { return $_GET['portalId']; } if (!empty($_COOKIE['auth-token'])) { $token = $this->entityManager->getRepository('AuthToken')->where(['token' => $_COOKIE['auth-token']])->findOne(); if ($token && $token->get('portalId')) { return $token->get('portalId'); } } return null; } protected function runThroughPortal(string $portalId, string $entryPoint, ?StdClass $data) { $app = new PortalApplication($portalId); $app->setClientBasePath($this->clientManager->getBasePath()); $app->run(EntryPoint::class, (object) [ 'entryPoint' => $entryPoint, 'data' => $data, 'final' => true, ]); } }