|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components 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\ApiComponentsBundle\Doctrine\Extension\ORM; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; |
|
17
|
|
|
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
|
18
|
|
|
use ApiPlatform\Metadata\Operation; |
|
19
|
|
|
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface; |
|
20
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
21
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\Route; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Daniel West <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class RouteExtension implements QueryCollectionExtensionInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private ?array $config; |
|
29
|
|
|
private ResourceAccessCheckerInterface $resourceAccessChecker; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(?array $config, ResourceAccessCheckerInterface $resourceAccessChecker) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->config = $config; |
|
34
|
|
|
$this->resourceAccessChecker = $resourceAccessChecker; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
|
38
|
|
|
{ |
|
39
|
|
|
if (!$this->config || Route::class !== $resourceClass) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
$alias = $queryBuilder->getRootAliases()[0]; |
|
43
|
|
|
foreach ($this->config as $index => $routeConfig) { |
|
44
|
|
|
if ($this->resourceAccessChecker->isGranted($resourceClass, $routeConfig['security'])) { |
|
45
|
|
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
$param = 'path_' . $index; |
|
48
|
|
|
$queryBuilder |
|
49
|
|
|
->andWhere( |
|
50
|
|
|
$queryBuilder->expr()->notLike("$alias.path", ':' . $param) |
|
51
|
|
|
); |
|
52
|
|
|
$queryBuilder->setParameter($param, str_replace('*', '%', $routeConfig['route'])); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|