1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obblm\Core\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Obblm\Core\Entity\Coach; |
7
|
|
|
use Obblm\Core\Event\RegisterCoachEvent; |
8
|
|
|
use Obblm\Core\Security\Roles; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
15
|
|
|
|
16
|
|
|
class CreateAdminCommand extends Command |
17
|
|
|
{ |
18
|
|
|
/** @var string */ |
19
|
|
|
protected static $defaultName = 'obblm:coach:create-admin'; |
20
|
|
|
/** @var EntityManagerInterface */ |
21
|
|
|
private $em; |
22
|
|
|
/** @var SymfonyStyle */ |
23
|
|
|
private $io; |
24
|
|
|
private $passwordEncoder; |
25
|
|
|
private $dispatcher; |
26
|
|
|
|
27
|
|
|
public function __construct(EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder, EventDispatcherInterface $dispatcher) |
28
|
|
|
{ |
29
|
|
|
$this->em = $em; |
30
|
|
|
$this->passwordEncoder = $passwordEncoder; |
31
|
|
|
$this->dispatcher = $dispatcher; |
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function configure():void |
36
|
|
|
{ |
37
|
|
|
$this->setDescription('Creates a new OBBLM Administrator.') |
38
|
|
|
->setHelp('This command will add a new administrator.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
42
|
|
|
{ |
43
|
|
|
$this->io = new SymfonyStyle($input, $output); |
44
|
|
|
|
45
|
|
|
$this->io->title('Create a new OBBLM Administrator'); |
46
|
|
|
$this->io->caution('Be carefull, this new user will have the highest right on the application'); |
47
|
|
|
$continue = $this->io->confirm('Are you sure you want to continue ?', true); |
48
|
|
|
if($continue) { |
49
|
|
|
$coachRepository = $this->em->getRepository(Coach::class); |
50
|
|
|
$this->io->section('User informations'); |
51
|
|
|
$username = $this->io->ask('User login', null, function ($username) use ($coachRepository) { |
52
|
|
|
if (empty($username)) { |
53
|
|
|
throw new \RuntimeException('The login cannot be empty.'); |
54
|
|
|
} |
55
|
|
|
if ($coachRepository->findOneByUsername($username)) { |
56
|
|
|
throw new \RuntimeException('This login is allready used.'); |
57
|
|
|
} |
58
|
|
|
return (string) $username; |
59
|
|
|
}); |
60
|
|
|
$email = $this->io->ask('User email', null, function ($email) use ($coachRepository) { |
61
|
|
|
if (empty($email)) { |
62
|
|
|
throw new \RuntimeException('The email cannot be empty.'); |
63
|
|
|
} |
64
|
|
|
if ($coachRepository->findOneByEmail($email)) { |
65
|
|
|
throw new \RuntimeException('This email is allready used.'); |
66
|
|
|
} |
67
|
|
|
return (string) $email; |
68
|
|
|
}); |
69
|
|
|
$password = $this->io->askHidden('User password', function ($password) { |
70
|
|
|
if (empty($password)) { |
71
|
|
|
throw new \RuntimeException('Password cannot be empty.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $password; |
75
|
|
|
}); |
76
|
|
|
$active = $this->io->confirm('Activate the user (or send him an activation email)', false); |
77
|
|
|
$coach = (new Coach()); |
78
|
|
|
$password = $this->passwordEncoder->encodePassword($coach, $password); |
79
|
|
|
$coach |
80
|
|
|
->setUsername($username) |
81
|
|
|
->setEmail($email) |
82
|
|
|
->setPassword($password) |
83
|
|
|
->setActive($active) |
84
|
|
|
->setLocale('en') |
85
|
|
|
->setRoles([Roles::ADMIN]) |
86
|
|
|
; |
87
|
|
|
if(!$active) { |
88
|
|
|
$coach |
89
|
|
|
->setHash(hash('sha256', $coach->getEmail())); |
90
|
|
|
$registration = new RegisterCoachEvent($coach); |
91
|
|
|
$this->dispatcher->dispatch($registration, RegisterCoachEvent::NAME); |
92
|
|
|
} |
93
|
|
|
$this->io->success("The coach $username has been created !"); |
94
|
|
|
$this->em->persist($coach); |
95
|
|
|
$this->em->flush(); |
96
|
|
|
return 1; |
97
|
|
|
} |
98
|
|
|
$this->io->text('Aborted.'); |
99
|
|
|
return 0; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|