|
3
|
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 |
|
|
|
22 |
/** |
|
|
23 |
* @author Matthieu Bontemps <matthieu@knplabs.com> |
|
|
24 |
* @author Thibault Duplessis <thibault.duplessis@gmail.com> |
|
|
25 |
* @author Luis Cordova <cordoval@gmail.com> |
|
|
26 |
*/ |
|
|
27 |
class CreateUserCommand extends ContainerAwareCommand |
|
|
28 |
{ |
|
|
29 |
/** |
|
|
30 |
* @see Command |
|
|
31 |
*/ |
|
|
32 |
protected function configure() |
|
|
33 |
{ |
|
|
34 |
$this |
|
|
35 |
->setName('fos:user:create') |
|
|
36 |
->setDescription('Create a user.') |
|
|
37 |
->setDefinition(array( |
|
|
38 |
new InputArgument('username', InputArgument::REQUIRED, 'The username'), |
|
|
39 |
new InputArgument('email', InputArgument::REQUIRED, 'The email'), |
|
|
40 |
new InputArgument('password', InputArgument::REQUIRED, 'The password'), |
|
|
41 |
new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'), |
|
|
42 |
new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'), |
|
|
43 |
)) |
|
|
44 |
->setHelp(<<<EOT |
|
|
45 |
The <info>fos:user:create</info> command creates a user: |
|
|
46 |
|
|
|
47 |
<info>php app/console fos:user:create matthieu</info> |
|
|
48 |
|
|
|
49 |
This interactive shell will ask you for an email and then a password. |
|
|
50 |
|
|
|
51 |
You can alternatively specify the email and password as the second and third arguments: |
|
|
52 |
|
|
|
53 |
<info>php app/console fos:user:create matthieu matthieu@example.com mypassword</info> |
|
|
54 |
|
|
|
55 |
You can create a super admin via the super-admin flag: |
|
|
56 |
|
|
|
57 |
<info>php app/console fos:user:create admin --super-admin</info> |
|
|
58 |
|
|
|
59 |
You can create an inactive user (will not be able to log in): |
|
|
60 |
|
|
|
61 |
<info>php app/console fos:user:create thibault --inactive</info> |
|
|
62 |
|
|
|
63 |
EOT |
|
|
64 |
); |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
/** |
|
|
68 |
* @see Command |
|
|
69 |
*/ |
|
|
70 |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
71 |
{ |
|
|
72 |
$username = $input->getArgument('username'); |
|
|
73 |
$email = $input->getArgument('email'); |
|
|
74 |
$password = $input->getArgument('password'); |
|
|
75 |
$inactive = $input->getOption('inactive'); |
|
|
76 |
$superadmin = $input->getOption('super-admin'); |
|
|
77 |
|
|
|
78 |
$manipulator = $this->getContainer()->get('fos_user.util.user_manipulator'); |
|
|
79 |
$manipulator->create($username, $password, $email, !$inactive, $superadmin); |
|
|
80 |
|
|
|
81 |
$output->writeln(sprintf('Created user <comment>%s</comment>', $username)); |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
/** |
|
|
85 |
* @see Command |
|
|
86 |
*/ |
|
|
87 |
protected function interact(InputInterface $input, OutputInterface $output) |
|
|
88 |
{ |
|
|
89 |
if (!$input->getArgument('username')) { |
|
|
90 |
$username = $this->getHelper('dialog')->askAndValidate( |
|
|
91 |
$output, |
|
|
92 |
'Please choose a username:', |
|
|
93 |
function($username) |
|
|
94 |
{ |
|
|
95 |
if (empty($username)) { |
|
|
96 |
throw new \Exception('Username can not be empty'); |
|
|
97 |
} |
|
|
98 |
return $username; |
|
|
99 |
} |
|
|
100 |
); |
|
|
101 |
$input->setArgument('username', $username); |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
if (!$input->getArgument('email')) { |
|
|
105 |
$email = $this->getHelper('dialog')->askAndValidate( |
|
|
106 |
$output, |
|
|
107 |
'Please choose an email:', |
|
|
108 |
function($email) |
|
|
109 |
{ |
|
|
110 |
if (empty($email)) { |
|
|
111 |
throw new \Exception('Email can not be empty'); |
|
|
112 |
} |
|
|
113 |
return $email; |
|
|
114 |
} |
|
|
115 |
); |
|
|
116 |
$input->setArgument('email', $email); |
|
|
117 |
} |
|
|
118 |
|
|
|
119 |
if (!$input->getArgument('password')) { |
|
|
120 |
$password = $this->getHelper('dialog')->askAndValidate( |
|
|
121 |
$output, |
|
|
122 |
'Please choose a password:', |
|
|
123 |
function($password) |
|
|
124 |
{ |
|
|
125 |
if (empty($password)) { |
|
|
126 |
throw new \Exception('Password can not be empty'); |
|
|
127 |
} |
|
|
128 |
return $password; |
|
|
129 |
} |
|
|
130 |
); |
|
|
131 |
$input->setArgument('password', $password); |
|
|
132 |
} |
|
|
133 |
} |
|
|
134 |
} |