From 06afd19aa5e1dd9e0fd99e813f586d808a188009 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 5 Dec 2024 15:49:40 +0200 Subject: [PATCH] discard userData imap and smtp handlers --- .../PersonalAccount/StorageFactory.php | 42 +-------- .../metadata/entityDefs/UserData.json | 6 -- application/Espo/Tools/Email/SendService.php | 86 +------------------ 3 files changed, 2 insertions(+), 132 deletions(-) diff --git a/application/Espo/Core/Mail/Account/PersonalAccount/StorageFactory.php b/application/Espo/Core/Mail/Account/PersonalAccount/StorageFactory.php index 3625b7f554..f8d48a8efa 100644 --- a/application/Espo/Core/Mail/Account/PersonalAccount/StorageFactory.php +++ b/application/Espo/Core/Mail/Account/PersonalAccount/StorageFactory.php @@ -38,14 +38,12 @@ use Espo\Core\Mail\Mail\Storage\Imap; use Espo\Core\Mail\Account\Storage\LaminasStorage; use Espo\Core\Utils\Log; use Espo\Core\InjectableFactory; -use Espo\Entities\UserData; use Espo\ORM\Name\Attribute; -use Espo\Repositories\UserData as UserDataRepository; -use Espo\ORM\EntityManager; use Laminas\Mail\Protocol\Exception\RuntimeException as ProtocolRuntimeException; use Laminas\Mail\Storage\Exception\InvalidArgumentException; use Laminas\Mail\Storage\Exception\RuntimeException; + use LogicException; use Throwable; @@ -54,7 +52,6 @@ class StorageFactory implements StorageFactoryInterface public function __construct( private Log $log, private InjectableFactory $injectableFactory, - private EntityManager $entityManager ) {} public function create(Account $account): LaminasStorage @@ -105,8 +102,6 @@ class StorageFactory implements StorageFactoryInterface $rawParams['security'] = $params->getSecurity(); } - $emailAddress = $rawParams['emailAddress'] ?? null; - $userId = $rawParams['userId'] ?? null; /** @var ?class-string $handlerClassName */ $handlerClassName = $rawParams['imapHandler'] ?? null; @@ -130,35 +125,6 @@ class StorageFactory implements StorageFactoryInterface } } - if ($emailAddress && $userId && !$handlerClassName) { - $emailAddress = strtolower($emailAddress); - - $userData = $this->getUserDataRepository()->getByUserId($userId); - - if ($userData) { - $imapHandlers = $userData->get('imapHandlers') ?? (object) []; - - if (isset($imapHandlers->$emailAddress)) { - /** @var class-string $handlerClassName */ - $handlerClassName = $imapHandlers->$emailAddress; - - try { - $handler = $this->injectableFactory->create($handlerClassName); - } catch (Throwable $e) { - $this->log->error( - "EmailAccount: Could not create Imap Handler for {$emailAddress}. Error: " . - $e->getMessage() - ); - } - - if ($handler && method_exists($handler, 'prepareProtocol')) { - // @todo Incorporate an interface `LaminasProtocolPreparator`. - $imapParams = $handler->prepareProtocol($userId, $emailAddress, $rawParams); - } - } - } - } - if (!$imapParams) { $imapParams = [ 'host' => $rawParams['host'], @@ -180,10 +146,4 @@ class StorageFactory implements StorageFactoryInterface return new LaminasStorage($storage); } - - private function getUserDataRepository(): UserDataRepository - { - /** @var UserDataRepository */ - return $this->entityManager->getRepository(UserData::ENTITY_TYPE); - } } diff --git a/application/Espo/Resources/metadata/entityDefs/UserData.json b/application/Espo/Resources/metadata/entityDefs/UserData.json index 7a4cc0d368..d49bc88d72 100644 --- a/application/Espo/Resources/metadata/entityDefs/UserData.json +++ b/application/Espo/Resources/metadata/entityDefs/UserData.json @@ -1,11 +1,5 @@ { "fields": { - "imapHandlers": { - "type": "jsonObject" - }, - "smtpHandlers": { - "type": "jsonObject" - }, "auth2FA": { "type": "bool" }, diff --git a/application/Espo/Tools/Email/SendService.php b/application/Espo/Tools/Email/SendService.php index 1174648e68..4600089b24 100644 --- a/application/Espo/Tools/Email/SendService.php +++ b/application/Espo/Tools/Email/SendService.php @@ -36,7 +36,6 @@ use Espo\Core\Exceptions\ErrorSilent; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\NotFound; use Espo\Core\FieldValidation\FieldValidationManager; -use Espo\Core\InjectableFactory; use Espo\Core\Mail\Account\Account; use Espo\Core\Mail\Account\GroupAccount\Account as GroupAccount; use Espo\Core\Mail\Account\GroupAccount\AccountFactory as GroupAccountFactory; @@ -60,17 +59,14 @@ use Espo\Entities\EmailAccount; use Espo\Entities\EmailAddress; use Espo\Entities\InboundEmail; use Espo\Entities\User; -use Espo\Entities\UserData; use Espo\Modules\Crm\Entities\CaseObj; use Espo\ORM\Collection; use Espo\ORM\Entity; use Espo\ORM\EntityManager; -use Espo\Repositories\UserData as UserDataRepository; use Espo\Tools\Stream\Service as StreamService; use Exception; use Laminas\Mail\Message; use LogicException; -use Throwable; use const FILTER_VALIDATE_EMAIL; @@ -94,7 +90,6 @@ class SendService private StreamService $streamService, private Config $config, private Log $log, - private InjectableFactory $injectableFactory, private Acl $acl, private SendingAccountProvider $accountProvider, private PersonalAccountService $personalAccountService, @@ -171,9 +166,6 @@ class SendService } if ($user && $smtpParams) { - // For bc. - $smtpParams = $this->applyUserHandler($user, $smtpParams, $fromAddress); - $emailSender->withSmtpParams($smtpParams); } @@ -377,19 +369,6 @@ class SendService return [$smtpParams, $groupAccount]; } - private function applyUserHandler(User $user, SmtpParams $smtpParams, string $emailAddress): SmtpParams - { - $raw = $smtpParams->toArray(); - - $applied = $this->applyUserHandlerInternal($user->getId(), $emailAddress, $raw); - - if ($applied) { - return SmtpParams::fromArray($raw); - } - - return $smtpParams; - } - /** * @throws Forbidden * @throws Error @@ -409,8 +388,6 @@ class SendService ); } - $fromAddress = $params->getFromAddress(); - if ( $userId && $userId !== $this->user->getId() && @@ -458,10 +435,6 @@ class SendService $params = $this->handlerProcessor->handle($handlerClassName, $params, $id); } - if ($user && $fromAddress) { - $params = $this->applyUserHandler($user, $params, $fromAddress); - } - try { $this->emailSender ->withSmtpParams($params) @@ -541,61 +514,10 @@ class SendService $smtpParams = $account->getSmtpParams(); - if (!$smtpParams) { - return null; - } - - // For bc. - $smtpParams = $this->applyUserHandler($user, $smtpParams, strtolower($address)); - return $smtpParams - ->withFromName($user->getName()) + ?->withFromName($user->getName()) ->withFromAddress($address); - } - /** - * @internal For bc. - * @param array $params - */ - private function applyUserHandlerInternal(string $userId, string $emailAddress, array &$params): bool - { - $userData = $this->getUserDataRepository()->getByUserId($userId); - - if (!$userData) { - return false; - } - - $smtpHandlers = $userData->get('smtpHandlers') ?? (object) []; - - if (!is_object($smtpHandlers)) { - return false; - } - - if (!isset($smtpHandlers->$emailAddress)) { - return false; - } - - /** @var class-string $handlerClassName */ - $handlerClassName = $smtpHandlers->$emailAddress; - - try { - $handler = $this->injectableFactory->create($handlerClassName); - } catch (Throwable $e) { - $this->log->error( - "Email sending: Could not create Smtp Handler for $emailAddress. Error: " . - $e->getMessage() . "." - ); - - return false; - } - - if (method_exists($handler, 'applyParams')) { - $handler->applyParams($userId, $emailAddress, $params); - - return true; - } - - return false; } /** @@ -647,12 +569,6 @@ class SendService return $this->config->get('smtpPassword'); } - private function getUserDataRepository(): UserDataRepository - { - /** @var UserDataRepository */ - return $this->entityManager->getRepository(UserData::ENTITY_TYPE); - } - private function applyReplied(Email $entity, Message $message): void { $replied = $entity->getReplied();