Skip to content

Commit

Permalink
Avoid collisions with cache keys at AbstractAdmin::isGranted()
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys authored and greg0ire committed Apr 14, 2020
1 parent df94e19 commit 0365019
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/Admin/AbstractAdmin.php
Expand Up @@ -2417,7 +2417,8 @@ public function getSecurityHandler()

public function isGranted($name, $object = null)
{
$key = md5(json_encode($name).($object ? '/'.spl_object_hash($object) : ''));
$objectRef = $object ? '/'.spl_object_hash($object).'#'.$this->id($object) : '';
$key = md5(json_encode($name).$objectRef);

if (!\array_key_exists($key, $this->cacheIsGranted)) {
$this->cacheIsGranted[$key] = $this->securityHandler->isGranted($this, $name, $object ?: $this);
Expand Down
45 changes: 33 additions & 12 deletions tests/Admin/AdminTest.php
Expand Up @@ -1294,35 +1294,56 @@ public function testDeterminedPerPageValue(): void
public function testIsGranted(): void
{
$admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
$modelManager = $this->createStub(ModelManagerInterface::class);
$modelManager
->method('getNormalizedIdentifier')
->willReturnCallback(static function (?object $entity = null): ?string {
return $entity ? $entity->id : null;
});

$entity = new \stdClass();
$admin->setModelManager($modelManager);

$entity1 = new \stdClass();
$entity1->id = '1';

$securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
$securityHandler
->expects($this->exactly(6))
->method('isGranted')
->willReturnCallback(static function (
AdminInterface $adminIn,
string $attributes,
$object = null
?object $object = null
) use (
$admin,
$entity
$entity1
): bool {
if ($admin === $adminIn && 'FOO' === $attributes) {
if (($object === $admin) || ($object === $entity)) {
return true;
}
}

return false;
return $admin === $adminIn && 'FOO' === $attributes &&
($object === $admin || $object === $entity1);
});

$admin->setSecurityHandler($securityHandler);

$this->assertTrue($admin->isGranted('FOO'));
$this->assertTrue($admin->isGranted('FOO', $entity));
$this->assertTrue($admin->isGranted('FOO'));
$this->assertTrue($admin->isGranted('FOO', $entity1));
$this->assertTrue($admin->isGranted('FOO', $entity1));
$this->assertFalse($admin->isGranted('BAR'));
$this->assertFalse($admin->isGranted('BAR'));
$this->assertFalse($admin->isGranted('BAR', $entity));
$this->assertFalse($admin->isGranted('BAR', $entity1));
$this->assertFalse($admin->isGranted('BAR', $entity1));

$entity2 = new \stdClass();
$entity2->id = '2';

$this->assertFalse($admin->isGranted('BAR', $entity2));
$this->assertFalse($admin->isGranted('BAR', $entity2));

$entity3 = new \stdClass();
$entity3->id = '3';

$this->assertFalse($admin->isGranted('BAR', $entity3));
$this->assertFalse($admin->isGranted('BAR', $entity3));
}

public function testSupportsPreviewMode(): void
Expand Down

0 comments on commit 0365019

Please sign in to comment.