-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Api): Added
AttributeValueQueryExtension
and `NodesTagsQueryExt…
…ension` to restrict tags and attributes linked to any published node.
- Loading branch information
1 parent
06bf63d
commit cd2a017
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
lib/RoadizCoreBundle/src/Api/Extension/AttributeValueQueryExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
89
lib/RoadizCoreBundle/src/Api/Extension/NodesTagsQueryExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |