1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license MIT, http://opensource.org/licenses/MIT |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2016 |
6
|
|
|
* @package symfony |
7
|
|
|
* @subpackage Command |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\ShopBundle\Command; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Console\Question\Question; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Creates new accounts or resets their passwords |
22
|
|
|
* @package symfony |
23
|
|
|
* @subpackage Command |
24
|
|
|
*/ |
25
|
|
|
class AccountCommand extends Command |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Configures the command name and description. |
29
|
|
|
*/ |
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this->setName( 'aimeos:account'); |
33
|
|
|
$this->setDescription( 'Creates new (admin) accounts' ); |
34
|
|
|
$this->addArgument( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' ); |
35
|
|
|
$this->addArgument( 'site', InputArgument::OPTIONAL, 'Site codes to create accounts for like "default"', 'default' ); |
36
|
|
|
$this->addOption( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' ); |
37
|
|
|
$this->addOption( 'super', null, InputOption::VALUE_NONE, 'If account should have super user privileges' ); |
38
|
|
|
$this->addOption( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' ); |
39
|
|
|
$this->addOption( 'editor', null, InputOption::VALUE_NONE, 'If account should have limited editor privileges' ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Execute the console command. |
45
|
|
|
* |
46
|
|
|
* @param InputInterface $input Input object |
47
|
|
|
* @param OutputInterface $output Output object |
48
|
|
|
*/ |
49
|
|
|
protected function execute( InputInterface $input, OutputInterface $output ) |
50
|
|
|
{ |
51
|
|
|
$code = $input->getArgument( 'email' ); |
52
|
|
|
if( ( $password = $input->getOption( 'password' ) ) === null ) |
53
|
|
|
{ |
54
|
|
|
$helper = $this->getHelper( 'question' ); |
55
|
|
|
$question = new Question( 'Password' ); |
56
|
|
|
$question->setHidden( true ); |
57
|
|
|
|
58
|
|
|
$password = $helper->ask( $input, $output, $question ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$context = $this->getContainer()->get( 'aimeos_context' )->get( false, 'command' ); |
62
|
|
|
$context->setEditor( 'aimeos:account' ); |
63
|
|
|
|
64
|
|
|
$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context ); |
65
|
|
|
$localeItem = $localeManager->bootstrap( $input->getArgument( 'site' ), '', '', false ); |
66
|
|
|
$context->setLocale( $localeItem ); |
67
|
|
|
|
68
|
|
|
$user = $this->createCustomerItem( $context, $code, $password ); |
69
|
|
|
|
70
|
|
|
if( $input->getOption( 'admin' ) ) { |
71
|
|
|
$this->addGroup( $input, $output, $context, $user, 'admin' ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if( $input->getOption( 'editor' ) ) { |
75
|
|
|
$this->addGroup( $input, $output, $context, $user, 'editor' ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if( $this->getContainer()->has( 'fos_user.user_manager' ) ) |
79
|
|
|
{ |
80
|
|
|
$userManager = $this->getContainer()->get( 'fos_user.user_manager' ); |
81
|
|
|
|
82
|
|
|
if( $input->getOption( 'super' ) ) { |
83
|
|
|
$userManager->promote( $code ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if( $input->getOption( 'admin' ) || $input->getOption( 'editor' ) ) { |
87
|
|
|
$userManager->addRole( $code, 'ROLE_ADMIN' ); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Adds the group to the given user |
95
|
|
|
* |
96
|
|
|
* @param InputInterface $input Input object |
97
|
|
|
* @param OutputInterface $output Output object |
98
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
99
|
|
|
* @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
100
|
|
|
* @param string $group Unique customer group code |
101
|
|
|
*/ |
102
|
|
|
protected function addGroup( InputInterface $input, OutputInterface $output, |
103
|
|
|
\Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $group ) |
104
|
|
|
{ |
105
|
|
|
$output->writeln( sprintf( 'Add "%1$s" group to user "%2$s" for sites', $group, $user->getCode() ) ); |
106
|
|
|
|
107
|
|
|
$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context ); |
108
|
|
|
|
109
|
|
|
foreach( $this->getSiteItems( $context, $input ) as $siteItem ) |
110
|
|
|
{ |
111
|
|
|
$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false ); |
112
|
|
|
|
113
|
|
|
$lcontext = clone $context; |
114
|
|
|
$lcontext->setLocale( $localeItem ); |
115
|
|
|
|
116
|
|
|
$output->writeln( '- ' . $siteItem->getCode() ); |
117
|
|
|
|
118
|
|
|
$groupItem = $this->getGroupItem( $lcontext, $group ); |
119
|
|
|
$this->addListItem( $lcontext, $user->getId(), $groupItem->getId() ); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Associates the user to the group by their given IDs |
126
|
|
|
* |
127
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
128
|
|
|
* @param string $userid Unique user ID |
129
|
|
|
* @param string $groupid Unique group ID |
130
|
|
|
*/ |
131
|
|
|
protected function addListItem( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupid ) |
132
|
|
|
{ |
133
|
|
|
$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'lists' ); |
134
|
|
|
$typeid = $manager->getSubmanager( 'type' )->findItem( 'default', array(), 'customer/group' )->getId(); |
135
|
|
|
|
136
|
|
|
$search = $manager->createSearch(); |
137
|
|
|
$expr = array( |
138
|
|
|
$search->compare( '==', 'customer.lists.parentid', $userid ), |
139
|
|
|
$search->compare( '==', 'customer.lists.refid', $groupid ), |
140
|
|
|
$search->compare( '==', 'customer.lists.domain', 'customer/group' ), |
141
|
|
|
$search->compare( '==', 'customer.lists.typeid', $typeid ), |
142
|
|
|
); |
143
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
144
|
|
|
$search->setSlice( 0, 1 ); |
145
|
|
|
|
146
|
|
|
if( count( $manager->searchItems( $search ) ) === 0 ) |
147
|
|
|
{ |
148
|
|
|
$item = $manager->createItem(); |
149
|
|
|
$item->setDomain( 'customer/group' ); |
150
|
|
|
$item->setParentId( $userid ); |
151
|
|
|
$item->setTypeId( $typeid ); |
152
|
|
|
$item->setRefId( $groupid ); |
153
|
|
|
$item->setStatus( 1 ); |
154
|
|
|
|
155
|
|
|
$manager->saveItem( $item, false ); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns the customer item for the given e-mail and set its password |
162
|
|
|
* |
163
|
|
|
* If the customer doesn't exist yet, it will be created. |
164
|
|
|
* |
165
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
166
|
|
|
* @param string $email Unique e-mail address |
167
|
|
|
* @param string $password New user password |
168
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Aimeos customer item object |
169
|
|
|
*/ |
170
|
|
|
protected function createCustomerItem( \Aimeos\MShop\Context\Item\Iface $context, $email, $password ) |
171
|
|
|
{ |
172
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' ); |
173
|
|
|
|
174
|
|
|
try { |
175
|
|
|
$item = $manager->findItem( $email ); |
176
|
|
|
} catch( \Aimeos\MShop\Exception $e ) { |
177
|
|
|
$item = $manager->createItem(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$item->setCode( $email ); |
181
|
|
|
$item->setLabel( $email ); |
182
|
|
|
$item->getPaymentAddress()->setEmail( $email ); |
183
|
|
|
$item->setPassword( $password ); |
184
|
|
|
$item->setStatus( 1 ); |
185
|
|
|
|
186
|
|
|
$manager->saveItem( $item ); |
187
|
|
|
|
188
|
|
|
return $item; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns the customer group item for the given code |
194
|
|
|
* |
195
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
196
|
|
|
* @param string $code Unique customer group code |
197
|
|
|
* @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object |
198
|
|
|
*/ |
199
|
|
|
protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code ) |
200
|
|
|
{ |
201
|
|
|
$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'group' ); |
202
|
|
|
|
203
|
|
|
try |
204
|
|
|
{ |
205
|
|
|
$item = $manager->findItem( $code ); |
206
|
|
|
} |
207
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
208
|
|
|
{ |
209
|
|
|
$item = $manager->createItem(); |
210
|
|
|
$item->setLabel( $code ); |
211
|
|
|
$item->setCode( $code ); |
212
|
|
|
|
213
|
|
|
$manager->saveItem( $item ); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $item; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|