-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SonataExtraBundle] delete batch object check base on patch
- Loading branch information
Showing
15 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
26 changes: 26 additions & 0 deletions
26
packages/sonata-extra-bundle/EventListener/PreObjectDeleteBatchEventEventListener.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\SonataExtraBundle\EventListener; | ||
|
||
use Sonata\AdminBundle\Admin\Pool; | ||
use Sonata\DoctrineORMAdminBundle\Event\PreObjectDeleteBatchEvent; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
|
||
class PreObjectDeleteBatchEventEventListener | ||
{ | ||
public function __construct(private Pool $pool) | ||
{ | ||
} | ||
|
||
#[AsEventListener] | ||
public function handlePreObjectDeleteBatchEvent(PreObjectDeleteBatchEvent $event): void | ||
{ | ||
$canDelete = $this->pool | ||
->getAdminByClass($event->getClassName()) | ||
->hasAccess('delete', $event->getObject()); | ||
|
||
if (!$canDelete) { | ||
$event->preventDelete(); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
32 changes: 32 additions & 0 deletions
32
...-bundle/Tests/DependencyInjection/DrawSonataExtraExtensionBatchDeleteCheckEnabledTest.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace DependencyInjection; | ||
|
||
use Draw\Bundle\SonataExtraBundle\DependencyInjection\DrawSonataExtraExtension; | ||
use Draw\Bundle\SonataExtraBundle\EventListener\PreObjectDeleteBatchEventEventListener; | ||
use Draw\Bundle\SonataExtraBundle\Tests\DependencyInjection\DrawSonataExtraExtensionTest; | ||
use Symfony\Component\DependencyInjection\Extension\Extension; | ||
|
||
class DrawSonataExtraExtensionBatchDeleteCheckEnabledTest extends DrawSonataExtraExtensionTest | ||
{ | ||
public function createExtension(): Extension | ||
{ | ||
return new DrawSonataExtraExtension(); | ||
} | ||
|
||
public function getConfiguration(): array | ||
{ | ||
return [ | ||
...parent::getConfiguration(), | ||
'batch_delete_check' => [ | ||
'enabled' => true, | ||
], | ||
]; | ||
} | ||
|
||
public static function provideTestHasServiceDefinition(): iterable | ||
{ | ||
yield from parent::provideTestHasServiceDefinition(); | ||
yield [PreObjectDeleteBatchEventEventListener::class]; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
87 changes: 87 additions & 0 deletions
87
tests/SonataExtraBundle/EventListener/PreObjectDeleteBatchEventEventListenerTest.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace App\Tests\SonataExtraBundle\EventListener; | ||
|
||
use App\Entity\User; | ||
use App\Security\Voter\CannotDeleteSelfVoter; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Draw\Bundle\SonataExtraBundle\EventListener\PreObjectDeleteBatchEventEventListener; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; | ||
use Sonata\DoctrineORMAdminBundle\Event\PreObjectDeleteBatchEvent; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
|
||
class PreObjectDeleteBatchEventEventListenerTest extends KernelTestCase implements AutowiredInterface | ||
{ | ||
#[AutowireService] | ||
private PreObjectDeleteBatchEventEventListener $object; | ||
|
||
#[AutowireService] | ||
private TokenStorageInterface $tokenStorage; | ||
|
||
#[AutowireService] | ||
private EntityManagerInterface $entityManager; | ||
|
||
public function testHandlePreObjectDeleteBatchEventCannotDelete(): void | ||
{ | ||
$user = $this->entityManager | ||
->getRepository(User::class) | ||
->findOneBy(['email' => '[email protected]']); | ||
|
||
$this->connectUser($user); | ||
|
||
$this->object->handlePreObjectDeleteBatchEvent( | ||
$event = new PreObjectDeleteBatchEvent( | ||
User::class, | ||
$user | ||
) | ||
); | ||
|
||
static::assertFalse( | ||
$event->shouldDelete(), | ||
CannotDeleteSelfVoter::class.' should prevent deletion of the user.' | ||
); | ||
} | ||
|
||
public function testHandlePreObjectDeleteBatchEventCanDelete(): void | ||
{ | ||
$user = $this->entityManager | ||
->getRepository(User::class) | ||
->findOneBy(['email' => '[email protected]']); | ||
|
||
$this->connectUser($user); | ||
|
||
$this->object->handlePreObjectDeleteBatchEvent( | ||
$event = new PreObjectDeleteBatchEvent( | ||
User::class, | ||
new User() | ||
) | ||
); | ||
|
||
static::assertTrue( | ||
$event->shouldDelete() | ||
); | ||
} | ||
|
||
private function connectUser(User $user): void | ||
{ | ||
$this->tokenStorage | ||
->setToken( | ||
new class($user) extends AbstractToken { | ||
public function __construct(User $user) | ||
{ | ||
parent::__construct(['ROLE_SUPER_ADMIN']); | ||
|
||
$this->setUser($user); | ||
} | ||
|
||
public function getCredentials() | ||
{ | ||
return null; | ||
} | ||
} | ||
); | ||
} | ||
} |