From a9e8d5b2b2aaa898fed6eeab07cb0f564cee4464 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 27 Dec 2022 14:37:34 +0200 Subject: [PATCH] auth error translatable exception message --- application/Espo/Core/Api/Auth.php | 5 +++++ .../Core/Authentication/Authentication.php | 8 +++++++- .../Core/Exceptions/ServiceUnavailable.php | 19 ++++++++++++++++++- .../Espo/Resources/i18n/en_US/Global.json | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/application/Espo/Core/Api/Auth.php b/application/Espo/Core/Api/Auth.php index 5a7ba805ca..8f7fc9646b 100644 --- a/application/Espo/Core/Api/Auth.php +++ b/application/Espo/Core/Api/Auth.php @@ -30,6 +30,7 @@ namespace Espo\Core\Api; use Espo\Core\Exceptions\BadRequest; +use Espo\Core\Exceptions\HasBody; use Espo\Core\Exceptions\ServiceUnavailable; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Authentication\ConfigDataProvider; @@ -233,6 +234,10 @@ class Auth $response->setStatus($e->getCode()); + if ($e->getBody()) { + $response->writeBody($e->getBody()); + } + $this->log->notice("Auth: " . $e->getMessage()); return; diff --git a/application/Espo/Core/Authentication/Authentication.php b/application/Espo/Core/Authentication/Authentication.php index 190cb7c5de..d717861195 100644 --- a/application/Espo/Core/Authentication/Authentication.php +++ b/application/Espo/Core/Authentication/Authentication.php @@ -30,6 +30,7 @@ namespace Espo\Core\Authentication; use Espo\Core\Authentication\Logout\Params as LogoutParams; +use Espo\Core\Exceptions\Error\Body; use Espo\Repositories\UserData as UserDataRepository; use Espo\Entities\Portal; use Espo\Entities\User; @@ -223,7 +224,12 @@ class Authentication } if (!$user->isAdmin() && $this->configDataProvider->isMaintenanceMode()) { - throw new ServiceUnavailable("Application is in maintenance mode."); + throw ServiceUnavailable::createWithBody( + "Application is in maintenance mod1e.", + Body::create() + ->withMessageTranslation('maintenanceModeError', 'messages') + ->encode() + ); } if (!$this->processUserCheck($user, $authLogRecord)) { diff --git a/application/Espo/Core/Exceptions/ServiceUnavailable.php b/application/Espo/Core/Exceptions/ServiceUnavailable.php index 62a65630fd..7d53525119 100644 --- a/application/Espo/Core/Exceptions/ServiceUnavailable.php +++ b/application/Espo/Core/Exceptions/ServiceUnavailable.php @@ -29,8 +29,25 @@ namespace Espo\Core\Exceptions; -class ServiceUnavailable extends \Exception +class ServiceUnavailable extends \Exception implements HasBody { /** @var int */ protected $code = 503; + private ?string $body = null; + + /** + * Create with a body (supposed to be sent to the frontend). + */ + public static function createWithBody(string $message, string $body): self + { + $exception = new static($message); + $exception->body = $body; + + return $exception; + } + + public function getBody(): ?string + { + return $this->body; + } } diff --git a/application/Espo/Resources/i18n/en_US/Global.json b/application/Espo/Resources/i18n/en_US/Global.json index 7b1b49de97..47f2553672 100644 --- a/application/Espo/Resources/i18n/en_US/Global.json +++ b/application/Espo/Resources/i18n/en_US/Global.json @@ -357,6 +357,7 @@ "massFollowZeroResult": "Nothing got followed", "massUnfollowZeroResult": "Nothing got unfollowed", "erasePersonalDataConfirmation": "Checked fields will be erased permanently. Are you sure?", + "maintenanceModeError": "The application currently is in maintenance mode.", "maintenanceMode": "The application currently is in maintenance mode. Only admin users have access.\n\nMaintenance mode can be disabled at Administration → Settings.", "resolveSaveConflict": "The record has been modified. You need to resolve the conflict before you can save the record.", "massPrintPdfMaxCountError": "Can't print more that {maxCount} records.",