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