Skip to content

Commit

Permalink
[Bug][Data loss]: if passing an invalid asset.id, fullpath to object …
Browse files Browse the repository at this point in the history
…mutation GraphQL query, the assets are silently discarded (#825)

* Fix: throw error message

* Fix: throw error message

* Apply suggestion by covering all the relational types and improve exception message

* Update ManyToOneRelation.php

* Apply php-cs-fixer changes

---------

Co-authored-by: jiajia <[email protected]>
Co-authored-by: kingjia90 <[email protected]>
  • Loading branch information
3 people authored Feb 21, 2024
1 parent 0263330 commit 2d8d815
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Data\ObjectMetadata;
use Pimcore\Model\DataObject\Fieldcollection\Data\AbstractData;
use Pimcore\Model\Exception\NotFoundException;

class AdvancedManyToManyObjectRelation extends Base
{
Expand Down Expand Up @@ -58,6 +59,13 @@ public function process($object, $newValue, $args, $context, ResolveInfo $info)
$item->setData($data);
}
$result[] = $item;
} else {
throw new NotFoundException(
sprintf('Element with id %s or fullpath %s not found',
$newValueItemValue['id'],
$newValueItemValue['fullpath']
)
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Data\ElementMetadata;
use Pimcore\Model\DataObject\Fieldcollection\Data\AbstractData;
use Pimcore\Model\Exception\NotFoundException;

class AdvancedManyToManyRelation extends Base
{
Expand Down Expand Up @@ -57,6 +58,13 @@ public function process($object, $newValue, $args, $context, ResolveInfo $info)
$item->setData($data);
}
$result[] = $item;
} else {
throw new NotFoundException(
sprintf('Element with id %s or fullpath %s not found',
$newValueItemValue['id'],
$newValueItemValue['fullpath']
)
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementIdentificationTrait;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Fieldcollection\Data\AbstractData;
use Pimcore\Model\Exception\NotFoundException;

class ManyToManyObjectRelation extends Base
{
Expand Down Expand Up @@ -50,6 +51,13 @@ public function process($object, $newValue, $args, $context, ResolveInfo $info)

if ($element) {
$result[] = $element;
} else {
throw new NotFoundException(
sprintf('Element with id %s or fullpath %s not found',
$newValueItemValue['id'],
$newValueItemValue['fullpath']
)
);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/GraphQL/DataObjectInputProcessor/ManyToManyRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementIdentificationTrait;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Fieldcollection\Data\AbstractData;
use Pimcore\Model\Exception\NotFoundException;

class ManyToManyRelation extends Base
{
Expand All @@ -45,6 +46,13 @@ public function process($object, $newValue, $args, $context, ResolveInfo $info)

if ($element) {
$result[] = $element;
} else {
throw new NotFoundException(
sprintf('Element with id %s or fullpath %s not found',
$newValueItemValue['id'],
$newValueItemValue['fullpath']
)
);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/GraphQL/DataObjectInputProcessor/ManyToOneRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementIdentificationTrait;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Fieldcollection\Data\AbstractData;
use Pimcore\Model\Exception\NotFoundException;

class ManyToOneRelation extends Base
{
Expand All @@ -40,8 +41,18 @@ public function process($object, $newValue, $args, $context, ResolveInfo $info):
$me = $this;
Service::setValue($object, $attribute, function ($container, $setter) use ($newValue) {
$element = null;

if (is_array($newValue)) {
$element = $this->getElementByTypeAndIdOrPath($newValue);

if (!$element) {
throw new NotFoundException(
sprintf('Element with id %s or fullpath %s not found',
$newValue['id'],
$newValue['fullpath']
)
);
}
}

return $container->$setter($element);
Expand Down

0 comments on commit 2d8d815

Please sign in to comment.