added event before rendering permissions (#1599)

This commit is contained in:
Kevin Papst
2020-03-30 01:22:57 +02:00
committed by GitHub
parent 7becddc3a4
commit 9e1e1a7a96
6 changed files with 136 additions and 5 deletions

View File

@@ -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(),
]);

View File

@@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Event;
use Symfony\Contracts\EventDispatcher\Event;
/**
* This event can be used, to dynamically change the displayed permissions in the permission screen.
*/
final class PermissionsEvent extends Event
{
/**
* @var array
*/
private $sections = [];
/**
* @param string $section
* @param string[] $permissions
* @return PermissionsEvent
*/
public function addPermissions(string $section, array $permissions): PermissionsEvent
{
$this->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;
}
}

View File

@@ -55,7 +55,7 @@
{% endif %}
{% endfor %}
{{ tables.data_table_footer(permissions) }}
{{ tables.data_table_footer() }}
{% endblock %}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Event;
use App\Event\PermissionsEvent;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Event\PermissionsEvent
*/
class PermissionsEventTest extends TestCase
{
public function testGetterAndSetter()
{
$sut = new PermissionsEvent();
self::assertEmpty($sut->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());
}
}

View File

@@ -48,7 +48,7 @@
</trans-unit>
<trans-unit id="invoice.first_template">
<source>invoice.first_template</source>
<target>Sie müssen zunächst eine Rechnungsvorlage erstellen, bevor Sie fortfahren können</target>
<target>Bitte legen Sie zunächst eine Rechnungsvorlage an</target>
</trans-unit>
</body>
</file>

View File

@@ -48,7 +48,7 @@
</trans-unit>
<trans-unit id="invoice.first_template">
<source>invoice.first_template</source>
<target>You have to create your first template invoice before you can proceed</target>
<target>Please create an invoice template first</target>
</trans-unit>
</body>
</file>