User roles and permission management via Admin UI (#1231)

This commit is contained in:
Kevin Papst
2019-11-10 18:53:56 +01:00
committed by GitHub
parent c6c4098759
commit af0f89774e
58 changed files with 1355 additions and 186 deletions

View File

@@ -15,7 +15,7 @@ use Symfony\Component\HttpKernel\HttpKernelBrowser;
/**
* @group integration
*/
class LayoutTest extends ControllerBaseTest
class LayoutControllerTest extends ControllerBaseTest
{
public function testNavigationMenus()
{
@@ -72,4 +72,26 @@ class LayoutTest extends ControllerBaseTest
$this->assertStringContainsString('<a href="/en/calendar/">', $content);
$this->assertStringContainsString('<span>Calendar</span>', $content);
}
public function testActiveEntries()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$user = $this->getUserByRole($em, User::ROLE_USER);
$this->request($client, '/layou/active_entries');
$this->assertTrue($client->getResponse()->isSuccessful());
$content = $client->getResponse()->getContent();
self::assertStringContainsString('<li class="dropdown messages-menu" style="display:none">', $content);
self::assertStringContainsString('<ul class="dropdown-menu"', $content);
self::assertStringContainsString('data-api="', $content);
self::assertStringContainsString('data-href="', $content);
self::assertStringContainsString('data-icon=', $content);
self::assertStringContainsString('data-format=', $content);
self::assertStringContainsString('<ul class="menu">', $content);
self::assertStringContainsString('<li class="messages-menu-empty" style="">', $content);
}
}

View File

@@ -0,0 +1,158 @@
<?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\Controller;
use App\Entity\RolePermission;
use App\Entity\User;
use Doctrine\ORM\EntityManager;
/**
* @group integration
*/
class PermissionControllerTest extends ControllerBaseTest
{
public function testPermissionsIsSecure()
{
$this->assertUrlIsSecured('/admin/permissions');
$this->assertUrlIsSecuredForRole(User::ROLE_ADMIN, '/admin/permissions');
}
public function testPermissions()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, '/admin/permissions');
$this->assertHasDataTable($client);
$this->assertDataTableRowCount($client, 'datatable_user_admin_permissions', 83);
$this->assertPageActions($client, [
'back' => $this->createUrl('/admin/user/'),
'roles modal-ajax-form' => $this->createUrl('/admin/permissions/roles/create'),
'help' => 'https://www.kimai.org/documentation/permissions.html'
]);
$content = $client->getResponse()->getContent();
// the english translation instead of the real system user role names
self::assertStringContainsString('<th data-field="User" class="alwaysVisible text-center">', $content);
self::assertStringContainsString('<th data-field="Teamlead" class="alwaysVisible text-center">', $content);
self::assertStringContainsString('<th data-field="Administrator" class="alwaysVisible text-center">', $content);
self::assertStringContainsString('<th data-field="System-Admin" class="alwaysVisible text-center">', $content);
}
public function testCreateRoleIsSecured()
{
$this->assertUrlIsSecured('/admin/permissions/roles/create');
$this->assertUrlIsSecuredForRole(User::ROLE_ADMIN, '/admin/permissions');
}
public function testCreateRole()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, '/admin/permissions/roles/create');
$form = $client->getCrawler()->filter('form[name=role]')->form();
$client->submit($form, [
'role' => [
'name' => 'TEST_ROLE',
]
]);
$this->assertIsRedirect($client, $this->createUrl('/admin/permissions'));
$client->followRedirect();
$content = $client->getResponse()->getContent();
// the english translation instead of the real system user role names
self::assertStringContainsString('<th data-field="User" class="alwaysVisible text-center">', $content);
self::assertStringContainsString('<th data-field="Teamlead" class="alwaysVisible text-center">', $content);
self::assertStringContainsString('<th data-field="Administrator" class="alwaysVisible text-center">', $content);
self::assertStringContainsString('<th data-field="System-Admin" class="alwaysVisible text-center">', $content);
self::assertStringContainsString('<th data-field="TEST_ROLE" class="alwaysVisible text-center">', $content);
}
public function testDeleteRoleIsSecured()
{
$this->assertUrlIsSecured('/admin/permissions/roles/1/delete');
$this->assertUrlIsSecuredForRole(User::ROLE_ADMIN, '/admin/permissions');
}
public function testDeleteRole()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, '/admin/permissions/roles/create');
$form = $client->getCrawler()->filter('form[name=role]')->form();
$client->submit($form, [
'role' => [
'name' => 'TEST_ROLE',
]
]);
$this->assertIsRedirect($client, $this->createUrl('/admin/permissions'));
$client->followRedirect();
$content = $client->getResponse()->getContent();
self::assertStringContainsString('<th data-field="TEST_ROLE" class="alwaysVisible text-center">', $content);
$this->request($client, '/admin/permissions/roles/1/delete');
$this->assertIsRedirect($client, $this->createUrl('/admin/permissions'));
$client->followRedirect();
self::assertHasFlashDeleteSuccess($client);
$content = $client->getResponse()->getContent();
self::assertStringNotContainsString('<th data-field="TEST_ROLE" class="alwaysVisible text-center">', $content);
}
public function testSavePermissionIsSecured()
{
$this->assertUrlIsSecured('/admin/permissions/roles/1/view_user/1');
$this->assertUrlIsSecuredForRole(User::ROLE_ADMIN, '/admin/permissions');
}
public function testSavePermission()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, '/admin/permissions/roles/create');
$form = $client->getCrawler()->filter('form[name=role]')->form();
$client->submit($form, [
'role' => [
'name' => 'TEST_ROLE',
]
]);
$this->assertIsRedirect($client, $this->createUrl('/admin/permissions'));
/** @var EntityManager $em */
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$rolePermissions = $em->getRepository(RolePermission::class)->findAll();
$this->assertEquals(0, count($rolePermissions));
// create the permission
$this->request($client, '/admin/permissions/roles/1/view_user/1');
$this->assertIsRedirect($client, $this->createUrl('/admin/permissions'));
$client->followRedirect();
$rolePermissions = $em->getRepository(RolePermission::class)->findAll();
$this->assertEquals(1, count($rolePermissions));
$permission = $rolePermissions[0];
self::assertInstanceOf(RolePermission::class, $permission);
self::assertEquals('view_user', $permission->getPermission());
self::assertTrue($permission->isAllowed());
self::assertEquals('TEST_ROLE', $permission->getRole()->getName());
self::assertEquals(1, $permission->getRole()->getId());
// flush the cache to prevent wrong results
$em->clear(RolePermission::class);
// update the permission
$this->request($client, '/admin/permissions/roles/1/view_user/0');
$this->assertIsRedirect($client, $this->createUrl('/admin/permissions'));
$client->followRedirect();
$rolePermissions = $em->getRepository(RolePermission::class)->findAll();
$this->assertEquals(1, count($rolePermissions));
$permission = $rolePermissions[0];
self::assertInstanceOf(RolePermission::class, $permission);
self::assertEquals('view_user', $permission->getPermission());
self::assertFalse($permission->isAllowed());
}
}

