1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Doctrine\Extension\ORM; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\ContextAwareQueryCollectionExtensionInterface; |
17
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface; |
18
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
19
|
|
|
use Doctrine\ORM\EntityRepository; |
20
|
|
|
use Doctrine\ORM\QueryBuilder; |
21
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
22
|
|
|
use Silverback\ApiComponentBundle\Annotation\Publishable; |
23
|
|
|
use Silverback\ApiComponentBundle\Helper\PublishableHelper; |
24
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Vincent Chalamon <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class PublishableExtension implements QueryItemExtensionInterface, ContextAwareQueryCollectionExtensionInterface |
30
|
|
|
{ |
31
|
|
|
private PublishableHelper $publishableHelper; |
32
|
|
|
private RequestStack $requestStack; |
33
|
|
|
private ManagerRegistry $registry; |
34
|
|
|
private ?Publishable $configuration = null; |
35
|
|
|
|
36
|
|
|
public function __construct(PublishableHelper $publishableHelper, RequestStack $requestStack, ManagerRegistry $registry) |
37
|
|
|
{ |
38
|
|
|
$this->publishableHelper = $publishableHelper; |
39
|
|
|
$this->requestStack = $requestStack; |
40
|
|
|
$this->registry = $registry; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void |
44
|
|
|
{ |
45
|
|
|
$configuration = $this->getConfiguration($resourceClass); |
46
|
|
|
|
47
|
|
|
if (!$configuration || !$this->requestStack->getCurrentRequest()) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!$this->isDraftRequest($context)) { |
52
|
|
|
// User has no access to draft object |
53
|
|
|
$this->updateQueryBuilderForUnauthorizedUsers($queryBuilder, $configuration); |
54
|
|
|
|
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$alias = $queryBuilder->getRootAliases()[0]; |
59
|
|
|
|
60
|
|
|
// (o.publishedResource = :id OR o.id = :id) ORDER BY o.publishedResource IS NULL LIMIT 1 |
61
|
|
|
$criteriaReset = false; |
62
|
|
|
foreach ($identifiers as $identifier => $value) { |
63
|
|
|
$predicates = $queryBuilder->expr()->orX( |
64
|
|
|
$queryBuilder->expr()->eq("$alias.$configuration->associationName", ":id_$identifier"), |
65
|
|
|
$queryBuilder->expr()->eq("$alias.$identifier", ":id_$identifier"), |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
// Reset queryBuilder to prevent an invalid DQL |
69
|
|
|
if (!$criteriaReset) { |
70
|
|
|
$queryBuilder->where($predicates); |
71
|
|
|
$criteriaReset = true; |
72
|
|
|
} else { |
73
|
|
|
$queryBuilder->andWhere($predicates); |
74
|
|
|
} |
75
|
|
|
$queryBuilder->setParameter("id_$identifier", $value); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$queryBuilder->addSelect("CASE WHEN $alias.$configuration->associationName IS NULL THEN 1 ELSE 0 END AS HIDDEN assocNameSort"); |
79
|
|
|
$queryBuilder->orderBy('assocNameSort', 'ASC'); |
80
|
|
|
$queryBuilder->setMaxResults(1); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = []): void |
84
|
|
|
{ |
85
|
|
|
if (!$configuration = $this->getConfiguration($resourceClass)) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!$this->isDraftRequest($context)) { |
90
|
|
|
// User has no access to draft object |
91
|
|
|
$this->updateQueryBuilderForUnauthorizedUsers($queryBuilder, $configuration); |
92
|
|
|
|
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$alias = $queryBuilder->getRootAliases()[0]; |
97
|
|
|
$identifiers = $this->registry->getManagerForClass($resourceClass)->getClassMetadata($resourceClass)->getIdentifier(); |
98
|
|
|
$dql = $this->getDQL($configuration, $resourceClass); |
99
|
|
|
|
100
|
|
|
// o.id NOT IN (SELECT p.publishedResource FROM {table} t WHERE t.publishedResource IS NOT NULL) |
101
|
|
|
foreach ($identifiers as $identifier) { |
102
|
|
|
$queryBuilder->andWhere($queryBuilder->expr()->notIn("$alias.$identifier", $dql)); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function getDQL(Publishable $configuration, string $resourceClass): string |
107
|
|
|
{ |
108
|
|
|
/** @var EntityRepository $repository */ |
109
|
|
|
$repository = $this->registry->getManagerForClass($resourceClass)->getRepository($resourceClass); |
110
|
|
|
$queryBuilder = $repository->createQueryBuilder('o2'); |
111
|
|
|
|
112
|
|
|
return $queryBuilder |
113
|
|
|
->select("IDENTITY(o2.$configuration->associationName)") |
114
|
|
|
->where($queryBuilder->expr()->isNotNull("o2.$configuration->associationName")) |
115
|
|
|
->getDQL(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function isDraftRequest(array $context): bool |
119
|
|
|
{ |
120
|
|
|
return $this->publishableHelper->isGranted() && false === ($context['filters']['published'] ?? false); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function updateQueryBuilderForUnauthorizedUsers(QueryBuilder $queryBuilder, Publishable $configuration): void |
124
|
|
|
{ |
125
|
|
|
$alias = $queryBuilder->getRootAliases()[0]; |
126
|
|
|
$queryBuilder |
127
|
|
|
->andWhere("$alias.$configuration->fieldName IS NOT NULL") |
128
|
|
|
->andWhere("$alias.$configuration->fieldName <= :currentTime") |
129
|
|
|
->setParameter('currentTime', new \DateTimeImmutable()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function getConfiguration(string $resourceClass): ?Publishable |
133
|
|
|
{ |
134
|
|
|
if (!$this->configuration && ($this->publishableHelper->isConfigured($resourceClass))) { |
135
|
|
|
$this->configuration = $this->publishableHelper->getConfiguration($resourceClass); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->configuration; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|