EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Link check attachment handling
Yuriiec473090e31a
diff --git a/application/Espo/Core/Record/Access/LinkCheck.php b/application/Espo/Core/Record/Access/LinkCheck.phpindex 0e0901bf74..2c28fc3120 100644--- a/application/Espo/Core/Record/Access/LinkCheck.php+++ b/application/Espo/Core/Record/Access/LinkCheck.php@@ -41,6 +41,7 @@ use Espo\Core\Name\Field;use Espo\Core\ORM\Defs\AttributeParam;use Espo\Core\ORM\Type\FieldType;use Espo\Core\Utils\Metadata;+use Espo\Entities\Attachment;use Espo\Entities\User;use Espo\Modules\Crm\Entities\Account;use Espo\Modules\Crm\Entities\Contact;@@ -249,7 +250,7 @@ class LinkCheck);}- $toSkip = $this->linkForeignAccessCheck($isOne, $entityType, $link, $foreignEntity);+ $toSkip = $this->linkForeignAccessCheck($isOne, $entity, $link, $foreignEntity);if ($toSkip) {return $foreignEntity;@@ -265,16 +266,16 @@ class LinkCheck*/private function linkForeignAccessCheck(bool $isOne,- string $entityType,+ Entity $entity,string $link,- Entity $foreignEntity+ Entity $foreignEntity,): bool {if ($isOne) {- return $this->linkForeignAccessCheckOne($entityType, $link, $foreignEntity);+ return $this->linkForeignAccessCheckOne($entity, $link, $foreignEntity);}- return $this->linkForeignAccessCheckMany($entityType, $link, $foreignEntity, true);+ return $this->linkForeignAccessCheckMany($entity, $link, $foreignEntity, true);}private function getParam(string $entityType, string $link, string $param): mixed@@ -340,7 +341,7 @@ class LinkCheck*/private function processLinkForeignInternal(Entity $entity, string $link, Entity $foreignEntity): void{- $toSkip = $this->linkForeignAccessCheckMany($entity->getEntityType(), $link, $foreignEntity);+ $toSkip = $this->linkForeignAccessCheckMany($entity, $link, $foreignEntity);if ($toSkip) {return;@@ -367,12 +368,14 @@ class LinkCheck* @throws Forbidden*/private function linkForeignAccessCheckMany(- string $entityType,+ Entity $entity,string $link,Entity $foreignEntity,- bool $fromUpdate = false+ bool $fromUpdate = false,): bool {+ $entityType = $entity->getEntityType();+/** @var AclTable::ACTION_* $action */$action = $this->getParam($entityType, $link, 'linkRequiredForeignAccess') ?? AclTable::ACTION_EDIT;@@ -406,6 +409,14 @@ class LinkCheckreturn true;}+ // Check parent and related IDs instead?+ if ($foreignEntity instanceof Attachment && !$this->checkAttachment($entity, $foreignEntity)) {+ throw ForbiddenSilent::createWithBody(+ "Cannot link an already linked attachment ($entityType:$link).",+ ErrorBody::create()+ );+ }+if ($this->acl->check($foreignEntity, $action)) {return false;}@@ -627,8 +638,10 @@ class LinkCheck* @return bool True indicates that the link checker should be bypassed.* @throws Forbidden*/- private function linkForeignAccessCheckOne(string $entityType, string $link, Entity $foreignEntity): bool+ private function linkForeignAccessCheckOne(Entity $entity, string $link, Entity $foreignEntity): bool{+ $entityType = $entity->getEntityType();+if ($this->getParam($entityType, $link, 'linkForeignAccessCheckDisabled')) {return true;}@@ -647,6 +660,13 @@ class LinkCheck}}+ if ($foreignEntity instanceof Attachment && !$this->checkAttachment($entity, $foreignEntity)) {+ throw ForbiddenSilent::createWithBody(+ "Cannot link an already linked attachment ($entityType:$link).",+ ErrorBody::create()+ );+ }+if ($this->checkIsAllowedForPortal($foreignEntity)) {return true;}@@ -747,4 +767,37 @@ class LinkCheck->encode());}++ private function checkAttachment(Entity $entity, Attachment $attachment): bool+ {+ $parentType = $attachment->getParentType();+ $parentId = $attachment->getParentId();++ if ($parentType && $parentId) {+ $toSame =+ $entity->getEntityType() === $parentType &&+ $entity->hasId() &&+ $entity->getId() === $parentId;++ if (!$toSame) {+ return false;+ }+ }++ $relatedType = $attachment->getRelatedType();+ $relatedId = $attachment->getRelatedId();++ if ($relatedType && $relatedId) {+ $toSame =+ $entity->getEntityType() === $relatedType &&+ $entity->hasId() &&+ $entity->getId() === $relatedId;++ if (!$toSame) {+ return false;+ }+ }++ return true;+ }}diff --git a/application/Espo/Entities/Attachment.php b/application/Espo/Entities/Attachment.phpindex b0971e4e57..5bd64ae1e1 100644--- a/application/Espo/Entities/Attachment.php+++ b/application/Espo/Entities/Attachment.php@@ -114,11 +114,27 @@ class Attachment extends Entityreturn $this->get('parentType');}+ /**+ * @since 10.0.8+ */+ public function getParentId(): ?string+ {+ return $this->get('parentId');+ }+public function getRelatedType(): ?string{return $this->get('relatedType');}+ /**+ * @since 10.0.8+ */+ public function getRelatedId(): ?string+ {+ return $this->get('relatedId');+ }+public function isBeingUploaded(): bool{return (bool) $this->get('isBeingUploaded');diff --git a/application/Espo/Modules/Crm/Entities/CaseObj.php b/application/Espo/Modules/Crm/Entities/CaseObj.phpindex ef26875b20..a9ecc4bf5a 100644--- a/application/Espo/Modules/Crm/Entities/CaseObj.php+++ b/application/Espo/Modules/Crm/Entities/CaseObj.php@@ -48,6 +48,11 @@ class CaseObj extends Entitypublic const STATUS_REJECTED = 'Rejected';public const STATUS_DUPLICATE = 'Duplicate';+ /**+ * @since 10.0.8+ */+ public const string FIELD_ATTACHMENTS = 'attachments';+protected $entityType = 'Case';public function getNumber(): ?intdiff --git a/application/Espo/Modules/Crm/Entities/Document.php b/application/Espo/Modules/Crm/Entities/Document.phpindex 67b5ff8f1a..b9743f0d6e 100644--- a/application/Espo/Modules/Crm/Entities/Document.php+++ b/application/Espo/Modules/Crm/Entities/Document.php@@ -44,6 +44,16 @@ class Document extends Entitypublic const STATUS_ACTIVE = 'Active';public const STATUS_DRAFT = 'Draft';+ /**+ * @since 10.0.8+ */+ public const string FIELD_FILE = 'file';++ /**+ * @since 10.0.8+ */+ public const string FIELD_PUBLISH_DATE = 'publishDate';+public function getName(): ?string{return $this->get(Field::NAME);diff --git a/tests/integration/Espo/Record/LinkTest.php b/tests/integration/Espo/Record/LinkTest.phpindex dbbd72d154..aee6ff5c94 100644--- a/tests/integration/Espo/Record/LinkTest.php+++ b/tests/integration/Espo/Record/LinkTest.php@@ -31,15 +31,18 @@ namespace tests\integration\Espo\Record;use Espo\Core\Exceptions\Forbidden;use Espo\Core\Field\Date;+use Espo\Core\Name\Field;use Espo\Core\ORM\Type\FieldType;use Espo\Core\Record\CreateParams;use Espo\Core\Record\ServiceContainer;use Espo\Core\Record\UpdateParams;use Espo\Core\Utils\Metadata;+use Espo\Entities\Attachment;use Espo\Entities\Email;use Espo\Modules\Crm\Entities\Account;use Espo\Modules\Crm\Entities\CaseObj;use Espo\Modules\Crm\Entities\Contact;+use Espo\Modules\Crm\Entities\Document;use Espo\Modules\Crm\Entities\Lead;use Espo\Modules\Crm\Entities\Opportunity;use Espo\Modules\Crm\Entities\Task;@@ -521,4 +524,130 @@ class LinkTest extends BaseTestCase->isRelatedById($case->getId()));}++ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ public function testAttachmentsOne(): void+ {+ $em = $this->getEntityManager();++ $attachment1 = $em->getRDBRepositoryByClass(Attachment::class)->getNew();+ $em->saveEntity($attachment1);++ $attachment2 = $em->getRDBRepositoryByClass(Attachment::class)->getNew();+ $em->saveEntity($attachment2);++ $service = $this->getContainer()->getByClass(ServiceContainer::class)->getByClass(Document::class);++ $document1 = $service->create((object) [+ Field::NAME => 'Good',+ Document::FIELD_FILE . 'Id' => $attachment1->getId(),+ Document::FIELD_PUBLISH_DATE => Date::createToday()->toString(),+ ])->getEntity();++ //++ $throw = false;++ try {+ $service->create((object) [+ Field::NAME => 'Bad',+ Document::FIELD_FILE . 'Id' => $attachment1->getId(),+ Document::FIELD_PUBLISH_DATE => Date::createToday()->toString(),+ ]);+ } catch (Forbidden) {+ $throw = true;+ }++ $this->assertTrue($throw, 'Should not allow to link already linked attachments.');++ //++ $service->create((object) [+ Field::NAME => 'Good 2',+ Document::FIELD_FILE . 'Id' => $attachment2->getId(),+ Document::FIELD_PUBLISH_DATE => Date::createToday()->toString(),+ ]);++ //++ $throw = false;++ try {+ $service->update($document1->getId(), (object) [+ Document::FIELD_FILE . 'Id' => $attachment2->getId(),+ ]);+ } catch (Forbidden) {+ $throw = true;+ }++ $this->assertTrue($throw, 'Should not allow to link already linked attachments.');+ }++ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ public function testAttachmentsMany(): void+ {+ $em = $this->getEntityManager();++ $attachment1 = $em->getRDBRepositoryByClass(Attachment::class)->getNew();+ $em->saveEntity($attachment1);++ $attachment2 = $em->getRDBRepositoryByClass(Attachment::class)->getNew();+ $em->saveEntity($attachment2);++ $attachment3 = $em->getRDBRepositoryByClass(Attachment::class)->getNew();+ $em->saveEntity($attachment3);++ $service = $this->getContainer()->getByClass(ServiceContainer::class)->getByClass(CaseObj::class);++ $case1 = $service->create((object) [+ Field::NAME => 'Good',+ CaseObj::FIELD_ATTACHMENTS . 'Ids' => [$attachment1->getId()],+ ])->getEntity();++ //++ $throw = false;++ try {+ $service->create((object) [+ Field::NAME => 'Bad',+ CaseObj::FIELD_ATTACHMENTS . 'Ids' => [$attachment1->getId()],+ ]);+ } catch (Forbidden) {+ $throw = true;+ }++ $this->assertTrue($throw, 'Should not allow to link already linked attachments.');++ //++ $case2 = $service->create((object) [+ Field::NAME => 'Good 2',+ CaseObj::FIELD_ATTACHMENTS . 'Ids' => [$attachment3->getId()],+ ])->getEntity();++ //++ $service->update($case1->getId(), (object) [+ CaseObj::FIELD_ATTACHMENTS . 'Ids' => [$attachment1->getId(), $attachment2->getId()],+ ]);++ //++ $throw = false;++ try {+ $service->update($case2->getId(), (object) [+ CaseObj::FIELD_ATTACHMENTS . 'Ids' => [$attachment2->getId()],+ ]);+ } catch (Forbidden) {+ $throw = true;+ }++ $this->assertTrue($throw, 'Should not allow to link already linked attachments.');+ }}