View File

@@ -30,6 +30,13 @@ class UserControllerTest extends ControllerBaseTest
$this->assertAccessIsGranted($client, '/admin/user/');
$this->assertHasDataTable($client);
$this->assertDataTableRowCount($client, 'datatable_user_admin', 7);
$this->assertPageActions($client, [
'search search-toggle visible-xs-inline' => '#',
'visibility' => '#',
'permissions' => $this->createUrl('/admin/permissions'),
'create' => $this->createUrl('/admin/user/create'),
'help' => 'https://www.kimai.org/documentation/users.html'
]);
}
public function testIndexActionWithSearchTermQuery()
@@ -220,18 +227,4 @@ class UserControllerTest extends ControllerBaseTest
],
];
}
public function testPermissionsIsSecure()
{
$this->assertUrlIsSecured('/admin/user/permissions');
$this->assertUrlIsSecuredForRole(User::ROLE_ADMIN, '/admin/user/permissions');
}
public function testPermissions()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, '/admin/user/permissions');
$this->assertHasDataTable($client);
$this->assertDataTableRowCount($client, 'datatable_user_admin_permissions', 83);
}
}

View File

@@ -0,0 +1,44 @@
<?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\Entity;
use App\Entity\Role;
use App\Entity\RolePermission;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Entity\RolePermission
*/
class RolePermissionTest extends TestCase
{
public function testDefaultValues()
{
$sut = new RolePermission();
self::assertNull($sut->getId());
self::assertNull($sut->getPermission());
self::assertNull($sut->getRole());
self::assertFalse($sut->isAllowed());
}
public function testSetterAndGetter()
{
$sut = new RolePermission();
self::assertInstanceOf(RolePermission::class, $sut->setPermission('foo'));
self::assertEquals('foo', $sut->getPermission());
$role = (new Role())->setName('sdfsd');
self::assertInstanceOf(RolePermission::class, $sut->setRole($role));
self::assertSame($role, $sut->getRole());
self::assertInstanceOf(RolePermission::class, $sut->setAllowed(true));
self::assertTrue($sut->isAllowed());
}
}

