|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Command; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\User; |
|
6
|
|
|
use App\Entity\UserType; |
|
7
|
|
|
use App\Repository\UserRepositoryInterface; |
|
8
|
|
|
use App\Repository\UserTypeRepositoryInterface; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
|
11
|
|
|
use Symfony\Component\Console\Command\Command; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
|
15
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
16
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
17
|
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; |
|
18
|
|
|
use Symfony\Component\Validator\Constraints\Email; |
|
19
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
20
|
|
|
|
|
21
|
|
|
#[AsCommand(name: 'app:add-user', description: 'Erstellt einen neuen Benutzer')] |
|
22
|
|
|
class AddUserCommand extends Command { |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(private readonly UserTypeRepositoryInterface $userTypeRepository, private readonly UserRepositoryInterface $userRepository, |
|
25
|
|
|
private readonly UserPasswordHasherInterface $passwordHasher, private readonly ValidatorInterface $validator, $name = null) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct($name); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int { |
|
31
|
|
|
$io = new SymfonyStyle($input, $output); |
|
32
|
|
|
|
|
33
|
|
|
$username = $io->ask('Username', null, function($username) { |
|
34
|
|
|
if(empty($username)) { |
|
35
|
|
|
throw new RuntimeException('Benutzername darf nicht leer sein.'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if($this->validator->validate($username, new Email())->count() > 0) { |
|
39
|
|
|
throw new RuntimeException('Benutzername muss eine E-Mail-Adresse sein.'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $username; |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
$firstname = $io->ask('Vorname', null, function($firstname) { |
|
46
|
|
|
if(empty($firstname)) { |
|
47
|
|
|
throw new RuntimeException('Vorname darf nicht leer sein.'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $firstname; |
|
51
|
|
|
}); |
|
52
|
|
|
|
|
53
|
|
|
$lastname = $io->ask('Nachname', null, function($lastname) { |
|
54
|
|
|
if(empty($lastname)) { |
|
55
|
|
|
throw new RuntimeException('Nachname darf nicht leer sein.'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $lastname; |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
$email = $io->ask('E-Mail', null, function($email) { |
|
62
|
|
|
if(empty($email)) { |
|
63
|
|
|
throw new RuntimeException('E-Mail darf nicht leer sein.'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $email; |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
$password = $io->askHidden('Passwort', function($password) { |
|
70
|
|
|
if(empty($password)) { |
|
71
|
|
|
throw new RuntimeException('Passwort darf nicht leer sein.'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $password; |
|
75
|
|
|
}); |
|
76
|
|
|
|
|
77
|
|
|
$io->askHidden('Repeat Password', function($repeatPassword) use ($password) { |
|
78
|
|
|
if($repeatPassword !== $password) { |
|
79
|
|
|
throw new RuntimeException('Passwörter müssen übereinstimmen.'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $repeatPassword; |
|
83
|
|
|
}); |
|
84
|
|
|
|
|
85
|
|
|
$question = new ConfirmationQuestion('Ist der Benutzer ein Administrator?'); |
|
86
|
|
|
$isAdmin = $io->askQuestion($question); |
|
87
|
|
|
|
|
88
|
|
|
$userTypes = $this->userTypeRepository->findAll(); |
|
89
|
|
|
$choices = array_map(fn(UserType $type) => $type->getAlias(), $userTypes); |
|
90
|
|
|
|
|
91
|
|
|
$question = new ChoiceQuestion('Benutzertyp wählen', $choices, 0); |
|
92
|
|
|
$selectedUserTypeAlias = $io->askQuestion($question); |
|
93
|
|
|
$userType = $userTypes[array_search($selectedUserTypeAlias, $choices)]; |
|
94
|
|
|
|
|
95
|
|
|
$user = (new User()) |
|
96
|
|
|
->setUsername($username) |
|
97
|
|
|
->setFirstname($firstname) |
|
98
|
|
|
->setLastname($lastname) |
|
99
|
|
|
->setEmail($email) |
|
100
|
|
|
->setType($userType); |
|
101
|
|
|
|
|
102
|
|
|
if($isAdmin === true) { |
|
103
|
|
|
$user->setRoles(['ROLE_SUPER_ADMIN']); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$encodedPassword = $this->passwordHasher->hashPassword($user, $password); |
|
107
|
|
|
|
|
108
|
|
|
$user->setPassword($encodedPassword); |
|
109
|
|
|
|
|
110
|
|
|
$this->userRepository->persist($user); |
|
111
|
|
|
|
|
112
|
|
|
$io->success('Benutzer erfolgreich erstellt'); |
|
113
|
|
|
|
|
114
|
|
|
return 0; |
|
115
|
|
|
} |
|
116
|
|
|
} |