|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
|
|
12
|
|
|
class BiCoreBundleClonaruoloCommand extends Command |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
protected static $defaultName = 'bicorebundle:clonaruolo'; |
|
16
|
|
|
private EntityManagerInterface $em; |
|
17
|
|
|
|
|
18
|
1 |
|
protected function configure(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$this |
|
21
|
1 |
|
->setDescription('Clona i permessi di un ruolo esistente su un nuovo ruolo') |
|
22
|
1 |
|
->setHelp('Specificare come parametri il nome del ruolo da clonare e quello nuovo') |
|
23
|
1 |
|
->addArgument('ruoloesistente', InputArgument::REQUIRED, 'Ruolo esistente') |
|
24
|
1 |
|
->addArgument('nuovoruolo', InputArgument::REQUIRED, 'Nuovo ruolo') |
|
25
|
|
|
; |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function __construct(EntityManagerInterface $em) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$this->em = $em; |
|
31
|
|
|
|
|
32
|
|
|
// you *must* call the parent constructor |
|
33
|
1 |
|
parent::__construct(); |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
37
|
|
|
{ |
|
38
|
1 |
|
$ruoloesistente = $input->getArgument('ruoloesistente'); |
|
39
|
1 |
|
$nuovoruolo = $input->getArgument('nuovoruolo'); |
|
40
|
|
|
|
|
41
|
1 |
|
if (!$ruoloesistente) { |
|
42
|
|
|
throw new Exception('Inserire il ruolo da clonare'); |
|
43
|
|
|
} |
|
44
|
1 |
|
if (!$nuovoruolo) { |
|
45
|
|
|
throw new Exception('Inserire il nuvo ruolo'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$query = $this->em->createQueryBuilder() |
|
49
|
1 |
|
->select('r') |
|
50
|
1 |
|
->from('BiCoreBundle:Ruoli', 'r') |
|
51
|
1 |
|
->where('r.ruolo = :ruolo') |
|
52
|
1 |
|
->setParameter('ruolo', $ruoloesistente) |
|
53
|
1 |
|
->getQuery() |
|
54
|
|
|
; |
|
55
|
1 |
|
$ruoloesistenteobj = $query->getResult(); |
|
56
|
1 |
|
if (!$ruoloesistenteobj) { |
|
57
|
|
|
throw new Exception('Non esiste il ruolo ' . $ruoloesistente); |
|
58
|
|
|
} else { |
|
59
|
1 |
|
$newruoloesistente = $ruoloesistenteobj[0]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
$query = $this->em->createQueryBuilder() |
|
63
|
1 |
|
->select('r') |
|
64
|
1 |
|
->from('BiCoreBundle:Ruoli', 'r') |
|
65
|
1 |
|
->where('r.ruolo = :ruolo') |
|
66
|
1 |
|
->setParameter('ruolo', $nuovoruolo) |
|
67
|
1 |
|
->getQuery() |
|
68
|
|
|
; |
|
69
|
1 |
|
$nuovruoloobj = $query->getResult(); |
|
70
|
1 |
|
if ($nuovruoloobj) { |
|
71
|
|
|
throw new Exception('Esiste già il ruolo ' . $nuovoruolo); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
$output->writeln('<info>Inizio clonazione del ruolo ' . $ruoloesistente . ' in ' . $nuovoruolo . '</info>'); |
|
75
|
1 |
|
$newnuovoruolo = clone $newruoloesistente; |
|
76
|
1 |
|
$newnuovoruolo->setRuolo($nuovoruolo); |
|
77
|
1 |
|
$this->em->persist($newnuovoruolo); |
|
78
|
1 |
|
$this->em->flush(); |
|
79
|
|
|
|
|
80
|
1 |
|
$query = $this->em->createQueryBuilder() |
|
81
|
1 |
|
->select('p') |
|
82
|
1 |
|
->from('BiCoreBundle:Permessi', 'p') |
|
83
|
1 |
|
->where('p.ruoli = :ruolo') |
|
84
|
1 |
|
->setParameter('ruolo', $newruoloesistente) |
|
85
|
1 |
|
->getQuery() |
|
86
|
|
|
; |
|
87
|
1 |
|
$permessiobj = $query->getResult(); |
|
88
|
1 |
|
foreach ($permessiobj as $permesso) { |
|
89
|
1 |
|
$newpermessi = clone $permesso; |
|
90
|
1 |
|
$newpermessi->setRuoli($newnuovoruolo); |
|
91
|
1 |
|
$this->em->persist($newpermessi); |
|
92
|
1 |
|
$this->em->flush(); |
|
93
|
|
|
} |
|
94
|
1 |
|
return 0; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|