34
tests/Entity/RoleTest.php Normal file
View File

@@ -0,0 +1,34 @@
<?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\Entity;
use App\Entity\Role;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Entity\Role
*/
class RoleTest extends TestCase
{
public function testDefaultValues()
{
$sut = new Role();
self::assertNull($sut->getId());
self::assertNull($sut->getName());
}
public function testSetterAndGetter()
{
$sut = new Role();
self::assertInstanceOf(Role::class, $sut->setName('foo'));
self::assertEquals('foo', $sut->getName());
}
}

View File

@@ -15,7 +15,7 @@ use App\Ldap\LdapDriver;
use App\Ldap\LdapDriverException;
use App\Ldap\LdapManager;
use App\Ldap\LdapUserHydrator;
use App\Security\RoleService;
use App\Tests\Mocks\Security\RoleServiceFactory;
use PHPUnit\Framework\TestCase;
/**
@@ -50,11 +50,13 @@ class LdapManagerTest extends TestCase
'role' => $roleConfig,
]);
$hydrator = new LdapUserHydrator($config, new RoleService([
$roles = [
'ROLE_TEAMLEAD' => ['ROLE_USER'],
'ROLE_ADMIN' => ['ROLE_TEAMLEAD'],
'ROLE_SUPER_ADMIN' => ['ROLE_ADMIN']
]));
];
$hydrator = new LdapUserHydrator($config, (new RoleServiceFactory($this))->create($roles));
return new LdapManager($driver, $hydrator, $config);
}

View File

@@ -12,7 +12,7 @@ namespace App\Tests\Ldap;
use App\Configuration\LdapConfiguration;
use App\Entity\User;
use App\Ldap\LdapUserHydrator;
use App\Security\RoleService;
use App\Tests\Mocks\Security\RoleServiceFactory;
use PHPUnit\Framework\TestCase;
/**
@@ -33,7 +33,7 @@ class LdapUserHydratorTest extends TestCase
'role' => [],
]);
$sut = new LdapUserHydrator($config, new RoleService([]));
$sut = new LdapUserHydrator($config, (new RoleServiceFactory($this))->create([]));
$user = $sut->hydrate(['dn' => 'blub']);
self::assertInstanceOf(User::class, $user);
self::assertEmpty($user->getUsername());
@@ -71,7 +71,7 @@ class LdapUserHydratorTest extends TestCase
'dn' => 'blub',
];
$sut = new LdapUserHydrator($config, new RoleService([]));
$sut = new LdapUserHydrator($config, (new RoleServiceFactory($this))->create([]));
$user = $sut->hydrate($ldapEntry);
self::assertInstanceOf(User::class, $user);
@@ -113,7 +113,7 @@ class LdapUserHydratorTest extends TestCase
'dn' => 'blub',
];
$sut = new LdapUserHydrator($config, new RoleService([]));
$sut = new LdapUserHydrator($config, (new RoleServiceFactory($this))->create([]));
$user = new User();
$user->setPassword('foobar');
$sut->hydrateUser($user, $ldapEntry);
@@ -176,11 +176,13 @@ class LdapUserHydratorTest extends TestCase
'count' => 4
];
$sut = new LdapUserHydrator($config, new RoleService([
$roles = [
'ROLE_TEAMLEAD' => ['ROLE_USER'],
'ROLE_ADMIN' => ['ROLE_TEAMLEAD'],
'ROLE_SUPER_ADMIN' => ['ROLE_ADMIN']
]));
];
$sut = new LdapUserHydrator($config, (new RoleServiceFactory($this))->create($roles));
$user = new User();
$sut->hydrateRoles($user, $ldapGroups);
self::assertEquals(['ROLE_TEAMLEAD', 'ROLE_ADMIN', 'ROLE_USER'], $user->getRoles());

View File

@@ -9,13 +9,20 @@
namespace App\Tests\Mocks\Security;
use App\Entity\Role;
use App\Entity\User;
use App\Repository\RoleRepository;
use App\Security\RoleService;
use App\Tests\Mocks\AbstractMockFactory;
class RoleServiceFactory extends AbstractMockFactory
{
public function create($roles = null): RoleService
/**
* @param string[]|null $roles
* @param Role[]|null $repositoryRoles
* @return RoleService
*/
public function create(?array $roles = null, ?array $repositoryRoles = []): RoleService
{
if (null === $roles) {
$roles = [
@@ -26,6 +33,10 @@ class RoleServiceFactory extends AbstractMockFactory
];
}
return new RoleService($roles);
$repository = $this->getMockBuilder(RoleRepository::class)->onlyMethods(['findAll'])->disableOriginalConstructor()->getMock();
$repository->method('findAll')->willReturn($repositoryRoles);
/* @var RoleRepository $repository */
return new RoleService($repository, $roles);
}
}

