1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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 Sylius\Bundle\UserBundle\Command; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
15
|
|
|
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; |
16
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
17
|
|
|
use Sylius\Component\Core\Model\UserInterface; |
18
|
|
|
use Sylius\Component\Rbac\Model\RoleInterface; |
19
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
21
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
22
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
23
|
|
|
use Symfony\Component\Console\Input\InputOption; |
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Aram Alipoor <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class CreateUserCommand extends ContainerAwareCommand |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
protected function configure() |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setName('sylius:user:create') |
38
|
|
|
->setDescription('Creates a new user account.') |
39
|
|
|
->setDefinition(array( |
40
|
|
|
new InputArgument('email', InputArgument::REQUIRED, 'Email'), |
41
|
|
|
new InputArgument('password', InputArgument::REQUIRED, 'Password'), |
42
|
|
|
new InputArgument('roles', InputArgument::IS_ARRAY, 'RBAC roles'), |
43
|
|
|
new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as a super admin'), |
44
|
|
|
new InputOption('disabled', null, InputOption::VALUE_NONE, 'Set the user as a disabled user'), |
45
|
|
|
)) |
46
|
|
|
->setHelp(<<<EOT |
47
|
|
|
The <info>%command.name%</info> command creates a new user account. |
48
|
|
|
EOT |
49
|
|
|
) |
50
|
|
|
; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
57
|
|
|
{ |
58
|
|
|
$email = $input->getArgument('email'); |
59
|
|
|
$password = $input->getArgument('password'); |
60
|
|
|
$roles = $input->getArgument('roles'); |
61
|
|
|
$superAdmin = $input->getOption('super-admin'); |
62
|
|
|
$disabled = $input->getOption('disabled'); |
63
|
|
|
|
64
|
|
|
$securityRoles = ['ROLE_USER']; |
65
|
|
|
if ($superAdmin) { |
66
|
|
|
$securityRoles[] = 'ROLE_ADMINISTRATION_ACCESS'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$user = $this->createUser( |
70
|
|
|
$email, |
71
|
|
|
$password, |
72
|
|
|
!$disabled, |
73
|
|
|
$roles, |
74
|
|
|
$securityRoles |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->getEntityManager()->persist($user); |
78
|
|
|
$this->getEntityManager()->flush(); |
79
|
|
|
|
80
|
|
|
$output->writeln(sprintf('Created user <comment>%s</comment>', $email)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
87
|
|
|
{ |
88
|
|
|
if (!$input->getArgument('email')) { |
89
|
|
|
$email = $this->getHelper('dialog')->askAndValidate( |
90
|
|
|
$output, |
91
|
|
|
'Please enter an email:', |
92
|
|
|
function($username) { |
93
|
|
|
if (empty($username)) { |
94
|
|
|
throw new \Exception('Email can not be empty'); |
95
|
|
|
} |
96
|
|
|
return $username; |
97
|
|
|
} |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$input->setArgument('email', $email); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!$input->getArgument('password')) { |
104
|
|
|
$password = $this->getHelper('dialog')->askHiddenResponseAndValidate( |
105
|
|
|
$output, |
106
|
|
|
'Please choose a password:', |
107
|
|
|
function($password) { |
108
|
|
|
if (empty($password)) { |
109
|
|
|
throw new \Exception('Password can not be empty'); |
110
|
|
|
} |
111
|
|
|
return $password; |
112
|
|
|
} |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$input->setArgument('password', $password); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!$input->getArgument('roles')) { |
119
|
|
|
$roles = $this->getHelper('dialog')->ask( |
120
|
|
|
$output, |
121
|
|
|
'Please enter user\'s roles (separated by space):' |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
if (!empty($roles)) { |
125
|
|
|
$input->setArgument('roles', explode(' ', $roles)); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $email |
132
|
|
|
* @param string $password |
133
|
|
|
* @param bool $enabled |
134
|
|
|
* @param array $roles |
135
|
|
|
* @param array $securityRoles |
136
|
|
|
* |
137
|
|
|
* @return UserInterface |
138
|
|
|
*/ |
139
|
|
|
protected function createUser($email, $password, $enabled, array $roles, array $securityRoles = array('ROLE_USER')) |
140
|
|
|
{ |
141
|
|
|
$canonicalizer = $this->getContainer()->get('sylius.user.canonicalizer'); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @var $user UserInterface |
145
|
|
|
* @var $customer CustomerInterface |
146
|
|
|
*/ |
147
|
|
|
$user = $this->getUserFactory()->createNew(); |
148
|
|
|
$customer = $this->getCustomerFactory()->createNew(); |
149
|
|
|
$user->setCustomer($customer); |
150
|
|
|
$user->setUsername($email); |
151
|
|
|
$user->setEmail($email); |
152
|
|
|
$user->setUsernameCanonical($canonicalizer->canonicalize($user->getUsername())); |
153
|
|
|
$user->setEmailCanonical($canonicalizer->canonicalize($user->getEmail())); |
154
|
|
|
$user->setPlainPassword($password); |
155
|
|
|
$user->setRoles($securityRoles); |
156
|
|
|
$user->setEnabled($enabled); |
157
|
|
|
|
158
|
|
|
$roleRepository = $this->getContainer()->get('sylius.repository.role'); |
159
|
|
|
foreach ($roles as $code) { |
160
|
|
|
/** @var RoleInterface $role */ |
161
|
|
|
$role = $roleRepository->findOneBy(array('code' => $code)); |
162
|
|
|
|
163
|
|
|
if (null === $role) { |
164
|
|
|
throw new \InvalidArgumentException( |
165
|
|
|
sprintf('No role with code `%s` does not exist.', $code) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$user->addAuthorizationRole($role); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->getContainer()->get('sylius.user.password_updater')->updatePassword($user); |
173
|
|
|
|
174
|
|
|
return $user; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return EntityManagerInterface |
179
|
|
|
*/ |
180
|
|
|
protected function getEntityManager() |
181
|
|
|
{ |
182
|
|
|
return $this->getContainer()->get('doctrine.orm.entity_manager'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return FactoryInterface |
187
|
|
|
*/ |
188
|
|
|
protected function getUserFactory() |
189
|
|
|
{ |
190
|
|
|
return $this->getContainer()->get('sylius.factory.user'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return FactoryInterface |
195
|
|
|
*/ |
196
|
|
|
protected function getCustomerFactory() |
197
|
|
|
{ |
198
|
|
|
return $this->getContainer()->get('sylius.factory.customer'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return EntityRepository |
203
|
|
|
*/ |
204
|
|
|
protected function getRoleRepository() |
205
|
|
|
{ |
206
|
|
|
return $this->getContainer()->get('sylius.repository.role'); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: