Files
kimai2/src/Command/CreateUserCommand.php
Kevin Papst 83a894823f release 1.17.2 (#3135)
* allow to input password interactively in console
* added block to simplify overwriting export template parts
* prevent installation in PHP 8.1
* composer update
* phpunit version to 9
* support negative amounts in excel export
* added system configuration actions for calendar, weekly timesheet, users
* added method to render text with full markdown support
2022-02-14 17:33:52 +01:00

92 lines
2.7 KiB
PHP

<?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\Command;
use App\Entity\User;
use App\User\UserService;
use App\Utils\CommandStyle;
use App\Validator\ValidationFailedException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
final class CreateUserCommand extends AbstractUserCommand
{
private $userService;
public function __construct(UserService $userService)
{
parent::__construct();
$this->userService = $userService;
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$roles = implode(',', [User::DEFAULT_ROLE, User::ROLE_ADMIN]);
$this
->setName('kimai:user:create')
->setAliases(['kimai:create-user'])
->setDescription('Create a new user')
->setHelp('This command allows you to create a new user.')
->addArgument('username', InputArgument::REQUIRED, 'A name for the new user (must be unique)')
->addArgument('email', InputArgument::REQUIRED, 'Email address of the new user (must be unique)')
->addArgument(
'role',
InputArgument::OPTIONAL,
'A comma separated list of user roles, e.g. "' . $roles . '"',
User::DEFAULT_ROLE
)
->addArgument('password', InputArgument::OPTIONAL, 'Password for the new user (requested if not provided)')
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new CommandStyle($input, $output);
$username = $input->getArgument('username');
$email = $input->getArgument('email');
$role = $input->getArgument('role');
if (null !== $input->getArgument('password')) {
$password = $input->getArgument('password');
} else {
$password = $this->askForPassword($input, $output);
}
$role = $role ?: User::DEFAULT_ROLE;
$user = $this->userService->createNewUser();
$user->setUsername($username);
$user->setPlainPassword($password);
$user->setEmail($email);
$user->setEnabled(true);
$user->setRoles(explode(',', $role));
try {
$this->userService->saveNewUser($user);
$io->success(sprintf('Success! Created user: %s', $username));
} catch (ValidationFailedException $ex) {
$io->validationError($ex);
return 2;
}
return 0;
}
}