Skip to content

Commit

Permalink
Update GDI to 2.x and add user to asset searches
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Oct 4, 2024
1 parent 352d086 commit 90a1870
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"php": "~8.2.0 || ~8.3.0",
"nesbot/carbon": "^2.65",
"pimcore/static-resolver-bundle": "1.x-dev",
"pimcore/generic-data-index-bundle": "1.x-dev",
"pimcore/generic-data-index-bundle": "2.x-dev",
"pimcore/pimcore": "^11.x-dev",
"zircote/swagger-php": "^4.8",
"ext-zip": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Exception;
use Pimcore\Bundle\StaticResolverBundle\Models\User\UserResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\CsvCollectionMessage;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Asset;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Util\Constant\Csv;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementServiceInterface;
Expand All @@ -28,8 +29,6 @@
use Pimcore\Bundle\StudioBackendBundle\Grid\Service\GridServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\PublishServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use Pimcore\Model\Asset;
use Pimcore\Model\Element\ElementDescriptor;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
Expand Down Expand Up @@ -61,7 +60,9 @@ public function __invoke(CsvCollectionMessage $message): void
if (!$this->shouldBeExecuted($jobRun)) {
return;
}

$user = $this->userResolver->getById($jobRun->getOwnerId());

if ($user === null) {
$this->abort($this->getAbortData(
Config::USER_NOT_FOUND_MESSAGE->value,
Expand All @@ -72,14 +73,10 @@ public function __invoke(CsvCollectionMessage $message): void
}

$jobAsset = $this->extractConfigFieldFromJobStepConfig($message, Csv::ASSET_TO_EXPORT->value);
// TODO: Replace getElementById with getAsset with permission check.
// We do not want to load the asset form the database. Permission check should be done in the index.
$asset = $this->getElementById(
new ElementDescriptor($jobAsset['type'], $jobAsset['id']),
$user,
$this->elementService
);
if (!$asset instanceof Asset || $asset->getType() === ElementTypes::TYPE_FOLDER) {

$asset = $this->assetService->getAsset($jobAsset['id'], $user);

if ($asset->getType() === ElementTypes::TYPE_FOLDER) {
$this->abort($this->getAbortData(
Config::ELEMENT_FOLDER_COLLECTION_NOT_SUPPORTED->value,
[
Expand All @@ -95,13 +92,11 @@ public function __invoke(CsvCollectionMessage $message): void
true
);

$indexAsset = $this->assetService->getAsset($asset->getId());

try {
$assetData = [
$asset->getId() => $this->gridService->getGridValuesForElement(
$columnCollection,
$indexAsset,
$asset,
ElementTypes::TYPE_ASSET
),
];
Expand Down
4 changes: 2 additions & 2 deletions src/Asset/Service/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public function getAssets(ElementParameters $parameters): Collection
/**
* @throws SearchException|NotFoundException
*/
public function getAsset(int $id): Asset|Archive|Audio|Document|AssetFolder|Image|Text|Unknown|Video
public function getAsset(int $id, ?UserInterface $user = null): Asset|Archive|Audio|Document|AssetFolder|Image|Text|Unknown|Video
{
$asset = $this->assetSearchService->getAssetById($id);
$asset = $this->assetSearchService->getAssetById($id, $user);

$this->eventDispatcher->dispatch(
new AssetEvent($asset),
Expand Down
2 changes: 1 addition & 1 deletion src/Asset/Service/AssetServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getAssets(ElementParameters $parameters): Collection;
/**
* @throws SearchException|NotFoundException
*/
public function getAsset(int $id): Asset|Archive|Audio|Document|AssetFolder|Image|Text|Unknown|Video;
public function getAsset(int $id, ?UserInterface $user = null): Asset|Archive|Audio|Document|AssetFolder|Image|Text|Unknown|Video;

/**
* @throws SearchException|NotFoundException
Expand Down
5 changes: 3 additions & 2 deletions src/DataIndex/Adapter/AssetSearchAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\SearchException;
use Pimcore\Model\UserInterface;
use function sprintf;

final readonly class AssetSearchAdapter implements AssetSearchAdapterInterface
Expand Down Expand Up @@ -71,10 +72,10 @@ public function searchAssets(QueryInterface $assetQuery): AssetSearchResult
/**
* @throws SearchException|NotFoundException
*/
public function getAssetById(int $id): Asset
public function getAssetById(int $id, ?UserInterface $user = null): Asset
{
try {
$asset = $this->searchService->byId($id);
$asset = $this->searchService->byId($id, $user);

Check failure on line 78 in src/DataIndex/Adapter/AssetSearchAdapter.php

View workflow job for this annotation

GitHub Actions / static-analysis (8.2, lowest, false) / Static analysis with phpstan

Parameter #2 $user of method Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface::byId() expects Pimcore\Model\User|null, Pimcore\Model\UserInterface|null given.

Check failure on line 78 in src/DataIndex/Adapter/AssetSearchAdapter.php

View workflow job for this annotation

GitHub Actions / static-analysis (8.3, highest, 11.x-dev as 11.99.9, true) / Static analysis with phpstan

Parameter #2 $user of method Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface::byId() expects Pimcore\Model\User|null, Pimcore\Model\UserInterface|null given.

Check failure on line 78 in src/DataIndex/Adapter/AssetSearchAdapter.php

View workflow job for this annotation

GitHub Actions / static-analysis (8.3, highest, false) / Static analysis with phpstan

Parameter #2 $user of method Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface::byId() expects Pimcore\Model\User|null, Pimcore\Model\UserInterface|null given.
} catch (AssetSearchException) {
throw new SearchException(sprintf('Asset with id %s', $id));
}
Expand Down
3 changes: 2 additions & 1 deletion src/DataIndex/Adapter/AssetSearchAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\SearchException;
use Pimcore\Model\UserInterface;

interface AssetSearchAdapterInterface
{
Expand All @@ -34,7 +35,7 @@ public function searchAssets(QueryInterface $assetQuery): AssetSearchResult;
/**
* @throws SearchException|NotFoundException
*/
public function getAssetById(int $id): Asset;
public function getAssetById(int $id, ?UserInterface $user = null): Asset;

/**
* @throws SearchException
Expand Down
8 changes: 6 additions & 2 deletions src/DataIndex/AssetSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\SearchException;
use Pimcore\Model\UserInterface;
use function count;

final readonly class AssetSearchService implements AssetSearchServiceInterface
Expand All @@ -53,9 +54,12 @@ public function searchAssets(QueryInterface $assetQuery): AssetSearchResult
/**
* @throws SearchException|NotFoundException
*/
public function getAssetById(int $id): Asset|Archive|Audio|Document|AssetFolder|Image|Text|Unknown|Video
public function getAssetById(
int $id,
?UserInterface $user = null
): Asset|Archive|Audio|Document|AssetFolder|Image|Text|Unknown|Video
{
return $this->assetSearchAdapter->getAssetById($id);
return $this->assetSearchAdapter->getAssetById($id, $user);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/DataIndex/AssetSearchServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\SearchException;
use Pimcore\Model\UserInterface;

interface AssetSearchServiceInterface
{
Expand All @@ -41,7 +42,10 @@ public function searchAssets(QueryInterface $assetQuery): AssetSearchResult;
/**
* @throws SearchException|NotFoundException
*/
public function getAssetById(int $id): Asset|Archive|Audio|Document|AssetFolder|Image|Text|Unknown|Video;
public function getAssetById(
int $id,
?UserInterface $user = null
): Asset|Archive|Audio|Document|AssetFolder|Image|Text|Unknown|Video;

/**
* @throws SearchException
Expand Down

0 comments on commit 90a1870

Please sign in to comment.