Skip to content

EspoCRM – Open Source CRM Application

Entry Point ref

Yurii579527a83bc6
diff --git a/application/Espo/Core/Application/RunnerParameterized.php b/application/Espo/Core/Application/RunnerParameterized.php
index a1c3438bda..fd5d9b0008 100644
--- a/application/Espo/Core/Application/RunnerParameterized.php
+++ b/application/Espo/Core/Application/RunnerParameterized.php
@@ -29,6 +29,7 @@
namespace Espo\Core\Application;
+use Espo\Core\Application\Exceptions\RunnerException;
use Espo\Core\Application\Runner\Params;
/**
@@ -36,5 +37,8 @@ use Espo\Core\Application\Runner\Params;
*/
interface RunnerParameterized
{
+ /**
+ * @throws RunnerException
+ */
public function run(Params $params): void;
}
diff --git a/application/Espo/Core/ApplicationRunners/EntryPoint.php b/application/Espo/Core/ApplicationRunners/EntryPoint.php
index a1dacef1e7..691ea558ae 100644
--- a/application/Espo/Core/ApplicationRunners/EntryPoint.php
+++ b/application/Espo/Core/ApplicationRunners/EntryPoint.php
@@ -29,23 +29,77 @@
namespace Espo\Core\ApplicationRunners;
+use Espo\Core\Application\Exceptions\RunnerException;
use Espo\Core\Application\RunnerParameterized;
use Espo\Core\Application\Runner\Params;
+use Espo\Core\EntryPoint\Params as EntryPointParams;
use Espo\Core\EntryPoint\Starter;
+use Espo\Core\Exceptions\BadRequest;
+use Espo\Core\Exceptions\Forbidden;
+use Espo\Core\Exceptions\NotFound;
+use RuntimeException;
/**
* Runs an entry point.
*/
class EntryPoint implements RunnerParameterized
{
+ public const string PARAM_ENTRY_POINT = 'entryPoint';
+ public const string PARAM_FINAL = 'final';
+ public const string PARAM_ALLOWED_METHODS = 'allowedMethods';
+
public function __construct(private Starter $starter)
{}
public function run(Params $params): void
{
- $this->starter->start(
- $params->get('entryPoint'),
- $params->get('final') ?? false
+ $entryPointParams = new EntryPointParams(
+ name: $this->getName($params),
+ allowedMethods: $this->getAllowedMethods($params),
+ final: $params->get(self::PARAM_FINAL) === true,
);
+
+ try {
+ $this->starter->start($entryPointParams);
+ } catch (BadRequest|Forbidden|NotFound $e) {
+ $message = $e->getMessage();
+
+ throw new RunnerException($message, previous: $e);
+ }
+ }
+
+ private function getName(Params $params): ?string
+ {
+ $name = $params->get(self::PARAM_ENTRY_POINT);
+
+ if ($name !== null && !is_string($name)) {
+ throw new RuntimeException("Bad 'entryPoint' value.");
+ }
+
+ return $name;
+ }
+
+ /**
+ * @return ?string[]
+ */
+ private function getAllowedMethods(Params $params): ?array
+ {
+ $value = $params->get(self::PARAM_ALLOWED_METHODS);
+
+ if ($value === null) {
+ return null;
+ }
+
+ if (!is_array($value)) {
+ throw new RuntimeException("Bad 'allowedMethods' value.");
+ }
+
+ foreach ($value as $it) {
+ if (!is_string($it)) {
+ throw new RuntimeException("Bad 'allowedMethods' item value.");
+ }
+ }
+
+ return $value;
}
}
diff --git a/application/Espo/Core/EntryPoint/Params.php b/application/Espo/Core/EntryPoint/Params.php
new file mode 100644
index 0000000000..4a45f77247
--- /dev/null
+++ b/application/Espo/Core/EntryPoint/Params.php
@@ -0,0 +1,45 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://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 Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\EntryPoint;
+
+/**
+ * @since 10.1.0
+ */
+readonly class Params
+{
+ /**
+ * @param string[] $allowedMethods
+ */
+ public function __construct(
+ public ?string $name = null,
+ public ?array $allowedMethods = null,
+ public bool $final = false,
+ ) {}
+}
diff --git a/application/Espo/Core/EntryPoint/Starter.php b/application/Espo/Core/EntryPoint/Starter.php
index 2f99ff946b..1ce1152ef5 100644
--- a/application/Espo/Core/EntryPoint/Starter.php
+++ b/application/Espo/Core/EntryPoint/Starter.php
@@ -29,9 +29,11 @@
namespace Espo\Core\EntryPoint;
+use Espo\Core\Api\Method;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Application\Runner\Params as RunnerParams;
use Espo\Core\ApplicationUser;
+use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Portal\Application as PortalApplication;
use Espo\Core\Authentication\AuthenticationFactory;
@@ -63,28 +65,34 @@ class Starter
private ApplicationUser $applicationUser,
private AuthTokenManager $authTokenManager,
private AuthBuilderFactory $authBuilderFactory,
- private ErrorOutput $errorOutput
+ private ErrorOutput $errorOutput,
) {}
/**
* @throws BadRequest
+ * @throws Forbidden
+ * @throws NotFound
*/
- public function start(?string $entryPoint = null, bool $final = false): void
+ public function start(Params $params = new Params()): void
{
$requestWrapped = new RequestWrapper(
ServerRequestCreatorFactory::create()->createServerRequestFromGlobals(),
Route::detectBasePath()
);
- if ($requestWrapped->getMethod() !== 'GET') {
- throw new BadRequest("Only GET requests allowed for entry points.");
- }
+ $method = $requestWrapped->getMethod();
- if ($entryPoint === null) {
- $entryPoint = $requestWrapped->getQueryParam('entryPoint');
+ if ($params->allowedMethods !== null) {
+ if (!in_array($method, $params->allowedMethods)) {
+ throw new BadRequest("Method '$method' is not allowed.");
+ }
+ } else if ($method !== Method::GET) {
+ throw new BadRequest("Only GET requests allowed for the entry point.");
}
- if (!$entryPoint) {
+ $name = $params->name ?? $requestWrapped->getQueryParam('entryPoint');
+
+ if (!$name) {
throw new BadRequest("No 'entryPoint' param.");
}
@@ -95,8 +103,8 @@ class Starter
*/
$portalId = Url::getPortalIdFromEnv();
- if ($portalId && !$final) {
- $this->runThroughPortal($portalId, $entryPoint);
+ if ($portalId && !$params->final) {
+ $this->runThroughPortal($portalId, $name);
return;
}
@@ -104,7 +112,7 @@ class Starter
$responseWrapped = new ResponseWrapper(new Response());
try {
- $authRequired = $this->entryPointManager->checkAuthRequired($entryPoint);
+ $authRequired = $this->entryPointManager->checkAuthRequired($name);
} catch (NotFound $exception) {
$this->errorOutput->processWithBodyPrinting($requestWrapped, $responseWrapped, $exception);
@@ -113,21 +121,21 @@ class Starter
return;
}
- if ($authRequired && !$final) {
+ if ($authRequired && !$params->final) {
$portalId = $this->detectPortalId($requestWrapped);
if ($portalId) {
- $this->runThroughPortal($portalId, $entryPoint);
+ $this->runThroughPortal($portalId, $name);
return;
}
}
$this->processRequest(
- $entryPoint,
- $requestWrapped,
- $responseWrapped,
- $authRequired
+ entryPoint: $name,
+ requestWrapped: $requestWrapped,
+ responseWrapped: $responseWrapped,
+ authRequired: $authRequired,
);
(new ResponseEmitter())->emit($responseWrapped->toPsr7());
@@ -137,15 +145,15 @@ class Starter
string $entryPoint,
RequestWrapper $requestWrapped,
ResponseWrapper $responseWrapped,
- bool $authRequired
+ bool $authRequired,
): void {
try {
$this->processRequestInternal(
- $entryPoint,
- $requestWrapped,
- $responseWrapped,
- $authRequired
+ entryPoint: $entryPoint,
+ request: $requestWrapped,
+ response: $responseWrapped,
+ authRequired: $authRequired,
);
} catch (Exception $exception) {
$this->errorOutput->processWithBodyPrinting($requestWrapped, $responseWrapped, $exception);
@@ -160,7 +168,7 @@ class Starter
string $entryPoint,
RequestWrapper $request,
ResponseWrapper $response,
- bool $authRequired
+ bool $authRequired,
): void {
$authentication = $this->authenticationFactory->create();
@@ -207,27 +215,26 @@ class Starter
$authToken = $this->authTokenManager->get($token);
- if ($authToken) {
- return $authToken->getPortalId();
- }
-
- return null;
+ return $authToken?->getPortalId();
}
+ /**
+ * @throws Forbidden
+ * @throws NotFound
+ */
private function runThroughPortal(string $portalId, string $entryPoint): void
{
$app = new PortalApplication($portalId);
- $clientManager = $app->getContainer()
- ->getByClass(ClientManager::class);
+ $clientManager = $app->getContainer()->getByClass(ClientManager::class);
$clientManager->setBasePath($this->clientManager->getBasePath());
$clientManager->setApiUrl('api/v1/portal-access/' . $portalId);
$clientManager->setApplicationId($portalId);
$params = RunnerParams::fromArray([
- 'entryPoint' => $entryPoint,
- 'final' => true,
+ EntryPointRunner::PARAM_ENTRY_POINT => $entryPoint,
+ EntryPointRunner::PARAM_FINAL => true,
]);
$app->run(EntryPointRunner::class, $params);
diff --git a/public/oauth-callback.php b/public/oauth-callback.php
index 98e215502b..011390800f 100644
--- a/public/oauth-callback.php
+++ b/public/oauth-callback.php
@@ -37,5 +37,5 @@ $app = new Application();
$app->run(
EntryPoint::class,
- Params::create()->with('entryPoint', 'oauthCallback')
+ Params::create()->with(EntryPoint::PARAM_ENTRY_POINT, 'oauthCallback')
);
diff --git a/public/oauth/callback/index.php b/public/oauth/callback/index.php
index fbad5cd532..c5263c4361 100644
--- a/public/oauth/callback/index.php
+++ b/public/oauth/callback/index.php
@@ -37,5 +37,5 @@ $app = new Application();
$app->run(
EntryPoint::class,
- Params::create()->with('entryPoint', 'oauthCallback')
+ Params::create()->with(EntryPoint::PARAM_ENTRY_POINT, 'oauthCallback')
);
diff --git a/public/portal/oauth-callback.php b/public/portal/oauth-callback.php
index 4a9d9195d2..344ccf46b9 100644
--- a/public/portal/oauth-callback.php
+++ b/public/portal/oauth-callback.php
@@ -37,5 +37,5 @@ $app = new Application();
$app->run(
EntryPoint::class,
- Params::create()->with('entryPoint', 'oauthCallback')
+ Params::create()->with(EntryPoint::PARAM_ENTRY_POINT, 'oauthCallback')
);