1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FSi\Bundle\AdminSecurityBundle\Command; |
11
|
|
|
|
12
|
|
|
use FSi\Bundle\AdminSecurityBundle\Doctrine\Repository\UserRepository; |
13
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
14
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\UserEvent; |
15
|
|
|
use FSi\Bundle\AdminSecurityBundle\Security\User\UserInterface; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
|
21
|
|
|
class DemoteUserCommand extends ContainerAwareCommand |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @see Command |
25
|
|
|
*/ |
26
|
|
|
protected function configure() |
27
|
|
|
{ |
28
|
|
|
$this |
29
|
|
|
->setName('fsi:user:demote') |
30
|
|
|
->setDescription('Demote a user.') |
31
|
|
|
->setDefinition([ |
32
|
|
|
new InputArgument('email', InputArgument::REQUIRED, 'The email'), |
33
|
|
|
new InputArgument('role', InputArgument::REQUIRED, 'The role'), |
34
|
|
|
]) |
35
|
|
|
->setHelp(<<<EOT |
36
|
|
|
The <info>fsi:user:demote</info> command demotes the user |
37
|
|
|
|
38
|
|
|
<info>php app/console fsi:user:demote [email protected] ROLE_ADMIN</info> |
39
|
|
|
|
40
|
|
|
EOT |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @see Command |
46
|
|
|
*/ |
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
48
|
|
|
{ |
49
|
|
|
$email = $input->getArgument('email'); |
50
|
|
|
$role = $input->getArgument('role'); |
51
|
|
|
|
52
|
|
|
/** @var UserRepository $userRepository */ |
53
|
|
|
$userRepository = $this->getContainer()->get('admin_security.repository.user'); |
54
|
|
|
$user = $userRepository->findUserByEmail($email); |
55
|
|
|
if (!($user instanceof UserInterface)) { |
56
|
|
|
throw new \InvalidArgumentException(sprintf('User with email "%s" cannot be found', $email)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$user->removeRole($role); |
60
|
|
|
|
61
|
|
|
$this->getContainer()->get('event_dispatcher')->dispatch( |
62
|
|
|
AdminSecurityEvents::DEMOTE_USER, |
63
|
|
|
new UserEvent($user) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$output->writeln(sprintf('User <comment>%s</comment> has been demoted', $email)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @see Command |
71
|
|
|
*/ |
72
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
73
|
|
|
{ |
74
|
|
|
if (!$input->getArgument('email')) { |
75
|
|
|
$email = $this->getHelper('dialog')->askAndValidate( |
76
|
|
|
$output, |
77
|
|
|
'Please choose an email:', |
78
|
|
|
function($email) { |
79
|
|
|
if (empty($email)) { |
80
|
|
|
throw new \Exception('Email can not be empty'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $email; |
84
|
|
|
} |
85
|
|
|
); |
86
|
|
|
$input->setArgument('email', $email); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!$input->getArgument('role')) { |
90
|
|
|
$email = $this->getHelper('dialog')->askAndValidate( |
91
|
|
|
$output, |
92
|
|
|
'Please choose a role:', |
93
|
|
|
function($email) { |
94
|
|
|
if (empty($email)) { |
95
|
|
|
throw new \Exception('Role can not be empty'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $email; |
99
|
|
|
} |
100
|
|
|
); |
101
|
|
|
$input->setArgument('role', $email); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|