Refactor authentication system (#2602)

Make auth configuration available via UI, remove FOSUserBundle and SAML-Bundle dependency
This commit is contained in:
Kevin Papst
2021-06-10 15:34:13 +02:00
committed by GitHub
parent 286b63e2c8
commit 7f20cb045c
155 changed files with 5590 additions and 1802 deletions

View File

@@ -0,0 +1,102 @@
<?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\Command;
use App\Command\ActivateUserCommand;
use App\Entity\User;
use App\Repository\UserRepository;
use App\User\UserService;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @covers \App\Command\ActivateUserCommand
* @group integration
*/
class ActivateUserCommandTest extends KernelTestCase
{
/**
* @var Application
*/
private $application;
protected function setUp(): void
{
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$container = self::$kernel->getContainer();
$userService = $container->get(UserService::class);
$this->application->add(new ActivateUserCommand($userService));
}
public function testCommandName()
{
$application = $this->application;
$command = $application->find('kimai:user:activate');
self::assertInstanceOf(ActivateUserCommand::class, $command);
// test alias
$command = $application->find('fos:user:activate');
self::assertInstanceOf(ActivateUserCommand::class, $command);
}
protected function callCommand(?string $username)
{
$command = $this->application->find('kimai:user:activate');
$input = [
'command' => $command->getName(),
];
if ($username !== null) {
$input['username'] = $username;
}
$commandTester = new CommandTester($command);
$commandTester->execute($input);
return $commandTester;
}
public function testActivate()
{
$commandTester = $this->callCommand('chris_user');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[OK] User "chris_user" has been activated.', $output);
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('chris_user');
self::assertInstanceOf(User::class, $user);
self::assertTrue($user->isEnabled());
}
public function testActivateOnActiveUser()
{
$commandTester = $this->callCommand('susan_super');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[WARNING] User "susan_super" is already active.', $output);
}
public function testWithMissingUsername()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "username").');
$this->callCommand(null);
}
}

View File

@@ -0,0 +1,109 @@
<?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\Command;
use App\Command\ChangePasswordCommand;
use App\Entity\User;
use App\Repository\UserRepository;
use App\User\UserService;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @covers \App\Command\ChangePasswordCommand
* @group integration
*/
class ChangePasswordCommandTest extends KernelTestCase
{
/**
* @var Application
*/
private $application;
protected function setUp(): void
{
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$container = self::$kernel->getContainer();
$userService = $container->get(UserService::class);
$this->application->add(new ChangePasswordCommand($userService));
}
public function testCommandName()
{
$application = $this->application;
$command = $application->find('kimai:user:password');
self::assertInstanceOf(ChangePasswordCommand::class, $command);
// test alias
$command = $application->find('fos:user:change-password');
self::assertInstanceOf(ChangePasswordCommand::class, $command);
}
protected function callCommand(?string $username, ?string $password)
{
$command = $this->application->find('kimai:user:password');
$input = [
'command' => $command->getName(),
];
if ($username !== null) {
$input['username'] = $username;
}
if ($password !== null) {
$input['password'] = $password;
}
$commandTester = new CommandTester($command);
$commandTester->execute($input);
return $commandTester;
}
public function testChangePassword()
{
$commandTester = $this->callCommand('john_user', '0987654321');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[OK] Changed password for user "john_user".', $output);
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('john_user');
self::assertInstanceOf(User::class, $user);
$container = self::$kernel->getContainer();
$encoderService = $container->get('security.password_encoder');
self::assertTrue($encoderService->isPasswordValid($user, '0987654321'));
}
public function testWithMissingUsername()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "username").');
$this->callCommand(null, '1234567890');
}
public function testWithMissingPassword()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "password").');
$this->callCommand('1234567890', null);
}
}

View File

