Created
March 17, 2022 15:45
-
-
Save javiereguiluz/ff065640bf00976ffe685d1572abb2bb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx