Created
June 27, 2019 17:51
-
-
Save piotrbrzezina/d92ca837db746e814007269c2b3c43b4 to your computer and use it in GitHub Desktop.
Extension
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
<?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