Skip to content

EspoCRM – Open Source CRM Application

Record field duplicator pass actual user

Yurii05e3c18b86c7
diff --git a/application/Espo/Classes/FieldDuplicators/Wysiwyg.php b/application/Espo/Classes/FieldDuplicators/Wysiwyg.php
index 8e25853464..48fc254fef 100644
--- a/application/Espo/Classes/FieldDuplicators/Wysiwyg.php
+++ b/application/Espo/Classes/FieldDuplicators/Wysiwyg.php
@@ -29,23 +29,26 @@
namespace Espo\Classes\FieldDuplicators;
+use Espo\Core\Acl;
use Espo\Core\Record\Duplicator\FieldDuplicator;
+use Espo\Core\Utils\Metadata;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
-
use Espo\Repositories\Attachment as AttachmentRepository;
use Espo\Entities\Attachment;
use stdClass;
+/**
+ * @noinspection PhpUnused
+ */
class Wysiwyg implements FieldDuplicator
{
- private $entityManager;
-
- public function __construct(EntityManager $entityManager)
- {
- $this->entityManager = $entityManager;
- }
+ public function __construct(
+ private EntityManager $entityManager,
+ private Acl $acl,
+ private Metadata $metadata,
+ ) {}
public function duplicate(Entity $entity, string $field): stdClass
{
@@ -75,13 +78,20 @@ class Wysiwyg implements FieldDuplicator
$attachmentList = [];
foreach ($attachmentIdList as $id) {
- /** @var Attachment|null $attachment */
- $attachment = $this->entityManager->getEntityById(Attachment::ENTITY_TYPE, $id);
+ $attachment = $this->entityManager->getRDBRepositoryByClass(Attachment::class)->getById($id);
if (!$attachment) {
continue;
}
+ if (!$this->acl->checkEntityRead($attachment)) {
+ continue;
+ }
+
+ if (!in_array($attachment->getType(), $this->getAllowedImageFileTypeList())) {
+ continue;
+ }
+
$attachmentList[] = $attachment;
}
@@ -95,10 +105,11 @@ class Wysiwyg implements FieldDuplicator
foreach ($attachmentList as $attachment) {
$copiedAttachment = $attachmentRepository->getCopiedAttachment($attachment);
- $copiedAttachment->set([
+ $copiedAttachment->setTargetField($field);
+
+ $copiedAttachment->setMultiple([
'relatedId' => null,
'relatedType' => $entity->getEntityType(),
- 'field' => $field,
]);
$this->entityManager->saveEntity($copiedAttachment);
@@ -114,4 +125,12 @@ class Wysiwyg implements FieldDuplicator
return $valueMap;
}
+
+ /**
+ * @return string[]
+ */
+ private function getAllowedImageFileTypeList(): array
+ {
+ return $this->metadata->get(['app', 'image', 'allowedFileTypeList']) ?? [];
+ }
}
diff --git a/application/Espo/Core/Record/Duplicator/EntityDuplicator.php b/application/Espo/Core/Record/Duplicator/EntityDuplicator.php
index 4aeae5a93f..bcaee97592 100644
--- a/application/Espo/Core/Record/Duplicator/EntityDuplicator.php
+++ b/application/Espo/Core/Record/Duplicator/EntityDuplicator.php
@@ -48,7 +48,7 @@ class EntityDuplicator
private Defs $defs,
private FieldDuplicatorFactory $fieldDuplicatorFactory,
private FieldUtil $fieldUtil,
- private Metadata $metadata
+ private Metadata $metadata,
) {}
public function duplicate(Entity $entity): stdClass
diff --git a/application/Espo/Core/Record/Duplicator/FieldDuplicatorFactory.php b/application/Espo/Core/Record/Duplicator/FieldDuplicatorFactory.php
index 79b2da3cf1..9637fb1fde 100644
--- a/application/Espo/Core/Record/Duplicator/FieldDuplicatorFactory.php
+++ b/application/Espo/Core/Record/Duplicator/FieldDuplicatorFactory.php
@@ -29,6 +29,10 @@
namespace Espo\Core\Record\Duplicator;
+use Espo\Core\Acl;
+use Espo\Core\Binding\BindingContainer;
+use Espo\Core\Binding\BindingContainerBuilder;
+use Espo\Entities\User;
use Espo\ORM\Defs;
use Espo\Core\Utils\Metadata;
use Espo\Core\InjectableFactory;
@@ -40,7 +44,9 @@ class FieldDuplicatorFactory
public function __construct(
private Defs $defs,
private Metadata $metadata,
- private InjectableFactory $injectableFactory
+ private InjectableFactory $injectableFactory,
+ private Acl $acl,
+ private User $user,
) {}
public function create(string $entityType, string $field): FieldDuplicator
@@ -51,7 +57,7 @@ class FieldDuplicatorFactory
throw new RuntimeException("No field duplicator for the field.");
}
- return $this->injectableFactory->create($className);
+ return $this->injectableFactory->createWithBinding($className, $this->createBinding());
}
public function has(string $entityType, string $field): bool
@@ -86,4 +92,12 @@ class FieldDuplicatorFactory
return null;
}
+
+ private function createBinding(): BindingContainer
+ {
+ return BindingContainerBuilder::create()
+ ->bindInstance(User::class, $this->user)
+ ->bindInstance(Acl::class, $this->acl)
+ ->build();
+ }
}
diff --git a/application/Espo/Core/Record/Service.php b/application/Espo/Core/Record/Service.php
index fb5cde1fd4..ad31c121f1 100644
--- a/application/Espo/Core/Record/Service.php
+++ b/application/Espo/Core/Record/Service.php
@@ -1473,7 +1473,7 @@ class Service implements Crud,
private function createEntityDuplicator(): EntityDuplicator
{
- return $this->injectableFactory->create(EntityDuplicator::class);
+ return $this->injectableFactory->createWithBinding(EntityDuplicator::class, $this->createBinding());
}
/**