@@ -12,6 +12,7 @@ namespace App\Tests\Command;
use App\Command\CreateUserCommand;
use App\Entity\User;
use App\Repository\UserRepository;
use App\User\UserService;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
@@ -25,7 +26,7 @@ class CreateUserCommandTest extends KernelTestCase
/**
* @var Application
*/
protected $application;
private $application;
protected function setUp(): void
{
@@ -33,12 +34,8 @@ class CreateUserCommandTest extends KernelTestCase
$this->application = new Application($kernel);
$container = self::$kernel->getContainer();
$passwordEncoder = $container->get('security.password_encoder');
$this->application->add(new CreateUserCommand(
$passwordEncoder,
$container->get('doctrine'),
$container->get('validator')
$container->get(UserService::class),
));
}
@@ -68,7 +65,7 @@ class CreateUserCommandTest extends KernelTestCase
protected function createUser($username, $email, $role, $password)
{
$command = $this->application->find('kimai:create-user');
$command = $this->application->find('kimai:user:create');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
@@ -86,19 +83,31 @@ class CreateUserCommandTest extends KernelTestCase
$commandTester = $this->createUser('xx', '', 'ROLE_USER', '');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] email ()', $output);
$this->assertStringContainsString('Please enter an email', $output);
$this->assertStringContainsString('This value should not be blank', $output);
$this->assertStringContainsString('[ERROR] plainPassword ()', $output);
$this->assertStringContainsString('Please enter a password', $output);
$this->assertStringContainsString('This value should not be blank', $output);
$this->assertStringContainsString('[ERROR] plainPassword ()', $output);
$this->assertStringContainsString('This value is too short. It should have 8 characters or more', $output);
}
public function testUserAlreadyExisting()
{
$this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar12');
$commandTester = $this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar');
$this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar123');
$commandTester = $this->createUser('MyTestUser', 'user2@example.com', 'ROLE_USER', 'foobar123');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] username (mytestuser)', $output);
$this->assertStringContainsString('The username is already used', $output);
$this->assertStringContainsString('[ERROR] username (MyTestUser)', $output);
$this->assertStringContainsString('The username is already used.', $output);
}
public function testEmailAlreadyExisting()
{
$this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar12');
$commandTester = $this->createUser('MyTestUser2', 'user@example.com', 'ROLE_USER', 'foobar');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] email (MyTestUser2)', $output);
$this->assertStringContainsString(' The email is already used.', $output);
}
public function testUserEmail()
@@ -107,6 +116,6 @@ class CreateUserCommandTest extends KernelTestCase
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] email (ROLE_USER)', $output);
$this->assertStringContainsString('The email is not valid', $output);
$this->assertStringContainsString('This value is not a valid email address', $output);
}
}

View File

@@ -0,0 +1,102 @@
<?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\Command;
use App\Command\DeactivateUserCommand;
use App\Entity\User;
use App\Repository\UserRepository;
use App\User\UserService;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @covers \App\Command\DeactivateUserCommand
* @group integration
*/
class DeactivateUserCommandTest extends KernelTestCase
{
/**
* @var Application
*/
private $application;
protected function setUp(): void
{
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$container = self::$kernel->getContainer();
$userService = $container->get(UserService::class);
$this->application->add(new DeactivateUserCommand($userService));
}
public function testCommandName()
{
$application = $this->application;
$command = $application->find('kimai:user:deactivate');
self::assertInstanceOf(DeactivateUserCommand::class, $command);
// test alias
$command = $application->find('fos:user:deactivate');
self::assertInstanceOf(DeactivateUserCommand::class, $command);
}
protected function callCommand(?string $username)
{
$command = $this->application->find('kimai:user:deactivate');
$input = [
'command' => $command->getName(),
];
if ($username !== null) {
$input['username'] = $username;
}
$commandTester = new CommandTester($command);
$commandTester->execute($input);
return $commandTester;
}
public function testDeactivate()
{
$commandTester = $this->callCommand('john_user');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[OK] User "john_user" has been deactivated.', $output);
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('john_user');
self::assertInstanceOf(User::class, $user);
self::assertFalse($user->isEnabled());
}
public function testDeactivateOnDeactivatedUser()
{
$commandTester = $this->callCommand('chris_user');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[WARNING] User "chris_user" is already deactivated.', $output);
}
public function testWithMissingUsername()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "username").');
$this->callCommand(null);
}
}

View File

