entry point refactor

This commit is contained in:
Yuri Kuznetsov
2020-07-17 17:08:30 +03:00
parent 2d94ad60f3
commit f87dcda391
4 changed files with 173 additions and 43 deletions

View File

@@ -55,15 +55,23 @@ class Auth
protected $authRequired;
protected $isEntryPoint = false;
private $isResolved = false;
private $isResolvedUseNoAuth = false;
public function __construct(Authentication $authentication, bool $authRequired = true, bool $isEntryPoint = false)
public function __construct(Authentication $authentication, bool $authRequired = true)
{
$this->authentication = $authentication;
$this->authRequired = $authRequired;
$this->isEntryPoint = $isEntryPoint;
}
public function createForEntryPoint(Authentication $authentication, bool $authRequired = true)
{
$instance = new Auth($authentication, $authRequired);
$instance->isEntryPoint = true;
return $instance;
}
protected function resolve()

View File

@@ -0,0 +1,95 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Api;
use Espo\Core\{
Api\Request,
Api\Response,
};
use Espo\Core\{
EntryPointManager,
ApplicationUser,
};
use StdClass;
/**
*
*/
class EntryPoint
{
protected $authRequired;
protected $authNotStrict;
protected $entryPointManager;
public function __construct(
EntryPointManager $entryPointManager,
ApplicationUser $applicationUser,
bool $authRequired = true,
bool $authNotStrict = false,
) {
$this->entryPointManager = $entryPointManager;
$this->applicationUser = $applicationUser;
$this->authRequired = $authRequired;
$this->authNotStrict = $authNotStrict;
$this->entryPoint = $entryPoint;
$this->data = $data;
}
public function process(string $entryPoint, Request $request, Response $response, ?StdClass $data)
{
$authentication = $this->injectableFactory->createWith(Authentication::class, [
'allowAnyAccess' => $this->authNotStrict,
]);
$apiAuth = ApiAuth::createForEntryPoint($authentication, $this->authRequired);
$apiAuth->process($request, $response);
if (!$apiAuth->isResolved()) {
return;
}
if ($apiAuth->isResolvedUseNoAuth()) {
$this->applicationUser->setupSystemUser();
}
ob_start();
$this->entryPointManager->run($entryPoint, $request, $response, $data);
$contents = ob_get_clean();
if ($contents) {
$response->writeBody($contents);
}
}
}

View File

@@ -66,14 +66,20 @@ use StdClass;
class EntryPoint implements ApplicationRunner
{
protected $injectableFactory;
protected $entryPointManager;
protected $entityManager;
protected $clientManager;
protected $applicationUser;
public function __construct(
InjectableFactory $injectableFactory, EntityManager $entityManager, ClientManager $clientManager, ApplicationUser $applicationUser
InjectableFactory $injectableFactory,
EntryPointManager $entryPointManager,
EntityManager $entityManager,
ClientManager $clientManager,
ApplicationUser $applicationUser
) {
$this->injectableFactory = $injectableFactory;
$this->entryPointManager = $entryPointManager;
$this->entityManager = $entityManager;
$this->clientManager = $clientManager;
$this->applicationUser = $applicationUser;
@@ -88,22 +94,17 @@ class EntryPoint implements ApplicationRunner
$final = $params->final ?? false;
$data = $params->data ?? null;
if (!$entryPoint) throw new Error();
if (!$entryPoint) {
throw new Error();
}
$entryPointManager = $this->injectableFactory->create(EntryPointManager::class);
$authRequired = $entryPointManager->checkAuthRequired($entryPoint);
$authNotStrict = $entryPointManager->checkNotStrictAuth($entryPoint);
$authRequired = $this->entryPointManager->checkAuthRequired($entryPoint);
$authNotStrict = $this->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,
]);
$portalId = $this->detectPortalId();
if ($portalId) {
$this->runThroughPortal($portalId, $entryPoint, $data);
return;
}
}
@@ -113,37 +114,12 @@ class EntryPoint implements ApplicationRunner
$slim->add(
function (Psr7Request $request, Psr7RequestHandler $handler) use (
$entryPointManager, $entryPoint, $data, $authRequired, $authNotStrict, $slim
$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);
}
$this->processRequest($entryPoint, $requestWrapped, $responseWrapped, $data, $authRequired, $authNotStrict);
return $responseWrapped->getResponse();
}
@@ -156,6 +132,43 @@ class EntryPoint implements ApplicationRunner
$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::createForEntryPoint($authentication, $authRequired);
$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'])) {
@@ -170,4 +183,15 @@ class EntryPoint implements ApplicationRunner
}
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,
]);
}
}

View File

@@ -50,6 +50,9 @@
"hookManager": {
"className": "Espo\\Core\\HookManager"
},
"entryPointManager": {
"className": "Espo\\Core\\EntryPointManager"
},
"notificatorFactory": {
"className": "Espo\\Core\\NotificatorFactory"
},