View File

@@ -0,0 +1,106 @@
<?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\Security;
use App\Repository\RolePermissionRepository;
use App\Security\RolePermissionManager;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Security\RolePermissionManager
*/
class RolePermissionManagerTest extends TestCase
{
public function testWithEmptyRepository()
{
$repository = $this->getMockBuilder(RolePermissionRepository::class)->onlyMethods(['getAllAsArray'])->disableOriginalConstructor()->getMock();
$repository->method('getAllAsArray')->willReturn([]);
/** @var RolePermissionRepository $repository */
$sut = new RolePermissionManager($repository, []);
self::assertFalse($sut->isRegisteredPermission('foo'));
self::assertEquals([], $sut->getPermissions());
self::assertFalse($sut->hasPermission('TEST_ROLE', 'foo'));
}
public function testWithRepositoryData()
{
$repository = $this->getMockBuilder(RolePermissionRepository::class)->onlyMethods(['getAllAsArray'])->disableOriginalConstructor()->getMock();
$repository->method('getAllAsArray')->willReturn([
['permission' => 'foo', 'role' => 'TEST_ROLE', 'allowed' => true],
['permission' => 'bar', 'role' => 'USER_ROLE', 'allowed' => true],
['permission' => 'foo', 'role' => 'USER_ROLE', 'allowed' => false],
]);
/** @var RolePermissionRepository $repository */
$sut = new RolePermissionManager($repository, []);
// only data injected through the config will be registered as "known"
self::assertFalse($sut->isRegisteredPermission('foo'));
self::assertFalse($sut->isRegisteredPermission('bar'));
self::assertEquals([], $sut->getPermissions());
self::assertTrue($sut->hasPermission('TEST_ROLE', 'foo'));
self::assertFalse($sut->hasPermission('USER_ROLE', 'foo'));
self::assertTrue($sut->hasPermission('USER_ROLE', 'bar'));
}
public function testWithConfigData()
{
$repository = $this->getMockBuilder(RolePermissionRepository::class)->onlyMethods(['getAllAsArray'])->disableOriginalConstructor()->getMock();
$repository->method('getAllAsArray')->willReturn([]);
/** @var RolePermissionRepository $repository */
$sut = new RolePermissionManager($repository, ['TEST_ROLE' => ['foo'], 'USER_ROLE' => ['bar']]);
self::assertTrue($sut->isRegisteredPermission('foo'));
self::assertTrue($sut->isRegisteredPermission('bar'));
self::assertEquals(['foo', 'bar'], $sut->getPermissions());
self::assertTrue($sut->hasPermission('TEST_ROLE', 'foo'));
self::assertFalse($sut->hasPermission('TEST_ROLE', 'bar'));
self::assertFalse($sut->hasPermission('USER_ROLE', 'foo'));
self::assertTrue($sut->hasPermission('USER_ROLE', 'bar'));
}
public function testWithMixedData()
{
$repository = $this->getMockBuilder(RolePermissionRepository::class)->onlyMethods(['getAllAsArray'])->disableOriginalConstructor()->getMock();
$repository->method('getAllAsArray')->willReturn([
['permission' => 'foo', 'role' => 'TEST_ROLE', 'allowed' => false],
['permission' => 'bar', 'role' => 'USER_ROLE', 'allowed' => true],
['permission' => 'foo', 'role' => 'USER_ROLE', 'allowed' => false],
['permission' => 'role_permissions', 'role' => 'ROLE_SUPER_ADMIN', 'allowed' => false],
['permission' => 'view_user', 'role' => 'ROLE_SUPER_ADMIN', 'allowed' => false],
['permission' => 'create_user', 'role' => 'ROLE_SUPER_ADMIN', 'allowed' => false],
]);
/** @var RolePermissionRepository $repository */
$sut = new RolePermissionManager($repository, [
'ROLE_SUPER_ADMIN' => ['role_permissions', 'view_user', 'create_user'],
'TEST_ROLE' => ['foo2', 'foo'],
'USER_ROLE' => ['foo', 'bar']
]);
self::assertTrue($sut->isRegisteredPermission('foo'));
self::assertTrue($sut->isRegisteredPermission('bar'));
self::assertEquals(['role_permissions', 'view_user', 'create_user', 'foo2', 'foo', 'bar'], array_values($sut->getPermissions()));
self::assertTrue($sut->hasPermission('TEST_ROLE', 'foo2'));
self::assertFalse($sut->hasPermission('TEST_ROLE', 'foo'));
self::assertFalse($sut->hasPermission('USER_ROLE', 'foo'));
self::assertTrue($sut->hasPermission('USER_ROLE', 'bar'));
self::assertFalse($sut->hasPermission('ROLE_SUPER_ADMIN', 'create_user'));
// the next two are a special case, which might never be falsified by the database
self::assertTrue($sut->hasPermission('ROLE_SUPER_ADMIN', 'role_permissions'));
self::assertTrue($sut->hasPermission('ROLE_SUPER_ADMIN', 'view_user'));
}
}

