From 9e1e1a7a96f1a210af91f6b4adbc81623fee18c2 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Mon, 30 Mar 2020 01:22:57 +0200 Subject: [PATCH] added event before rendering permissions (#1599) --- src/Controller/PermissionController.php | 11 +++- src/Event/PermissionsEvent.php | 74 +++++++++++++++++++++++++ templates/user/permissions.html.twig | 2 +- tests/Event/PermissionsEventTest.php | 50 +++++++++++++++++ translations/flashmessages.de.xlf | 2 +- translations/flashmessages.en.xlf | 2 +- 6 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 src/Event/PermissionsEvent.php create mode 100644 tests/Event/PermissionsEventTest.php diff --git a/src/Controller/PermissionController.php b/src/Controller/PermissionController.php index 3644ca61..1746ce09 100644 --- a/src/Controller/PermissionController.php +++ b/src/Controller/PermissionController.php @@ -12,6 +12,7 @@ namespace App\Controller; use App\Entity\Role; use App\Entity\RolePermission; use App\Event\PermissionSectionsEvent; +use App\Event\PermissionsEvent; use App\Form\RoleType; use App\Model\PermissionSection; use App\Repository\RolePermissionRepository; @@ -143,10 +144,16 @@ final class PermissionController extends AbstractController $roles[$role->getName()] = $role; } + $event = new PermissionsEvent(); + foreach ($permissionSorted as $title => $permissions) { + $event->addPermissions($title, $permissions); + } + + $dispatcher->dispatch($event); + return $this->render('user/permissions.html.twig', [ 'roles' => array_values($roles), - 'permissions' => $this->manager->getPermissions(), - 'sorted' => $permissionSorted, + 'sorted' => $event->getPermissions(), 'manager' => $this->manager, 'system_roles' => $this->roleService->getSystemRoles(), ]); diff --git a/src/Event/PermissionsEvent.php b/src/Event/PermissionsEvent.php new file mode 100644 index 00000000..63d31335 --- /dev/null +++ b/src/Event/PermissionsEvent.php @@ -0,0 +1,74 @@ +sections[$section] = $permissions; + + return $this; + } + + public function removePermission(string $section, string $permission): PermissionsEvent + { + if (array_key_exists($section, $this->sections)) { + if (array_key_exists($permission, $this->sections[$section])) { + unset($this->sections[$section][$permission]); + } + } + + return $this; + } + + public function hasSection(string $section): bool + { + return array_key_exists($section, $this->sections); + } + + public function removeSection(string $section): PermissionsEvent + { + if (array_key_exists($section, $this->sections)) { + unset($this->sections[$section]); + } + + return $this; + } + + public function getSection(string $section): ?array + { + if (array_key_exists($section, $this->sections)) { + return $this->sections[$section]; + } + + return null; + } + + public function getPermissions(): array + { + return $this->sections; + } +} diff --git a/templates/user/permissions.html.twig b/templates/user/permissions.html.twig index 8486e3a2..18014c55 100644 --- a/templates/user/permissions.html.twig +++ b/templates/user/permissions.html.twig @@ -55,7 +55,7 @@ {% endif %} {% endfor %} - {{ tables.data_table_footer(permissions) }} + {{ tables.data_table_footer() }} {% endblock %} diff --git a/tests/Event/PermissionsEventTest.php b/tests/Event/PermissionsEventTest.php new file mode 100644 index 00000000..683fae10 --- /dev/null +++ b/tests/Event/PermissionsEventTest.php @@ -0,0 +1,50 @@ +getPermissions()); + self::assertFalse($sut->hasSection('foo')); + self::assertNull($sut->getSection('foo')); + + self::assertInstanceOf(PermissionsEvent::class, $sut->removePermission('test', 'foo')); + + $sut->addPermissions('foo', []); + self::assertTrue($sut->hasSection('foo')); + self::assertEquals([], $sut->getSection('foo')); + self::assertEquals(['foo' => []], $sut->getPermissions()); + + self::assertInstanceOf(PermissionsEvent::class, $sut->removeSection('foo')); + self::assertFalse($sut->hasSection('foo')); + self::assertNull($sut->getSection('foo')); + + $sut->addPermissions('bar', ['foo' => 123, 'hello' => 'world', 'test' => false]); + self::assertEquals(['bar' => ['foo' => 123, 'hello' => 'world', 'test' => false]], $sut->getPermissions()); + self::assertInstanceOf(PermissionsEvent::class, $sut->removePermission('bar', 'xxx')); + self::assertEquals(['bar' => ['foo' => 123, 'hello' => 'world', 'test' => false]], $sut->getPermissions()); + self::assertInstanceOf(PermissionsEvent::class, $sut->removePermission('bar', 'foo')); + self::assertEquals(['bar' => ['hello' => 'world', 'test' => false]], $sut->getPermissions()); + self::assertInstanceOf(PermissionsEvent::class, $sut->removePermission('bar', 'test')); + self::assertEquals(['bar' => ['hello' => 'world']], $sut->getPermissions()); + self::assertInstanceOf(PermissionsEvent::class, $sut->removePermission('bar', 'hello')); + self::assertEquals(['bar' => []], $sut->getPermissions()); + } +} diff --git a/translations/flashmessages.de.xlf b/translations/flashmessages.de.xlf index 79c3d425..024529d9 100644 --- a/translations/flashmessages.de.xlf +++ b/translations/flashmessages.de.xlf @@ -48,7 +48,7 @@ invoice.first_template - Sie müssen zunächst eine Rechnungsvorlage erstellen, bevor Sie fortfahren können + Bitte legen Sie zunächst eine Rechnungsvorlage an diff --git a/translations/flashmessages.en.xlf b/translations/flashmessages.en.xlf index 76de6ad5..ae914ec4 100644 --- a/translations/flashmessages.en.xlf +++ b/translations/flashmessages.en.xlf @@ -48,7 +48,7 @@ invoice.first_template - You have to create your first template invoice before you can proceed + Please create an invoice template first