@@ -0,0 +1,142 @@
<?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\Command;
use App\Command\DemoteUserCommand;
use App\Entity\User;
use App\Repository\UserRepository;
use App\User\UserService;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @covers \App\Command\AbstractRoleCommand
* @covers \App\Command\DemoteUserCommand
* @group integration
*/
class DemoteUserCommandTest extends KernelTestCase
{
/**
* @var Application
*/
private $application;
protected function setUp(): void
{
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$container = self::$kernel->getContainer();
$userService = $container->get(UserService::class);
$this->application->add(new DemoteUserCommand($userService));
}
public function testCommandName()
{
$application = $this->application;
$command = $application->find('kimai:user:demote');
self::assertInstanceOf(DemoteUserCommand::class, $command);
// test alias
$command = $application->find('fos:user:demote');
self::assertInstanceOf(DemoteUserCommand::class, $command);
}
protected function callCommand(?string $username, ?string $role, bool $super = false)
{
$command = $this->application->find('kimai:user:demote');
$input = [
'command' => $command->getName(),
];
if ($role !== null) {
$input['role'] = $role;
}
if ($username !== null) {
$input['username'] = $username;
}
if ($super) {
$input['--super'] = true;
}
$commandTester = new CommandTester($command);
$commandTester->execute($input);
return $commandTester;
}
public function testDemoteRole()
{
$commandTester = $this->callCommand('tony_teamlead', 'ROLE_TEAMLEAD');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[OK] Role "ROLE_TEAMLEAD" has been removed from user "tony_teamlead".', $output);
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('tony_teamlead');
self::assertInstanceOf(User::class, $user);
self::assertFalse($user->isTeamlead());
}
public function testDemoteSuper()
{
$commandTester = $this->callCommand('susan_super', null, true);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[OK] Super administrator role has been removed from the user "susan_super".', $output);
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('susan_super');
self::assertInstanceOf(User::class, $user);
self::assertFalse($user->isSuperAdmin());
}
public function testDemoteSuperFailsOnTeamlead()
{
$commandTester = $this->callCommand('tony_teamlead', null, true);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[WARNING] User "tony_teamlead" doesn\'t have the super administrator role.', $output);
}
public function testDemoteAdminFailsOnTeamlead()
{
$commandTester = $this->callCommand('tony_teamlead', 'ROLE_ADMIN', false);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[WARNING] User "tony_teamlead" didn\'t have "ROLE_ADMIN" role.', $output);
}
public function testDemoteRoleAndSuperFails()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('You can pass either the role or the --super option (but not both simultaneously).');
$this->callCommand('john_user', 'ROLE_TEAMLEAD', true);
}
public function testWithMissingUsername()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "username").');
$this->callCommand(null, null, true);
}
}

View File

@@ -0,0 +1,142 @@
<?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\Command;
use App\Command\PromoteUserCommand;
use App\Entity\User;
use App\Repository\UserRepository;
use App\User\UserService;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @covers \App\Command\AbstractRoleCommand
* @covers \App\Command\PromoteUserCommand
* @group integration
*/
class PromoteUserCommandTest extends KernelTestCase
{
/**
* @var Application
*/
private $application;
protected function setUp(): void
{
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$container = self::$kernel->getContainer();
$userService = $container->get(UserService::class);
$this->application->add(new PromoteUserCommand($userService));
}
public function testCommandName()
{
$application = $this->application;
$command = $application->find('kimai:user:promote');
self::assertInstanceOf(PromoteUserCommand::class, $command);
// test alias
$command = $application->find('fos:user:promote');
self::assertInstanceOf(PromoteUserCommand::class, $command);
}
protected function callCommand(?string $username, ?string $role, bool $super = false)
{
$command = $this->application->find('kimai:user:promote');
$input = [
'command' => $command->getName(),
];
if ($role !== null) {
$input['role'] = $role;
}
if ($username !== null) {
$input['username'] = $username;
}
if ($super) {
$input['--super'] = true;
}
$commandTester = new CommandTester($command);
$commandTester->execute($input);
return $commandTester;
}
public function testPromoteRole()
{
$commandTester = $this->callCommand('john_user', 'ROLE_TEAMLEAD');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[OK] Role "ROLE_TEAMLEAD" has been added to user "john_user".', $output);
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('john_user');
self::assertInstanceOf(User::class, $user);
self::assertTrue($user->isTeamlead());
}
public function testPromoteSuper()
{
$commandTester = $this->callCommand('john_user', null, true);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[OK] User "john_user" has been promoted as a super administrator.', $output);
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('john_user');
self::assertInstanceOf(User::class, $user);
self::assertTrue($user->isSuperAdmin());
}
public function testPromoteSuperFailsOnSuperAdmin()
{
$commandTester = $this->callCommand('susan_super', null, true);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[WARNING] User "susan_super" does already have the super administrator role.', $output);
}
public function testPromoteTeamleadFailsOnTeamlead()
{
$commandTester = $this->callCommand('tony_teamlead', 'ROLE_TEAMLEAD', false);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[WARNING] User "tony_teamlead" did already have "ROLE_TEAMLEAD" role.', $output);
}
public function testPromoteRoleAndSuperFails()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('You can pass either the role or the --super option (but not both simultaneously).');
$this->callCommand('john_user', 'ROLE_TEAMLEAD', true);
}
public function testWithMissingUsername()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "username").');
$this->callCommand(null, null, true);
}
}