diff --git a/config/mago.toml b/config/mago.toml index b3d12e3..4854a12 100644 --- a/config/mago.toml +++ b/config/mago.toml @@ -53,4 +53,7 @@ ignore = [ { code = "possibly-invalid-argument", in = ["tests/Test/TestApplication/Controller/ConfirmationModalCrudController.php"] }, # 2 errors before EasyAdmin 5.10 # AdminRouteGenerator::usesPrettyUrls() only exists in EasyAdmin 4, EasyAdmin 5 always uses pretty URLs { code = "non-existent-method", in = ["src/Field/Configurator/EntityConfigurator.php"] }, # 1 error on EasyAdmin 5 + # CrudPermissionCheckerInterface only exists since EasyAdmin 5.9, EntityConfigurator links every target before + { code = "non-existent-class-like", in = ["src/Field/Configurator/EntityConfigurator.php"] }, # 1 error before EasyAdmin 5.9 + { code = "invalid-method-access", in = ["src/Field/Configurator/EntityConfigurator.php"] }, # 1 error before EasyAdmin 5.9 ] diff --git a/config/phpstan.neon b/config/phpstan.neon index 7d8ca06..070123d 100644 --- a/config/phpstan.neon +++ b/config/phpstan.neon @@ -33,6 +33,12 @@ parameters: identifier: method.notFound path: ../src/Field/Configurator/EntityConfigurator.php count: 1 + # CrudPermissionCheckerInterface only exists since EasyAdmin 5.9, EntityConfigurator links every target before + - + message: '#^.+ EasyCorp\\Bundle\\EasyAdminBundle\\Contracts\\Security\\CrudPermissionCheckerInterface( as its type)?\.$#' + identifier: class.notFound + path: ../src/Field/Configurator/EntityConfigurator.php + count: 3 # BaseCrudDtoController::handleFormRequest() is a byte-identical copy of EasyAdmin's, so it keeps EasyAdmin's # untyped form and nested access to the posted data - diff --git a/src/Field/Configurator/EntityConfigurator.php b/src/Field/Configurator/EntityConfigurator.php index 44878dd..459eab3 100644 --- a/src/Field/Configurator/EntityConfigurator.php +++ b/src/Field/Configurator/EntityConfigurator.php @@ -13,6 +13,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface; use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface; use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface; +use EasyCorp\Bundle\EasyAdminBundle\Contracts\Security\CrudPermissionCheckerInterface; use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto; use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; @@ -50,6 +51,8 @@ public function __construct( private PropertyAccessorInterface $propertyAccessor, private EntityManagerInterface $entityManager, private AdminRouteGenerator $adminRouteGenerator, + // since EasyAdmin 5.9, before it every link is generated + private CrudPermissionCheckerInterface|null $crudPermissionChecker = null, ) { } @@ -204,6 +207,7 @@ private function configureToOneAssociation(FieldDto $field, EntityMetadata $enti $this->generateLinkToAssociatedEntity( $entityMetadata, $targetEntityDto, + $context, ), ); } @@ -241,6 +245,7 @@ function (object $entity) use ($entityMetadata, $field, $targetSingleIdentifierF $entityMetadata->targetEntityFqcn(), $this->propertyAccessor->getValue($entity, $targetSingleIdentifierFieldName), ), + $context, ); } @@ -257,13 +262,19 @@ function (object $entity) use ($entityMetadata, $field, $targetSingleIdentifierF $field->setFormattedValue($formattedValue); } - private function generateLinkToAssociatedEntity(EntityMetadata $entityMetadata, EntityDto $entityDto): string|null + /** @param AdminContext $context */ + private function generateLinkToAssociatedEntity(EntityMetadata $entityMetadata, EntityDto $entityDto, AdminContext $context): string|null { $entityId = $entityDto->getPrimaryKeyValue(); if ($entityId === null) { return null; } + // no link to a detail page the user may not open, like EasyAdmin's AssociationConfigurator + if ($this->crudPermissionChecker?->isGranted($context, $entityMetadata->targetCrudControllerFqcn(), Action::DETAIL, $entityDto) === false) { + return null; + } + return $this->adminUrlGenerator ->unsetAll() ->setDashboard($entityMetadata->targetDashboardControllerFqcn()) diff --git a/tests/Field/Configurator/EntityConfiguratorTest.php b/tests/Field/Configurator/EntityConfiguratorTest.php index 62c12d5..3020b0b 100644 --- a/tests/Field/Configurator/EntityConfiguratorTest.php +++ b/tests/Field/Configurator/EntityConfiguratorTest.php @@ -6,6 +6,7 @@ use Doctrine\ORM\Tools\SchemaTool; use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface; +use EasyCorp\Bundle\EasyAdminBundle\EasyAdminBundle; use Override; use PHPUnit\Framework\Attributes\CoversClass; use Protung\EasyAdminPlusBundle\Field\Configurator\EntityConfigurator; @@ -15,6 +16,8 @@ use Protung\EasyAdminPlusBundle\Tests\Test\TestApplication\Entity\Report; use Symfony\Component\DomCrawler\Crawler; +use function version_compare; + /** * The links of entity fields to the detail page of the linked entity. * @@ -89,6 +92,43 @@ public function testNoLinkWhenNoDashboardHasARouteForTheTarget(): void self::assertCount(0, $field->filter('a')); } + public function testNoLinkWhenTheUserMayNotOpenTheDetailPageOfTheTarget(): void + { + self::skipBeforeEasyAdmin59(); + + $this->assertRequestGet(); + + $field = $this->renderedField('Detail forbidden report'); + self::assertSame('1', $this->extractFieldValue($field)); + self::assertCount(0, $field->filter('a')); + } + + public function testListLinksEachTargetToItsDetailPage(): void + { + $this->assertRequestGet(); + + $field = $this->renderedField('Reports'); + self::assertSame(['http://localhost/admin/report/1'], $field->filter('li a')->extract(['href'])); + } + + public function testListHasNoLinksWhenTheUserMayNotOpenTheDetailPageOfTheTargets(): void + { + self::skipBeforeEasyAdmin59(); + + $this->assertRequestGet(); + + $field = $this->renderedField('Detail forbidden reports'); + self::assertSame(['1'], $field->filter('li')->each(static fn (Crawler $item): string => $item->text(normalizeWhitespace: true))); + self::assertCount(0, $field->filter('a')); + } + + private static function skipBeforeEasyAdmin59(): void + { + if (version_compare(EasyAdminBundle::VERSION, '5.9.0', '<')) { + self::markTestSkipped('EasyAdmin can only tell whether the user may open the detail page of a target since 5.9.'); + } + } + private function renderedField(string $label): Crawler { $field = $this->getClient()->getCrawler() diff --git a/tests/Test/TestApplication/Controller/DetailForbiddenReportCrudController.php b/tests/Test/TestApplication/Controller/DetailForbiddenReportCrudController.php new file mode 100644 index 0000000..222cc8e --- /dev/null +++ b/tests/Test/TestApplication/Controller/DetailForbiddenReportCrudController.php @@ -0,0 +1,31 @@ + + */ +final class DetailForbiddenReportCrudController extends AbstractCrudController +{ + #[Override] + public static function getEntityFqcn(): string + { + return Report::class; + } + + #[Override] + public function configureActions(Actions $actions): Actions + { + return $actions->setPermission(Action::DETAIL, 'ROLE_SUPER_ADMIN'); + } +} diff --git a/tests/Test/TestApplication/Controller/ReportLinkCrudController.php b/tests/Test/TestApplication/Controller/ReportLinkCrudController.php index 3c2d4fb..585919b 100644 --- a/tests/Test/TestApplication/Controller/ReportLinkCrudController.php +++ b/tests/Test/TestApplication/Controller/ReportLinkCrudController.php @@ -10,8 +10,8 @@ use Protung\EasyAdminPlusBundle\Tests\Test\TestApplication\Entity\Report; /** - * Entity fields that link each report to itself, through a CRUD controller that only the main dashboard has routes for - * and through one that no dashboard has routes for. + * Entity fields that link each report to itself, through a CRUD controller that only the main dashboard has routes for, + * through one that no dashboard has routes for and through one whose detail page the user may not open. * * @template-extends AbstractCrudController */ @@ -35,5 +35,22 @@ public function configureFields(string $pageName): iterable yield EntityField::new('id', 'Unrouted report') ->setCrudController(UnroutedReportCrudController::class) ->setEntityDisplayField('id'); + + yield EntityField::new('id', 'Detail forbidden report') + ->setCrudController(DetailForbiddenReportCrudController::class) + ->setEntityDisplayField('id'); + + // as a to-many association the reports whose ID is the ID of this one, so the report itself + yield EntityField::new('id', 'Reports') + ->setCrudController(ReportCrudController::class) + ->setAssociationToMany() + ->renderAsList() + ->setEntityDisplayField('id'); + + yield EntityField::new('id', 'Detail forbidden reports') + ->setCrudController(DetailForbiddenReportCrudController::class) + ->setAssociationToMany() + ->renderAsList() + ->setEntityDisplayField('id'); } }