Skip to content

EspoCRM – Open Source CRM Application

Ref

Yurii90876fc8201d
diff --git a/application/Espo/Core/Mail/Account/PersonalAccount/Service.php b/application/Espo/Core/Mail/Account/PersonalAccount/Service.php
index 5de3192ed6..837858a899 100644
--- a/application/Espo/Core/Mail/Account/PersonalAccount/Service.php
+++ b/application/Espo/Core/Mail/Account/PersonalAccount/Service.php
@@ -34,7 +34,6 @@ use Espo\Core\Mail\Account\Util\AddressUtil;
use Espo\Core\Mail\Account\Util\NotificationHelper;
use Espo\Core\Mail\Exceptions\ImapError;
use Espo\Core\Mail\Exceptions\NoImap;
-use Espo\Core\Utils\Config;
use Espo\Core\Utils\Log;
use Espo\Core\Mail\Account\Account as Account;
use Espo\Core\Exceptions\Forbidden;
@@ -90,42 +89,22 @@ class Service
*/
public function getFolderList(Params $params): array
{
- $userId = $params->getUserId();
+ $params = $this->connectPreProcess($params);
- if (
- $userId &&
- !$this->user->isAdmin() &&
- $userId !== $this->user->getId()
- ) {
- throw new Forbidden();
- }
-
- if (
- $params->getHost() &&
- !$this->addressUtil->isAllowedAddress($params) &&
- !$this->hostCheck->isHostAndNotInternal($params->getHost())
- ) {
- throw new Forbidden("Not allowed internal host.");
- }
+ try {
+ $storage = $this->storageFactory->createWithParams($params);
- if ($params->getId()) {
- $account = $this->accountFactory->create($params->getId());
+ return $storage->getFolderNames();
+ } catch (Exception $e) {
+ $this->log->warning("IMAP get folder list failed.", [
+ 'exception' => $e,
+ ]);
- if (
- !$this->user->isAdmin() &&
- $account->getUser()->getId() !== $this->user->getId()
- ) {
- throw new Forbidden();
- }
+ $message = $e instanceof ImapError ?
+ $e->getMessage() : '';
- $params = $params
- ->withPassword($this->getPassword($params, $account))
- ->withImapHandlerClassName($account->getImapHandlerClassName());
+ throw new ErrorSilent($message);
}
-
- $storage = $this->storageFactory->createWithParams($params);
-
- return $storage->getFolderNames();
}
/**
@@ -134,45 +113,11 @@ class Service
*/
public function testConnection(Params $params): void
{
- $userId = $params->getUserId();
-
- if (
- $userId &&
- !$this->user->isAdmin() &&
- $userId !== $this->user->getId()
- ) {
- throw new Forbidden();
- }
-
- if (!$params->getId() && $params->getPassword() === null) {
- throw new Forbidden();
- }
-
- if (
- $params->getHost() &&
- !$this->addressUtil->isAllowedAddress($params) &&
- !$this->hostCheck->isHostAndNotInternal($params->getHost())
- ) {
- throw new Forbidden("Not allowed host.");
- }
-
- if ($params->getId()) {
- $account = $this->accountFactory->create($params->getId());
-
- if (
- !$this->user->isAdmin() &&
- $account->getUser()->getId() !== $this->user->getId()
- ) {
- throw new Forbidden();
- }
-
- $params = $params
- ->withPassword($this->getPassword($params, $account))
- ->withImapHandlerClassName($account->getImapHandlerClassName());
- }
+ $params = $this->connectPreProcess($params);
try {
$storage = $this->storageFactory->createWithParams($params);
+
$storage->getFolderNames();
} catch (Exception $e) {
$this->log->warning("IMAP test connection failed.", [
@@ -219,4 +164,52 @@ class Service
$storage->appendMessage($message->toString(), $folder);
}
+
+ /**
+ * @param Params $params
+ * @return Params
+ * @throws Error
+ * @throws Forbidden
+ */
+ private function connectPreProcess(Params $params): Params
+ {
+ $userId = $params->getUserId();
+
+ if (
+ $userId &&
+ !$this->user->isAdmin() &&
+ $userId !== $this->user->getId()
+ ) {
+ throw new Forbidden();
+ }
+
+ if (!$params->getId() && $params->getPassword() === null) {
+ throw new Forbidden();
+ }
+
+ if (
+ $params->getHost() &&
+ !$this->addressUtil->isAllowedAddress($params) &&
+ !$this->hostCheck->isHostAndNotInternal($params->getHost())
+ ) {
+ throw new Forbidden("Not allowed internal host.");
+ }
+
+ if ($params->getId()) {
+ $account = $this->accountFactory->create($params->getId());
+
+ if (
+ !$this->user->isAdmin() &&
+ $account->getUser()->getId() !== $this->user->getId()
+ ) {
+ throw new Forbidden();
+ }
+
+ $params = $params
+ ->withPassword($this->getPassword($params, $account))
+ ->withImapHandlerClassName($account->getImapHandlerClassName());
+ }
+
+ return $params;
+ }
}