injectableFactory = $injectableFactory; $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(); $entryPointManager = $this->injectableFactory->create(EntryPointManager::class); $authRequired = $entryPointManager->checkAuthRequired($entryPoint); $authNotStrict = $entryPointManager->checkNotStrictAuth($entryPoint); if ($authRequired && !$authNotStrict && !$final) { if ($portalId = $this->detectPortalId()) { $app = new PortalApplication($portalId); $app->setClientBasePath($this->clientManager->getBasePath()); $app->run(EntryPoint::class, (object) [ 'entryPoint' => $entryPoint, 'data' => $data, 'final' => true, ]); return; } } $slim = SlimAppFactory::create(); $slim->setBasePath(Route::detectBasePath()); $slim->add( function (Psr7Request $request, Psr7RequestHandler $handler) use ( $entryPointManager, $entryPoint, $data, $authRequired, $authNotStrict, $slim ) : Psr7Response { $requestWrapped = new RequestWrapper($request, $slim->getBasePath()); $responseWrapped = new ResponseWrapper($handler->handle($request)); try { $authentication = $this->injectableFactory->createWith(Authentication::class, [ 'allowAnyAccess' => $authNotStrict, ]); $apiAuth = new ApiAuth($authentication, $authRequired, true); $apiAuth->process($requestWrapped, $responseWrapped); if (!$apiAuth->isResolved()) { return $responseWrapped->getResponse(); } if ($apiAuth->isResolvedUseNoAuth()) { $this->applicationUser->setupSystemUser(); } ob_start(); $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); } return $responseWrapped->getResponse(); } ); $slim->get('/', function (Psr7Request $request, Psr7Response $response) : Psr7Response { return $response; }); $slim->run(); } 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; } }