Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotrbrzezina/d92ca837db746e814007269c2b3c43b4 to your computer and use it in GitHub Desktop.
Save piotrbrzezina/d92ca837db746e814007269c2b3c43b4 to your computer and use it in GitHub Desktop.
Extension
<?php
class OwnerExtension implements QueryCollectionExtensionInterface
{
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function applyToCollection(
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
string $operationName = null
) {
$token = $this->tokenStorage->getToken();
if ($token === null) {
return;
}
$user = $token->getUser();
if (!$user instanceof User) {
return;
}
$rootAlias = $queryBuilder->getRootAliases()[0];
switch ($resourceClass) {
case Subject::class:
$queryBuilder->andWhere(sprintf('%s.user = :current_user', $rootAlias));
break;
default:
return;
}
$queryBuilder->setParameter('current_user', $user->getId());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment