|
13 | 13 |
|
14 | 14 | use App\Entity\User;
|
15 | 15 | use App\Repository\UserRepository;
|
| 16 | +use App\User\UserFactory; |
16 | 17 | use App\Utils\Validator;
|
17 | 18 | use Doctrine\ORM\EntityManagerInterface;
|
18 | 19 | use Symfony\Component\Console\Command\Command;
|
|
22 | 23 | use Symfony\Component\Console\Input\InputOption;
|
23 | 24 | use Symfony\Component\Console\Output\OutputInterface;
|
24 | 25 | use Symfony\Component\Console\Style\SymfonyStyle;
|
25 |
| -use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
26 | 26 | use Symfony\Component\Stopwatch\Stopwatch;
|
27 | 27 |
|
28 | 28 | /**
|
@@ -56,16 +56,16 @@ class AddUserCommand extends Command
|
56 | 56 | private $io;
|
57 | 57 |
|
58 | 58 | private $entityManager;
|
59 |
| - private $passwordEncoder; |
| 59 | + private $userFactory; |
60 | 60 | private $validator;
|
61 | 61 | private $users;
|
62 | 62 |
|
63 |
| - public function __construct(EntityManagerInterface $em, UserPasswordEncoderInterface $encoder, Validator $validator, UserRepository $users) |
| 63 | + public function __construct(EntityManagerInterface $em, UserFactory $userFactory, Validator $validator, UserRepository $users) |
64 | 64 | {
|
65 | 65 | parent::__construct();
|
66 | 66 |
|
67 | 67 | $this->entityManager = $em;
|
68 |
| - $this->passwordEncoder = $encoder; |
| 68 | + $this->userFactory = $userFactory; |
69 | 69 | $this->validator = $validator;
|
70 | 70 | $this->users = $users;
|
71 | 71 | }
|
@@ -181,16 +181,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
|
181 | 181 | // make sure to validate the user data is correct
|
182 | 182 | $this->validateUserData($username, $plainPassword, $email, $fullName);
|
183 | 183 |
|
184 |
| - // create the user and encode its password |
185 |
| - $user = new User(); |
186 |
| - $user->setFullName($fullName); |
187 |
| - $user->setUsername($username); |
188 |
| - $user->setEmail($email); |
189 |
| - $user->setRoles([$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER']); |
190 |
| - |
191 |
| - // See https://symfony.com/doc/current/book/security.html#security-encoding-password |
192 |
| - $encodedPassword = $this->passwordEncoder->encodePassword($user, $plainPassword); |
193 |
| - $user->setPassword($encodedPassword); |
| 184 | + $user = $this->userFactory->createUser($username, $email, $fullName, $plainPassword, [$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER']); |
194 | 185 |
|
195 | 186 | $this->entityManager->persist($user);
|
196 | 187 | $this->entityManager->flush();
|
|
0 commit comments