EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Email apply users from group folder changes
Yurii463684a7edc9
diff --git a/application/Espo/Core/Mail/Importer/DefaultImporter.php b/application/Espo/Core/Mail/Importer/DefaultImporter.phpindex db76d5f018..bae0910b02 100644--- a/application/Espo/Core/Mail/Importer/DefaultImporter.php+++ b/application/Espo/Core/Mail/Importer/DefaultImporter.php@@ -29,8 +29,8 @@namespace Espo\Core\Mail\Importer;-use Espo\Core\Field\DateTime as DateTimeField;+use Espo\Core\Field\Link;use Espo\Core\Field\LinkMultiple;use Espo\Core\Field\LinkParent;use Espo\Core\Job\Job\Data as JobData;@@ -54,16 +54,10 @@ use Espo\Core\ORM\Entity as CoreEntity;use Espo\Entities\Attachment;use Espo\Entities\Email;use Espo\Entities\EmailFilter;-use Espo\Entities\GroupEmailFolder;-use Espo\Entities\Team;-use Espo\Entities\User;use Espo\ORM\Defs\Params\AttributeParam;-use Espo\ORM\Name\Attribute;-use Espo\ORM\Query\Part\Condition;-use Espo\ORM\Query\Part\Expression;-use Espo\ORM\Query\SelectBuilder;use Espo\Repositories\Email as EmailRepository;use Espo\ORM\EntityManager;+use Espo\Tools\Email\GroupFolderApplier;use Espo\Tools\Stream\Jobs\ProcessNoteAcl;use DateTime;@@ -89,6 +83,7 @@ class DefaultImporter implements Importerprivate ParentFinder $parentFinder,private AutoReplyDetector $autoReplyDetector,private EmailSaver $emailSaver,+ private GroupFolderApplier $groupFolderApplier,) {$this->notificator = $notificatorFactory->createByClass(Email::class);$this->filtersMatcher = new FiltersMatcher();@@ -208,6 +203,10 @@ class DefaultImporter implements Importerreturn $duplicate;}+ if ($email->getGroupFolder()) {+ $this->applyGroupFolder($email, $email->getGroupFolder()->getId());+ }+if (!$email->getMessageId()) {$email->setDummyMessageId();}@@ -597,7 +596,7 @@ class DefaultImporter implements Importer$matchedFilter->getAction() === EmailFilter::ACTION_MOVE_TO_GROUP_FOLDER &&$matchedFilter->getGroupEmailFolderId()) {- $this->applyGroupFolder($email, $matchedFilter->getGroupEmailFolderId());+ $email->setGroupFolderId($matchedFilter->getGroupEmailFolderId());}return false;@@ -648,53 +647,12 @@ class DefaultImporter implements Importerarray $fetchedTeamIds = [],): bool {- $email->setGroupFolderId($groupFolderId);-- $groupFolder = $this->entityManager- ->getRDBRepositoryByClass(GroupEmailFolder::class)- ->getById($groupFolderId);-- if (!$groupFolder || !$groupFolder->getTeams()->getCount()) {- return false;- }-- $added = false;-- foreach ($groupFolder->getTeams()->getIdList() as $teamId) {- if (!in_array($teamId, $fetchedTeamIds)) {- $added = true;-- $email->addTeamId($teamId);- }- }-- $users = $this->entityManager- ->getRDBRepositoryByClass(User::class)- ->select([Attribute::ID])- ->where([- 'type' => [User::TYPE_REGULAR, User::TYPE_ADMIN],- 'isActive' => true,- Attribute::ID . '!=' => $fetchedUserIds,- ])- ->where(- Condition::in(- Expression::column(Attribute::ID),- SelectBuilder::create()- ->from(Team::RELATIONSHIP_TEAM_USER)- ->select('userId')- ->where(['teamId' => $groupFolder->getTeams()->getIdList()])- ->build()- )- )- ->find();-- foreach ($users as $user) {- $added = true;-- $email->addUserId($user->getId());- }-- return $added;+ return $this->groupFolderApplier->apply(+ email: $email,+ groupFolder: Link::create($groupFolderId),+ ignoreUserIds: $fetchedUserIds,+ ignoreTeamIds: $fetchedTeamIds,+ );}private function scheduleAclJob(Email $email): voiddiff --git a/application/Espo/Entities/GroupEmailFolder.php b/application/Espo/Entities/GroupEmailFolder.phpindex 809a7e30fd..2f781a2c80 100644--- a/application/Espo/Entities/GroupEmailFolder.php+++ b/application/Espo/Entities/GroupEmailFolder.php@@ -30,6 +30,7 @@namespace Espo\Entities;use Espo\Core\Field\LinkMultiple;+use Espo\Core\Name\Field;use Espo\Core\ORM\Entity;class GroupEmailFolder extends Entity@@ -46,4 +47,12 @@ class GroupEmailFolder extends Entity/** @var LinkMultiple */return $this->getValueObject('teams');}++ /**+ * @since 10.1.0+ */+ public function setTeams(LinkMultiple $teams): self+ {+ return $this->setValueObject(Field::TEAMS, $teams);+ }}diff --git a/application/Espo/Tools/Email/GroupFolderApplier.php b/application/Espo/Tools/Email/GroupFolderApplier.phpnew file mode 100644index 0000000000..783e94d2ba--- /dev/null+++ b/application/Espo/Tools/Email/GroupFolderApplier.php@@ -0,0 +1,115 @@+<?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\Tools\Email;++use Espo\Core\Field\Link;+use Espo\Entities\Email;+use Espo\Entities\GroupEmailFolder;+use Espo\Entities\Team;+use Espo\Entities\User;+use Espo\ORM\EntityManager;+use Espo\ORM\Name\Attribute;+use Espo\ORM\Query\Part\Condition;+use Espo\ORM\Query\Part\Expression;+use Espo\ORM\Query\SelectBuilder;++/**+ * @internal+ */+class GroupFolderApplier+{+ public function __construct(+ private EntityManager $entityManager,+ ) {}++ /**+ * @param string[] $ignoreUserIds+ * @param string[] $ignoreTeamIds+ */+ public function apply(+ Email $email,+ GroupEmailFolder|Link $groupFolder,+ array $ignoreUserIds = [],+ array $ignoreTeamIds = [],+ ): bool {++ $folderId = $groupFolder->getId();++ $email->setGroupFolderId($folderId);++ if ($groupFolder instanceof Link) {+ $groupFolder = $this->entityManager->getRDBRepositoryByClass(GroupEmailFolder::class)->getById($folderId);+ }++ if (+ !$groupFolder ||+ !$groupFolder->getTeams()->getCount()+ ) {+ return false;+ }++ $added = false;++ foreach (array_diff($groupFolder->getTeams()->getIdList(), $ignoreTeamIds) as $teamId) {+ $added = true;++ $email->addTeamId($teamId);+ }++ $users = $this->entityManager+ ->getRDBRepositoryByClass(User::class)+ ->select([Attribute::ID])+ ->sth()+ ->where([+ User::FIELD_TYPE => [User::TYPE_REGULAR, User::TYPE_ADMIN],+ User::FIELD_IS_ACTIVE => true,+ Attribute::ID . '!=' => $ignoreUserIds,+ ])+ ->where(+ Condition::in(+ Expression::column(Attribute::ID),+ SelectBuilder::create()+ ->from(Team::RELATIONSHIP_TEAM_USER)+ ->select('userId')+ ->where(['teamId' => $groupFolder->getTeams()->getIdList()])+ ->build()+ )+ )+ ->find();++ foreach ($users as $user) {+ $added = true;++ $email->addUserId($user->getId());+ }++ return $added;+ }+}diff --git a/application/Espo/Tools/Email/InboxService.php b/application/Espo/Tools/Email/InboxService.phpindex 111ec8e094..9f2217eb3a 100644--- a/application/Espo/Tools/Email/InboxService.php+++ b/application/Espo/Tools/Email/InboxService.php@@ -43,13 +43,9 @@ use Espo\Entities\Email;use Espo\Entities\EmailFolder;use Espo\Entities\GroupEmailFolder;use Espo\Entities\Notification;-use Espo\Entities\Team;use Espo\Entities\User;use Espo\ORM\EntityManager;use Espo\ORM\Name\Attribute;-use Espo\ORM\Query\Part\Condition;-use Espo\ORM\Query\Part\Expression;-use Espo\ORM\Query\SelectBuilder;use Exception;use RuntimeException;@@ -62,6 +58,7 @@ class InboxServiceprivate Log $log,private SelectBuilderFactory $selectBuilderFactory,private WebSocketSubmission $webSocketSubmission,+ private GroupFolderApplier $groupFolderApplier,) {}/**@@ -170,7 +167,7 @@ class InboxService->setGroupFolder($folder)->setGroupStatusFolder(null);- $this->applyGroupFolder($email, $folder);+ $this->groupFolderApplier->apply($email, $folder);$this->entityManager->saveEntity($email);@@ -682,38 +679,4 @@ class InboxServicereturn $folder;}-- private function applyGroupFolder(Email $email, GroupEmailFolder $folder): void- {- if (!$folder->getTeams()->getCount()) {- return;- }-- foreach ($folder->getTeams()->getIdList() as $teamId) {- $email->addTeamId($teamId);- }-- $users = $this->entityManager- ->getRDBRepositoryByClass(User::class)- ->select([Attribute::ID])- ->where([- 'type' => [User::TYPE_REGULAR, User::TYPE_ADMIN],- 'isActive' => true,- ])- ->where(- Condition::in(- Expression::column(Attribute::ID),- SelectBuilder::create()- ->from(Team::RELATIONSHIP_TEAM_USER)- ->select('userId')- ->where(['teamId' => $folder->getTeams()->getIdList()])- ->build()- )- )- ->find();-- foreach ($users as $user) {- $email->addUserId($user->getId());- }- }}diff --git a/application/Espo/Tools/Email/SendService.php b/application/Espo/Tools/Email/SendService.phpindex 024e59e529..e39850f271 100644--- a/application/Espo/Tools/Email/SendService.php+++ b/application/Espo/Tools/Email/SendService.php@@ -102,6 +102,7 @@ class SendServiceprivate PersonalAccountFactory $personalAccountFactory,private GroupAccountFactory $groupAccountFactory,private ConfigDataProvider $configDataProvider,+ private GroupFolderApplier $groupFolderApplier,) {}/**@@ -599,6 +600,8 @@ class SendServiceif ($replied && $replied->getGroupFolder()) {$entity->setGroupFolder($replied->getGroupFolder());++ $this->groupFolderApplier->apply($entity, $replied->getGroupFolder());}}}diff --git a/tests/integration/Espo/Tools/Email/GroupFolderApplierTest.php b/tests/integration/Espo/Tools/Email/GroupFolderApplierTest.phpnew file mode 100644index 0000000000..6465fcd53b--- /dev/null+++ b/tests/integration/Espo/Tools/Email/GroupFolderApplierTest.php@@ -0,0 +1,68 @@+<?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 tests\integration\Espo\Tools\Email;++use Espo\Core\Field\LinkMultiple;+use Espo\Entities\Email;+use Espo\Entities\GroupEmailFolder;+use Espo\Entities\Team;+use Espo\Entities\User;+use Espo\Tools\Email\GroupFolderApplier;+use tests\integration\Core\BaseTestCase;++class GroupFolderApplierTest extends BaseTestCase+{+ public function testApply(): void+ {+ $em = $this->getEntityManager();++ $team = $this->getEntityManager()->getRDBRepositoryByClass(Team::class)->getNew();+ $em->saveEntity($team);++ $user1 = $this->getEntityManager()->getRDBRepositoryByClass(User::class)->getNew();+ $user1+ ->setUserName('test-1')+ ->setTeams(LinkMultiple::create()->withAddedId($team->getId()));+ $em->saveEntity($user1);++ $folder = $em->getRDBRepositoryByClass(GroupEmailFolder::class)->getNew();+ $folder->setTeams(LinkMultiple::create()->withAddedId($team->getId()));+ $em->saveEntity($folder);++ $email = $em->getRDBRepositoryByClass(Email::class)->getNew();++ $applier = $this->getInjectableFactory()->create(GroupFolderApplier::class);++ $applier->apply($email, $folder);++ $this->assertContains($user1->getId(), $email->getUsers()->getIdList());+ $this->assertContains($team->getId(), $email->getTeams()->getIdList());+ }+}diff --git a/tests/unit/Espo/Core/Mail/ImporterTest.php b/tests/unit/Espo/Core/Mail/ImporterTest.phpindex 8afe73ddb0..e1f4f6c807 100644--- a/tests/unit/Espo/Core/Mail/ImporterTest.php+++ b/tests/unit/Espo/Core/Mail/ImporterTest.php@@ -49,6 +49,7 @@ use Espo\Core\Repositories\Database;use Espo\Core\Utils\Config;use Espo\ORM\Repository\RDBSelectBuilder;+use Espo\Tools\Email\GroupFolderApplier;use PHPUnit\Framework\TestCase;class ImporterTest extends TestCase@@ -209,6 +210,7 @@ class ImporterTest extends TestCaseparentFinder: $this->parentFinder,autoReplyDetector: $this->createMock(Importer\AutoReplyDetector::class),emailSaver: $saver,+ groupFolderApplier: $this->createMock(GroupFolderApplier::class),);$message = new MessageWrapper(0, null, null, $contents);