View File

@@ -9,7 +9,8 @@
namespace App\Tests\Security;
use App\Security\RoleService;
use App\Entity\Role;
use App\Tests\Mocks\Security\RoleServiceFactory;
use PHPUnit\Framework\TestCase;
/**
@@ -17,7 +18,7 @@ use PHPUnit\Framework\TestCase;
*/
class RoleServiceTest extends TestCase
{
public function testGetAvailableNames()
public function testWithEmptyRepository()
{
$real = [
'ROLE_TEAMLEAD' => [0 => 'ROLE_USER'],
@@ -25,10 +26,34 @@ class RoleServiceTest extends TestCase
'ROLE_SUPER_ADMIN' => [0 => 'ROLE_ADMIN']
];
$sut = new RoleService($real);
$sut = (new RoleServiceFactory($this))->create($real);
$expected = ['ROLE_TEAMLEAD', 'ROLE_USER', 'ROLE_ADMIN', 'ROLE_SUPER_ADMIN'];
self::assertEquals($expected, $sut->getAvailableNames());
self::assertEquals($real, $sut->getSystemRoles());
}
public function testWithRepositoryData()
{
$real = [
'ROLE_TEAMLEAD' => [0 => 'ROLE_USER'],
'ROLE_ADMIN' => [0 => 'ROLE_TEAMLEAD'],
'ROLE_SUPER_ADMIN' => [0 => 'ROLE_ADMIN']
];
$repository = [
(new Role())->setName('TEST_ROLE'),
(new Role())->setName('ROLE_ADMIN'),
(new Role())->setName('ROLE_ADMINX'),
(new Role())->setName('TEST_ROLE'),
];
$sut = (new RoleServiceFactory($this))->create($real, $repository);
$expected = ['ROLE_TEAMLEAD', 'ROLE_USER', 'ROLE_ADMIN', 'ROLE_SUPER_ADMIN', 'TEST_ROLE', 'ROLE_ADMINX'];
self::assertEquals($expected, $sut->getAvailableNames());
self::assertEquals($real, $sut->getSystemRoles());
}
}

View File

@@ -10,9 +10,9 @@
namespace App\Tests\Voter;
use App\Entity\User;
use App\Repository\RolePermissionRepository;
use App\Security\AclDecisionManager;
use App\Security\RolePermissionManager;
use App\Tests\Mocks\Security\RoleServiceFactory;
use App\Voter\AbstractVoter;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;
@@ -97,9 +97,10 @@ abstract class AbstractVoterTest extends TestCase
];
}
$factory = new RoleServiceFactory($this);
$roleService = $factory->create();
$repository = $this->getMockBuilder(RolePermissionRepository::class)->onlyMethods(['getAllAsArray'])->disableOriginalConstructor()->getMock();
$repository->method('getAllAsArray')->willReturn([]);
return new RolePermissionManager($roleService, $permissions);
/* @var RolePermissionRepository $repository */
return new RolePermissionManager($repository, $permissions);
}
}