Skip to content

Instantly share code, notes, and snippets.

@javiereguiluz
Created March 17, 2022 15:45
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 javiereguiluz/ff065640bf00976ffe685d1572abb2bb to your computer and use it in GitHub Desktop.
Save javiereguiluz/ff065640bf00976ffe685d1572abb2bb to your computer and use it in GitHub Desktop.
<?php
class BlogPostCrudController extends AbstractCrudController
{
// ...
// Thanks to this method, admin users can see all blog posts but
// the rest of users can only see their blog posts
public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
$isAdminUser = $this->isGranted('ROLE_ADMIN');
$defaultQueryBuilder = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
if ($isAdminUser) {
return $defaultQueryBuilder;
}
return $defaultQueryBuilder->andWhere('entity.user = :user')->setParameter('user', $this->getUser());
}
}
@antoine1003
Copy link

thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment