From 94065bfa3488ca19eb9eec786382527cd65b999d Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 10 Oct 2024 18:42:48 +0300 Subject: [PATCH] email to task originalEmailId --- .../Classes/RecordHooks/Task/BeforeCreate.php | 72 +++++++++++++++++++ .../Resources/metadata/recordDefs/Task.json | 3 + client/src/views/email/detail.js | 2 +- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 application/Espo/Modules/Crm/Classes/RecordHooks/Task/BeforeCreate.php diff --git a/application/Espo/Modules/Crm/Classes/RecordHooks/Task/BeforeCreate.php b/application/Espo/Modules/Crm/Classes/RecordHooks/Task/BeforeCreate.php new file mode 100644 index 0000000000..6571f4eb7e --- /dev/null +++ b/application/Espo/Modules/Crm/Classes/RecordHooks/Task/BeforeCreate.php @@ -0,0 +1,72 @@ +. + * + * 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\Modules\Crm\Classes\RecordHooks\Task; + +use Espo\Core\Acl; +use Espo\Core\Exceptions\BadRequest; +use Espo\Core\Exceptions\Forbidden; +use Espo\Core\Record\Hook\SaveHook; +use Espo\Entities\Email; +use Espo\Modules\Crm\Entities\Task; +use Espo\ORM\Entity; +use Espo\ORM\EntityManager; + +/** + * @implements SaveHook + */ +class BeforeCreate implements SaveHook +{ + public function __construct( + private Acl $acl, + private EntityManager $entityManager, + ) {} + + public function process(Entity $entity): void + { + /** @var ?string $emailId */ + $emailId = $entity->get('originalEmailId'); + + if ($emailId === null) { + return; + } + + $email = $this->entityManager->getRDBRepositoryByClass(Email::class)->getById($emailId); + + if (!$email) { + throw new BadRequest("Email record not found."); + } + + if (!$this->acl->checkEntityRead($entity)) { + throw new Forbidden("No access to email."); + } + + $entity->set('emailId', $emailId); + } +} diff --git a/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Task.json b/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Task.json index ec2621635e..4465744a0b 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Task.json +++ b/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Task.json @@ -4,5 +4,8 @@ ], "saverClassNameList": [ "Espo\\Core\\FieldProcessing\\Reminder\\Saver" + ], + "beforeCreateHookClassNameList": [ + "Espo\\Modules\\Crm\\Classes\\RecordHooks\\Task\\BeforeCreate" ] } diff --git a/client/src/views/email/detail.js b/client/src/views/email/detail.js index a95a9139fd..112908d678 100644 --- a/client/src/views/email/detail.js +++ b/client/src/views/email/detail.js @@ -357,7 +357,7 @@ class EmailDetailView extends DetailView { attributes.parentId = this.model.get('parentId'); attributes.parentName = this.model.get('parentName'); attributes.parentType = this.model.get('parentType'); - attributes.emailId = this.model.id; + attributes.originalEmailId = this.model.id; const subject = this.model.get('name');