Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/mago.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
6 changes: 6 additions & 0 deletions config/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
-
Expand Down
13 changes: 12 additions & 1 deletion src/Field/Configurator/EntityConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
) {
}

Expand Down Expand Up @@ -204,6 +207,7 @@ private function configureToOneAssociation(FieldDto $field, EntityMetadata $enti
$this->generateLinkToAssociatedEntity(
$entityMetadata,
$targetEntityDto,
$context,
),
);
}
Expand Down Expand Up @@ -241,6 +245,7 @@ function (object $entity) use ($entityMetadata, $field, $targetSingleIdentifierF
$entityMetadata->targetEntityFqcn(),
$this->propertyAccessor->getValue($entity, $targetSingleIdentifierFieldName),
),
$context,
);
}

Expand All @@ -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<object> $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())
Expand Down
40 changes: 40 additions & 0 deletions tests/Field/Configurator/EntityConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Protung\EasyAdminPlusBundle\Tests\Test\TestApplication\Controller;

use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use Override;
use Protung\EasyAdminPlusBundle\Tests\Test\TestApplication\Entity\Report;

/**
* A CRUD controller whose detail page needs a role the test user doesn't have.
*
* @template-extends AbstractCrudController<Report>
*/
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');
}
}
21 changes: 19 additions & 2 deletions tests/Test/TestApplication/Controller/ReportLinkCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Report>
*/
Expand All @@ -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');
}
}
Loading