|
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
|
|
|
|
|
14
|
|
|
class PromoteCoachCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected static $defaultName = 'obblm:coach:promote'; |
|
18
|
|
|
/** @var EntityManagerInterface */ |
|
19
|
|
|
private $em; |
|
20
|
|
|
/** @var SymfonyStyle */ |
|
21
|
|
|
private $io; |
|
22
|
|
|
private $passwordEncoder; |
|
|
|
|
|
|
23
|
|
|
private $dispatcher; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public function __construct(EntityManagerInterface $em) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->em = $em; |
|
28
|
|
|
parent::__construct(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function configure():void |
|
32
|
|
|
{ |
|
33
|
|
|
$this->setDescription('Promotes an existing user to OBBLM Administrator.') |
|
34
|
|
|
->setHelp('This command will promote an existing user to the administrator role.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->io = new SymfonyStyle($input, $output); |
|
40
|
|
|
|
|
41
|
|
|
$this->io->title('Promote a new OBBLM Administrator'); |
|
42
|
|
|
$this->io->caution('Be carefull, this new user will have the highest right on the application'); |
|
43
|
|
|
$continue = $this->io->confirm('Are you sure you want to continue ?', true); |
|
44
|
|
|
if($continue) { |
|
45
|
|
|
$coachRepository = $this->em->getRepository(Coach::class); |
|
46
|
|
|
|
|
47
|
|
|
$choice = $this->io->choice('Search him by login or email ', ['login', 'email']); |
|
48
|
|
|
|
|
49
|
|
|
if($choice !== 'login' && $choice !== 'email') { |
|
50
|
|
|
throw new \RuntimeException("Something went wrong."); |
|
51
|
|
|
return 0; |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
elseif($choice === 'login') { |
|
54
|
|
|
$coach = $this->io->ask('User login', null, function ($username) use ($coachRepository) { |
|
55
|
|
|
if (empty($username)) { |
|
56
|
|
|
throw new \RuntimeException('The login cannot be empty.'); |
|
57
|
|
|
} |
|
58
|
|
|
$coach = $coachRepository->findOneByUsername($username); |
|
59
|
|
|
if (!$coach) { |
|
60
|
|
|
throw new \RuntimeException("This login doesn't exist."); |
|
61
|
|
|
} |
|
62
|
|
|
return $coach; |
|
63
|
|
|
}); |
|
64
|
|
|
} |
|
65
|
|
|
elseif ($choice === 'email') { |
|
66
|
|
|
$coach = $this->io->ask('User email', null, function ($email) use ($coachRepository) { |
|
67
|
|
|
if (empty($email)) { |
|
68
|
|
|
throw new \RuntimeException('The email cannot be empty.'); |
|
69
|
|
|
} |
|
70
|
|
|
$coach = $coachRepository->findOneByEmail($email); |
|
71
|
|
|
if (!$coach) { |
|
72
|
|
|
throw new \RuntimeException("This email doesn't exist."); |
|
73
|
|
|
} |
|
74
|
|
|
return $coach; |
|
75
|
|
|
}); |
|
76
|
|
|
} |
|
77
|
|
|
/** @var Coach $coach */ |
|
78
|
|
|
$continue = $this->io->confirm("Are you sure you want to promote {$coach->getUsername()} ?", true); |
|
|
|
|
|
|
79
|
|
|
if($continue) { |
|
80
|
|
|
$coach |
|
81
|
|
|
->setRoles([Roles::ADMIN]); |
|
82
|
|
|
$this->em->persist($coach); |
|
83
|
|
|
$this->em->flush(); |
|
84
|
|
|
$this->io->success("The coach {$coach->getUsername()} has been promoted !"); |
|
85
|
|
|
return 1; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
$this->io->text('Aborted.'); |
|
89
|
|
|
return 0; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|