discard userData imap and smtp handlers

This commit is contained in:
Yuri Kuznetsov
2024-12-05 15:49:40 +02:00
parent 7f0e1cc4ba
commit 06afd19aa5
3 changed files with 2 additions and 132 deletions
@@ -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);
}
}
@@ -1,11 +1,5 @@
{
"fields": {
"imapHandlers": {
"type": "jsonObject"
},
"smtpHandlers": {
"type": "jsonObject"
},
"auth2FA": {
"type": "bool"
},
+1 -85
View File
@@ -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<string, mixed> $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<object> $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();