Skip to content

Commit

Permalink
Merge branch 'hotfix/v2.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Dec 13, 2023
2 parents 06bf63d + b21ad0b commit ecd5863
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [v2.2.1](https://github.com/roadiz/core-bundle-dev-app/compare/v2.2.0...v2.2.1) (2023-12-13)


### Bug Fixes

* **Api:** Added `AttributeValueQueryExtension` and `NodesTagsQueryExtension` to restrict tags and attributes linked to any published node. ([cd2a017](https://github.com/roadiz/core-bundle-dev-app/commit/cd2a017e840ec5fed0efd5c0825bfcb3d09f6275))

## [v2.2.0](https://github.com/roadiz/core-bundle-dev-app/compare/v2.1.51...v2.2.0) (2023-12-12)


Expand Down
17 changes: 16 additions & 1 deletion lib/RoadizCoreBundle/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
parameters:
roadiz_core.cms_version: '2.2.0'
roadiz_core.cms_version: '2.2.1'
roadiz_core.cms_version_prefix: 'main'
env(APP_NAMESPACE): "roadiz"
env(APP_VERSION): "0.1.0"
Expand Down Expand Up @@ -114,6 +114,21 @@ services:
# Extension must be called after all filtering BUT before default pagination extension
tags: [ { name: 'api_platform.doctrine.orm.query_extension.collection', priority: -40 } ]

#
# These API doctrine extension must be called last before pagination
# to perform on existing JOIN with node entities (found after filtering)
#
RZ\Roadiz\CoreBundle\Api\Extension\AttributeValueQueryExtension:
tags: [
{ name: 'api_platform.doctrine.orm.query_extension.collection', priority: -40 },
{ name: 'api_platform.doctrine.orm.query_extension.item', priority: -40 },
]
RZ\Roadiz\CoreBundle\Api\Extension\NodesTagsQueryExtension:
tags: [
{ name: 'api_platform.doctrine.orm.query_extension.collection', priority: -40 },
{ name: 'api_platform.doctrine.orm.query_extension.item', priority: -40 },
]

RZ\Roadiz\CoreBundle\Bag\:
resource: '../src/Bag/'
autowire: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Api\Extension;

use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryBuilderHelper;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\QueryBuilder;
use RZ\Roadiz\CoreBundle\Entity\AttributeValue;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;

final class AttributeValueQueryExtension implements QueryItemExtensionInterface, QueryCollectionExtensionInterface
{
private PreviewResolverInterface $previewResolver;

public function __construct(
PreviewResolverInterface $previewResolver
) {
$this->previewResolver = $previewResolver;
}

public function applyToItem(
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
array $identifiers,
?Operation $operation = null,
array $context = []
): void {
$this->apply($queryBuilder, $resourceClass);
}

public function applyToCollection(
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
?Operation $operation = null,
array $context = []
): void {
$this->apply($queryBuilder, $resourceClass);
}

private function apply(
QueryBuilder $queryBuilder,
string $resourceClass
): void {
if (
$resourceClass !== AttributeValue::class
) {
return;
}

$parts = $queryBuilder->getDQLPart('join');
$rootAlias = $queryBuilder->getRootAliases()[0];
if (!\is_array($parts) || !isset($parts[$rootAlias])) {
return;
}

$existingNodeJoin = QueryBuilderHelper::getExistingJoin($queryBuilder, 'o', 'node');
if (null === $existingNodeJoin || !$existingNodeJoin->getAlias()) {
return;
}

if ($this->previewResolver->isPreview()) {
$queryBuilder
->andWhere($queryBuilder->expr()->lte($existingNodeJoin->getAlias() . '.status', ':status'))
->setParameter(':status', Node::PUBLISHED);
return;
}

$queryBuilder
->andWhere($queryBuilder->expr()->eq($existingNodeJoin->getAlias() . '.status', ':status'))
->setParameter(':status', Node::PUBLISHED);
return;
}
}
89 changes: 89 additions & 0 deletions lib/RoadizCoreBundle/src/Api/Extension/NodesTagsQueryExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Api\Extension;

use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryBuilderHelper;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\QueryBuilder;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;

final class NodesTagsQueryExtension implements QueryItemExtensionInterface, QueryCollectionExtensionInterface
{
private PreviewResolverInterface $previewResolver;

public function __construct(
PreviewResolverInterface $previewResolver
) {
$this->previewResolver = $previewResolver;
}

public function applyToItem(
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
array $identifiers,
?Operation $operation = null,
array $context = []
): void {
$this->apply($queryBuilder, $resourceClass);
}

public function applyToCollection(
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
?Operation $operation = null,
array $context = []
): void {
$this->apply($queryBuilder, $resourceClass);
}

private function apply(
QueryBuilder $queryBuilder,
string $resourceClass
): void {
if (
$resourceClass !== Tag::class
) {
return;
}

$parts = $queryBuilder->getDQLPart('join');
$rootAlias = $queryBuilder->getRootAliases()[0];
if (!\is_array($parts) || !isset($parts[$rootAlias])) {
return;
}

$existingJoin = QueryBuilderHelper::getExistingJoin($queryBuilder, 'o', 'nodesTags');
if (null === $existingJoin || !$existingJoin->getAlias()) {
return;
}
$existingNodeJoin = QueryBuilderHelper::getExistingJoin(
$queryBuilder,
$existingJoin->getAlias(),
'node'
);
if (null === $existingNodeJoin || !$existingNodeJoin->getAlias()) {
return;
}

if ($this->previewResolver->isPreview()) {
$queryBuilder
->andWhere($queryBuilder->expr()->lte($existingNodeJoin->getAlias() . '.status', ':status'))
->setParameter(':status', Node::PUBLISHED);
return;
}

$queryBuilder
->andWhere($queryBuilder->expr()->eq($existingNodeJoin->getAlias() . '.status', ':status'))
->setParameter(':status', Node::PUBLISHED);
return;
}
}

0 comments on commit ecd5863

Please sign in to comment.