Skip to content

EspoCRM – Open Source CRM Application

Import attachment check

Yurii9cd07314401a
diff --git a/application/Espo/Tools/Import/Import.php b/application/Espo/Tools/Import/Import.php
index f313deeb54..68e248e1b6 100644
--- a/application/Espo/Tools/Import/Import.php
+++ b/application/Espo/Tools/Import/Import.php
@@ -81,6 +81,8 @@ class Import
private const string DEFAULT_DATE_FORMAT = 'YYYY-MM-DD';
private const string DEFAULT_TIME_FORMAT = 'HH:mm';
+ public const string FILE_ROLE = 'Import File';
+
/** @var string[] */
private array $attributeList = [];
private Params $params;
@@ -205,11 +207,7 @@ class Import
assert(is_string($this->entityType));
assert(is_string($this->attachmentId));
- $attachment = $this->entityManager->getRepositoryByClass(Attachment::class)->getById($this->attachmentId);
-
- if (!$attachment) {
- throw new Error('Attachment not found.');
- }
+ $attachment = $this->getAttachment();
$contents = $this->fileStorageManager->getContents($attachment);
@@ -1584,4 +1582,33 @@ class Import
return $this->aclManager->getScopeForbiddenAttributeList($this->user, $entityType, Table::ACTION_EDIT);
}
+
+ /**
+ * @throws Error
+ * @throws Forbidden
+ */
+ private function getAttachment(): Attachment
+ {
+ $id = $this->attachmentId ?? throw new LogicException();
+
+ $attachment = $this->entityManager->getRepositoryByClass(Attachment::class)->getById($id);
+
+ if (!$attachment) {
+ throw new Error('Attachment not found.');
+ }
+
+ if ($attachment->getRole() !== self::FILE_ROLE) {
+ throw new Error('Wrong attachment.');
+ }
+
+ if (
+ !$this->user->isAdmin() &&
+ !$this->user->isSystem() &&
+ $attachment->getCreatedBy()?->getId() !== $this->user->getId()
+ ) {
+ throw new Forbidden('No access to attachment.');
+ }
+
+ return $attachment;
+ }
}
diff --git a/application/Espo/Tools/Import/Service.php b/application/Espo/Tools/Import/Service.php
index 203c48bd41..3ca9c276a7 100644
--- a/application/Espo/Tools/Import/Service.php
+++ b/application/Espo/Tools/Import/Service.php
@@ -340,7 +340,7 @@ class Service
$attachment = $this->entityManager->getRDBRepositoryByClass(Attachment::class)->getNew();
$attachment->setType('text/csv');
- $attachment->setRole('Import File');
+ $attachment->setRole(Import::FILE_ROLE);
$attachment->setName('import-file.csv');
$attachment->setContents($contents);
diff --git a/tests/integration/Espo/Tools/Import/ImportTest.php b/tests/integration/Espo/Tools/Import/ImportTest.php
index 377be290c2..99fed2f766 100644
--- a/tests/integration/Espo/Tools/Import/ImportTest.php
+++ b/tests/integration/Espo/Tools/Import/ImportTest.php
@@ -32,6 +32,7 @@ namespace integration\Espo\Tools\Import;
use Espo\Core\Acl\Table;
use Espo\Entities\Attachment;
use Espo\Entities\Role;
+use Espo\Entities\User;
use Espo\Modules\Crm\Entities\Account;
use Espo\Modules\Crm\Entities\Contact;
use Espo\Tools\Import\Import;
@@ -83,6 +84,7 @@ class ImportTest extends BaseTestCase
$attachment
->setName('test.csv')
->setType('text/csv')
+ ->setRole(Import::FILE_ROLE)
->setContents(
<<<'EOT'
lastName,accountType
@@ -99,6 +101,7 @@ class ImportTest extends BaseTestCase
$import = $this->getInjectableFactory()->create(Import::class);
$import
+ ->setUser($this->getContainer()->getByClass(User::class))
->setEntityType(Contact::ENTITY_TYPE)
->setParams(
Params::create()
@@ -145,6 +148,7 @@ class ImportTest extends BaseTestCase
$attachment
->setName('test.csv')
->setType('text/csv')
+ ->setRole(Import::FILE_ROLE)
->setContents(
<<<'EOT'
lastName,accountName
@@ -211,6 +215,7 @@ class ImportTest extends BaseTestCase
$attachment
->setName('test.csv')
->setType('text/csv')
+ ->setRole(Import::FILE_ROLE)
->setContents(
<<<'EOT'
name