add user management
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Repository\UserRepository;
|
||||
use App\Security\ApplicationRoles;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
@@ -11,6 +14,7 @@ use Symfony\Component\Console\Helper\QuestionHelper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
|
||||
@@ -18,13 +22,13 @@ use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
name: 'mto:agent:user:create',
|
||||
description: 'Creates a new application user'
|
||||
)]
|
||||
class CreateUserCommand extends Command
|
||||
final class CreateUserCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private UserPasswordHasherInterface $passwordHasher
|
||||
)
|
||||
{
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly UserRepository $users,
|
||||
private readonly UserPasswordHasherInterface $passwordHasher,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
@@ -33,76 +37,81 @@ class CreateUserCommand extends Command
|
||||
/** @var QuestionHelper $helper */
|
||||
$helper = $this->getHelper('question');
|
||||
|
||||
// =============================
|
||||
// Email
|
||||
// =============================
|
||||
$emailQuestion = new Question('E-Mail: ');
|
||||
$emailQuestion->setValidator(function ($value) {
|
||||
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||||
$emailQuestion->setValidator(function (mixed $value): string {
|
||||
$email = strtolower(trim((string) $value));
|
||||
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
throw new \RuntimeException('Invalid email address.');
|
||||
}
|
||||
return strtolower(trim($value));
|
||||
|
||||
return $email;
|
||||
});
|
||||
|
||||
$email = $helper->ask($input, $output, $emailQuestion);
|
||||
|
||||
// Prüfen ob User existiert
|
||||
$existingUser = $this->em
|
||||
->getRepository(User::class)
|
||||
->findOneBy(['email' => $email]);
|
||||
|
||||
if ($existingUser) {
|
||||
if ($this->users->findOneByNormalizedEmail($email) instanceof User) {
|
||||
$output->writeln('<error>User already exists.</error>');
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// =============================
|
||||
// Passwort
|
||||
// =============================
|
||||
$passwordQuestion = new Question('Password: ');
|
||||
$passwordQuestion->setHidden(true);
|
||||
$passwordQuestion->setHiddenFallback(false);
|
||||
|
||||
$plainPassword = $helper->ask($input, $output, $passwordQuestion);
|
||||
$plainPassword = (string) $helper->ask($input, $output, $passwordQuestion);
|
||||
|
||||
if (strlen($plainPassword) < 8) {
|
||||
if (strlen(trim($plainPassword)) < 8) {
|
||||
$output->writeln('<error>Password must be at least 8 characters.</error>');
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$passwordRepeatQuestion = new Question('Repeat password: ');
|
||||
$passwordRepeatQuestion->setHidden(true);
|
||||
$passwordRepeatQuestion->setHiddenFallback(false);
|
||||
|
||||
$passwordRepeat = (string) $helper->ask($input, $output, $passwordRepeatQuestion);
|
||||
|
||||
if ($plainPassword !== $passwordRepeat) {
|
||||
$output->writeln('<error>Passwords do not match.</error>');
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// =============================
|
||||
// Rolle auswählen
|
||||
// =============================
|
||||
$roleQuestion = new ChoiceQuestion(
|
||||
'Select role:',
|
||||
[
|
||||
'ROLE_SUPER_ADMIN',
|
||||
'ROLE_KNOWLEDGE_ADMIN',
|
||||
'ROLE_EDITOR',
|
||||
'ROLE_ADMIN_AREA',
|
||||
'ROLE_CHAT_USER',
|
||||
],
|
||||
0
|
||||
'Select role(s), comma-separated if needed:',
|
||||
ApplicationRoles::assignableRoleNames(),
|
||||
'0'
|
||||
);
|
||||
$roleQuestion->setMultiselect(true);
|
||||
|
||||
$role = $helper->ask($input, $output, $roleQuestion);
|
||||
$roles = $helper->ask($input, $output, $roleQuestion);
|
||||
$roles = is_array($roles) ? array_values(array_unique(array_map('strval', $roles))) : [];
|
||||
|
||||
if ($roles === []) {
|
||||
$output->writeln('<error>At least one role is required.</error>');
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$activeQuestion = new ConfirmationQuestion('Activate user? [Y/n] ', true);
|
||||
$isActive = (bool) $helper->ask($input, $output, $activeQuestion);
|
||||
|
||||
// =============================
|
||||
// User erzeugen
|
||||
// =============================
|
||||
$user = new User();
|
||||
$user->setEmail($email);
|
||||
$user->setRoles([$role]);
|
||||
|
||||
$hashedPassword = $this->passwordHasher->hashPassword($user, $plainPassword);
|
||||
$user->setPassword($hashedPassword);
|
||||
$user->setRoles($roles);
|
||||
$user->setIsActive($isActive);
|
||||
$user->setPassword($this->passwordHasher->hashPassword($user, $plainPassword));
|
||||
|
||||
$this->em->persist($user);
|
||||
$this->em->flush();
|
||||
|
||||
$output->writeln('<info>User created successfully.</info>');
|
||||
$output->writeln('Email: ' . $email);
|
||||
$output->writeln('Role: ' . $role);
|
||||
$output->writeln('Roles: ' . implode(', ', $roles));
|
||||
$output->writeln('Active: ' . ($isActive ? 'yes' : 'no'));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user