-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathBookmarkQueryCollectionExtension.php
38 lines (32 loc) · 1.16 KB
/
BookmarkQueryCollectionExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
declare(strict_types=1);
namespace App\Doctrine\Orm\Extension;
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use App\Entity\Bookmark;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\SecurityBundle\Security;
/**
* Restrict Bookmark collection to current user.
*/
final readonly class BookmarkQueryCollectionExtension implements QueryCollectionExtensionInterface
{
public function __construct(private Security $security)
{
}
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
if (
Bookmark::class !== $resourceClass
|| '_api_/bookmarks{._format}_get_collection' !== $operation->getName()
|| !$user = $this->security->getUser()
) {
return;
}
$queryBuilder
->andWhere(\sprintf('%s.user = :user', $queryBuilder->getRootAliases()[0]))
->setParameter('user', $user)
;
}
}