|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the FOSUserBundle package. |
|
5 * |
|
6 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace FOS\UserBundle\Command; |
|
13 |
|
14 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
15 use Symfony\Component\Console\Input\InputArgument; |
|
16 use Symfony\Component\Console\Input\InputOption; |
|
17 use Symfony\Component\Console\Input\InputInterface; |
|
18 use Symfony\Component\Console\Output\OutputInterface; |
|
19 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
20 use FOS\UserBundle\Model\User; |
|
21 use FOS\UserBundle\Util\UserManipulator; |
|
22 |
|
23 /** |
|
24 * @author Lenar Lõhmus <lenar@city.ee> |
|
25 */ |
|
26 abstract class RoleCommand extends ContainerAwareCommand |
|
27 { |
|
28 /** |
|
29 * @see Command |
|
30 */ |
|
31 protected function configure() |
|
32 { |
|
33 $this |
|
34 ->setDefinition(array( |
|
35 new InputArgument('username', InputArgument::REQUIRED, 'The username'), |
|
36 new InputArgument('role', InputArgument::OPTIONAL, 'The role'), |
|
37 new InputOption('super', null, InputOption::VALUE_NONE, 'Instead specifying role, use this to quickly add the super administrator role'), |
|
38 )); |
|
39 } |
|
40 |
|
41 /** |
|
42 * @see Command |
|
43 */ |
|
44 protected function execute(InputInterface $input, OutputInterface $output) |
|
45 { |
|
46 $username = $input->getArgument('username'); |
|
47 $role = $input->getArgument('role'); |
|
48 $super = (true === $input->getOption('super')); |
|
49 |
|
50 if (null !== $role && $super) { |
|
51 throw new \InvalidArgumentException('You can pass either the role or the --super option (but not both simultaneously).'); |
|
52 } |
|
53 |
|
54 if (null === $role && !$super) { |
|
55 throw new \RuntimeException('Not enough arguments.'); |
|
56 } |
|
57 |
|
58 $manipulator = $this->getContainer()->get('fos_user.util.user_manipulator'); |
|
59 $this->executeRoleCommand($manipulator, $output, $username, $super, $role); |
|
60 } |
|
61 |
|
62 /** |
|
63 * @see Command |
|
64 */ |
|
65 abstract protected function executeRoleCommand(UserManipulator $manipulator, OutputInterface $output, $username, $super, $role); |
|
66 |
|
67 /** |
|
68 * @see Command |
|
69 */ |
|
70 protected function interact(InputInterface $input, OutputInterface $output) |
|
71 { |
|
72 if (!$input->getArgument('username')) { |
|
73 $username = $this->getHelper('dialog')->askAndValidate( |
|
74 $output, |
|
75 'Please choose a username:', |
|
76 function($username) |
|
77 { |
|
78 if (empty($username)) { |
|
79 throw new \Exception('Username can not be empty'); |
|
80 } |
|
81 return $username; |
|
82 } |
|
83 ); |
|
84 $input->setArgument('username', $username); |
|
85 } |
|
86 if ((true !== $input->getOption('super')) && !$input->getArgument('role')) { |
|
87 $role = $this->getHelper('dialog')->askAndValidate( |
|
88 $output, |
|
89 'Please choose a role:', |
|
90 function($role) |
|
91 { |
|
92 if (empty($role)) { |
|
93 throw new \Exception('Role can not be empty'); |
|
94 } |
|
95 return $role; |
|
96 } |
|
97 ); |
|
98 $input->setArgument('role', $role); |
|
99 } |
|
100 } |
|
101 } |