1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license MIT, http://opensource.org/licenses/MIT |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Shop\Command; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Creates new accounts or resets their passwords |
17
|
|
|
*/ |
18
|
|
|
class AccountCommand extends AbstractCommand |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The console command name. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $name = 'aimeos:account'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Creates new (admin) accounts'; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the console command. |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function fire() |
41
|
|
|
{ |
42
|
|
|
$context = $this->getLaravel()->make( 'Aimeos\Shop\Base\Context' )->get( false ); |
43
|
|
|
$context->setEditor( 'aimeos:account' ); |
44
|
|
|
|
45
|
|
|
$code = $this->argument( 'email' ); |
46
|
|
|
if( ( $password = $this->option( 'password' ) ) === null ) { |
47
|
|
|
$password = $this->secret( 'Password' ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$user = $this->getCustomerItem( $context, $code, $password ); |
|
|
|
|
51
|
|
|
|
52
|
|
|
if( $this->option( 'admin' ) ) { |
53
|
|
|
$this->addPrivilege( $context, $user, 'admin' ); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Associates the user to the group by their given IDs |
60
|
|
|
* |
61
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
62
|
|
|
* @param string $userid Unique user ID |
63
|
|
|
* @param string $groupid Unique group ID |
64
|
|
|
*/ |
65
|
|
|
protected function addListItem( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupid ) |
66
|
|
|
{ |
67
|
|
|
$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'lists' ); |
68
|
|
|
$typeid = $this->getTypeItem( $manager, 'default' )->getId(); |
69
|
|
|
|
70
|
|
|
$search = $manager->createSearch(); |
71
|
|
|
$expr = array( |
72
|
|
|
$search->compare( '==', 'customer.lists.parentid', $userid ), |
73
|
|
|
$search->compare( '==', 'customer.lists.refid', $groupid ), |
74
|
|
|
$search->compare( '==', 'customer.lists.domain', 'customer/group' ), |
75
|
|
|
$search->compare( '==', 'customer.lists.typeid', $typeid ), |
76
|
|
|
); |
77
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
78
|
|
|
$search->setSlice( 0, 1 ); |
79
|
|
|
|
80
|
|
|
if( count( $manager->searchItems( $search ) ) === 0 ) |
81
|
|
|
{ |
82
|
|
|
$item = $manager->createItem(); |
83
|
|
|
$item->setDomain( 'customer/group' ); |
84
|
|
|
$item->setParentId( $userid ); |
85
|
|
|
$item->setTypeId( $typeid ); |
86
|
|
|
$item->setRefId( $groupid ); |
87
|
|
|
$item->setStatus( 1 ); |
88
|
|
|
|
89
|
|
|
$manager->saveItem( $item, false ); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds the privilege to the given user |
96
|
|
|
* |
97
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
98
|
|
|
* @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
99
|
|
|
* @param string $privilege Unique customer group code |
100
|
|
|
*/ |
101
|
|
|
protected function addPrivilege( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $privilege ) |
102
|
|
|
{ |
103
|
|
|
$this->info( sprintf( 'Add "%1$s" privilege to user "%2$s" for sites', $privilege, $user->getCode() ) ); |
104
|
|
|
|
105
|
|
|
$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context ); |
106
|
|
|
|
107
|
|
|
foreach( $this->getSiteItems( $context, $this->argument( 'site' ) ) as $siteItem ) |
108
|
|
|
{ |
109
|
|
|
$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false ); |
110
|
|
|
|
111
|
|
|
$lcontext = clone $context; |
112
|
|
|
$lcontext->setLocale( $localeItem ); |
113
|
|
|
|
114
|
|
|
$this->info( $siteItem->getCode() ); |
115
|
|
|
|
116
|
|
|
$groupItem = $this->getGroupItem( $lcontext, $privilege ); |
117
|
|
|
$this->addListItem( $lcontext, $user->getId(), $groupItem->getId() ); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the console command arguments. |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
protected function getArguments() |
128
|
|
|
{ |
129
|
|
|
return array( |
130
|
|
|
array( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' ), |
131
|
|
|
array( 'site', InputArgument::OPTIONAL, 'Site codes to create accounts for like "default unittest" (none for all)' ), |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns the customer item for the given e-mail and set its password |
138
|
|
|
* |
139
|
|
|
* If the customer doesn't exist yet, it will be created. |
140
|
|
|
* |
141
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
142
|
|
|
* @param string $email Unique e-mail address |
143
|
|
|
* @param string $password New user password |
144
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Aimeos customer item object |
145
|
|
|
*/ |
146
|
|
|
protected function getCustomerItem( \Aimeos\MShop\Context\Item\Iface $context, $email, $password ) |
147
|
|
|
{ |
148
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' ); |
149
|
|
|
|
150
|
|
|
try { |
151
|
|
|
$item = $manager->getItem( $email ); |
152
|
|
|
} catch( \Aimeos\MShop\Exception $e ) { |
153
|
|
|
$item = $manager->createItem(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$item->setCode( $email ); |
157
|
|
|
$item->getPaymentAddress()->setEmail( $email ); |
158
|
|
|
$item->setPassword( $password ); |
159
|
|
|
|
160
|
|
|
$manager->saveItem( $item ); |
161
|
|
|
|
162
|
|
|
return $item; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the customer group item for the given code |
168
|
|
|
* |
169
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
170
|
|
|
* @param string $code Unique customer group code |
171
|
|
|
* @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object |
172
|
|
|
*/ |
173
|
|
|
protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code ) |
174
|
|
|
{ |
175
|
|
|
$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'group' ); |
176
|
|
|
|
177
|
|
|
try |
178
|
|
|
{ |
179
|
|
|
$item = $manager->getItem( $code ); |
180
|
|
|
} |
181
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
182
|
|
|
{ |
183
|
|
|
$item = $manager->createItem(); |
184
|
|
|
$item->setLabel( $code ); |
185
|
|
|
$item->setCode( $code ); |
186
|
|
|
|
187
|
|
|
$manager->saveItem( $item ); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $item; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the console command options. |
196
|
|
|
* |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
protected function getOptions() |
200
|
|
|
{ |
201
|
|
|
return array( |
202
|
|
|
array( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' ), |
203
|
|
|
array( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' ), |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns the customer group type item for the given code |
210
|
|
|
* |
211
|
|
|
* @param \Aimeos\MShop\Common\Manager\Iface $listManager Aimeos customer list manager object |
212
|
|
|
* @param string $code Unique code for a customer list type |
213
|
|
|
* @return \Aimeos\MShop\Common\Item\Type\Iface Aimeos customer list type item object |
214
|
|
|
* @throws \Exception If no customer list type item for the given code is found |
215
|
|
|
*/ |
216
|
|
|
protected function getTypeItem( \Aimeos\MShop\Common\Manager\Iface $listManager, $code ) |
217
|
|
|
{ |
218
|
|
|
$manager = $listManager->getSubmanager( 'type' ); |
219
|
|
|
|
220
|
|
|
$search = $manager->createSearch(); |
221
|
|
|
$expr = array( |
222
|
|
|
$search->compare( '==', 'customer.lists.type.domain', 'customer/group' ), |
223
|
|
|
$search->compare( '==', 'customer.lists.type.code', $code ), |
224
|
|
|
); |
225
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
226
|
|
|
|
227
|
|
|
$result = $manager->searchItems( $search ); |
228
|
|
|
|
229
|
|
|
if( ( $item = reset( $result ) ) === false ) |
230
|
|
|
{ |
231
|
|
|
$msg = sprintf( 'No user list type item for domain "%1$s" and code "%2$s" found', 'customer/group', $code ); |
232
|
|
|
throw new \Exception( $msg ); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $item; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.