forked from cleverage/processuibundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserCreateCommand.php
121 lines (104 loc) · 4.17 KB
/
UserCreateCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/UiProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\UiProcessBundle\Command;
use CleverAge\UiProcessBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Validator\ValidatorInterface;
#[AsCommand(
name: 'cleverage:ui-process:user-create',
description: 'Command to create a new admin into database for ui process.'
)]
class UserCreateCommand extends Command
{
public function __construct(
private readonly ValidatorInterface $validator,
private readonly UserPasswordHasherInterface $passwordEncoder,
private readonly EntityManagerInterface $em,
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('email', InputArgument::OPTIONAL, 'Email address')
->addArgument('password', InputArgument::OPTIONAL, 'Password')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$style = new SymfonyStyle($input, $output);
if (!empty($input->getArgument('email'))) {
$username = $input->getArgument('email');
} else {
$username = $this->ask('Please enter the email.', $style, [new Email()]);
}
if (!empty($input->getArgument('password'))) {
$password = $input->getArgument('password');
} else {
$password = $this->askPassword(
(new Question('Please enter the user password.'))->setHidden(true)->setHiddenFallback(false),
$input,
$output
);
}
$user = new User();
$user->setEmail($username);
$user->setRoles(['ROLE_USER', 'ROLE_ADMIN']);
$user->setPassword($this->passwordEncoder->hashPassword($user, $password));
$this->em->persist($user);
$this->em->flush();
$style->writeln('<info>User created.</info>');
return Command::SUCCESS;
}
/**
* @param Constraint[] $constraints
*/
private function ask(string $question, SymfonyStyle $style, array $constraints = []): mixed
{
$value = $style->ask($question);
$violations = $this->validator->validate($value, $constraints);
while ($violations->count() > 0) {
$violationsMessage = $violations->get(0)->getMessage();
$style->writeln("<error>$violationsMessage</error>");
$value = $style->ask($question);
$violations = $this->validator->validate($value, $constraints);
}
return $value;
}
private function askPassword(Question $question, InputInterface $input, OutputInterface $output): mixed
{
$constraints = [new NotBlank(), new Length(min: 8)];
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
$password = $helper->ask($input, $output, $question);
$violations = $this->validator->validate($password, $constraints);
while ($violations->count() > 0) {
$violationsMessage = $violations->get(0)->getMessage();
$output->writeln("<error>$violationsMessage</error>");
$password = $helper->ask($input, $output, $question);
$violations = $this->validator->validate($password, $constraints);
}
return $password;
}
}