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 name and signature of the console command. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $signature = 'aimeos:account |
30
|
|
|
{email? : E-Mail adress of the (admin) user (will ask for if not given)} |
31
|
|
|
{site=default : Site to create account for} |
32
|
|
|
{--password= : Secret password for the account (will ask for if not given)} |
33
|
|
|
{--super : If account should have super user privileges for all sites} |
34
|
|
|
{--admin : If account should have site administrator privileges} |
35
|
|
|
{--editor : If account should have limited editor privileges} |
36
|
|
|
{--api : If account should be able to access the APIs} |
37
|
|
|
'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The console command description. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $description = 'Creates new (admin) accounts'; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Execute the console command. |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function handle() |
53
|
|
|
{ |
54
|
|
|
if( ( $code = $this->argument( 'email' ) ) === null ) { |
55
|
|
|
$code = $this->ask( 'E-Mail' ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if( ( $password = $this->option( 'password' ) ) === null ) { |
59
|
|
|
$password = $this->secret( 'Password' ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$context = $this->getLaravel()->make( 'Aimeos\Shop\Base\Context' )->get( false, 'command' ); |
63
|
|
|
$context->setEditor( 'aimeos:account' ); |
64
|
|
|
|
65
|
|
|
$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' ); |
66
|
|
|
$localeItem = $localeManager->bootstrap( $this->argument( 'site' ), '', '', false ); |
67
|
|
|
$context->setLocale( $localeItem ); |
68
|
|
|
|
69
|
|
|
$this->addGroups( $context, $this->createCustomerItem( $context, $code, $password ) ); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Associates the user to the group by their given IDs |
75
|
|
|
* |
76
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
77
|
|
|
* @param string $userid Unique user ID |
78
|
|
|
* @param string $groupid Unique group ID |
79
|
|
|
*/ |
80
|
|
|
protected function addListItem( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupid ) |
81
|
|
|
{ |
82
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' ); |
83
|
|
|
$typeManager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists/type' ); |
84
|
|
|
|
85
|
|
|
$typeid = $typeManager->findItem( 'default', array(), 'customer/group' )->getId(); |
86
|
|
|
|
87
|
|
|
$search = $manager->createSearch(); |
88
|
|
|
$expr = array( |
89
|
|
|
$search->compare( '==', 'customer.lists.parentid', $userid ), |
90
|
|
|
$search->compare( '==', 'customer.lists.refid', $groupid ), |
91
|
|
|
$search->compare( '==', 'customer.lists.domain', 'customer/group' ), |
92
|
|
|
$search->compare( '==', 'customer.lists.typeid', $typeid ), |
93
|
|
|
); |
94
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
95
|
|
|
$search->setSlice( 0, 1 ); |
96
|
|
|
|
97
|
|
|
if( count( $manager->searchItems( $search ) ) === 0 ) |
98
|
|
|
{ |
99
|
|
|
$item = $manager->createItem(); |
100
|
|
|
$item->setDomain( 'customer/group' ); |
101
|
|
|
$item->setParentId( $userid ); |
102
|
|
|
$item->setTypeId( $typeid ); |
103
|
|
|
$item->setRefId( $groupid ); |
104
|
|
|
$item->setStatus( 1 ); |
105
|
|
|
|
106
|
|
|
$manager->saveItem( $item, false ); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Adds the group to the given user |
113
|
|
|
* |
114
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
115
|
|
|
* @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
116
|
|
|
*/ |
117
|
|
|
protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user ) |
118
|
|
|
{ |
119
|
|
|
if( $this->option( 'super' ) ) { |
120
|
|
|
$this->addGroup( $context, $user, 'super' ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if( $this->option( 'admin' ) ) { |
124
|
|
|
$this->addGroup( $context, $user, 'admin' ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if( $this->option( 'editor' ) ) { |
128
|
|
|
$this->addGroup( $context, $user, 'editor' ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if( $this->option( 'api' ) ) { |
132
|
|
|
$this->addGroup( $context, $user, 'api' ); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Adds the group to the given user |
139
|
|
|
* |
140
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
141
|
|
|
* @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
142
|
|
|
* @param string $group Unique customer group code |
143
|
|
|
*/ |
144
|
|
|
protected function addGroup( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $group ) |
145
|
|
|
{ |
146
|
|
|
$msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"'; |
147
|
|
|
$this->info( sprintf( $msg, $group, $user->getCode(), $this->argument( 'site' ) ) ); |
148
|
|
|
|
149
|
|
|
$groupItem = $this->getGroupItem( $context, $group ); |
150
|
|
|
$this->addListItem( $context, $user->getId(), $groupItem->getId() ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns the customer item for the given e-mail and set its password |
156
|
|
|
* |
157
|
|
|
* If the customer doesn't exist yet, it will be created. |
158
|
|
|
* |
159
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
160
|
|
|
* @param string $email Unique e-mail address |
161
|
|
|
* @param string $password New user password |
162
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Aimeos customer item object |
163
|
|
|
*/ |
164
|
|
|
protected function createCustomerItem( \Aimeos\MShop\Context\Item\Iface $context, $email, $password ) |
165
|
|
|
{ |
166
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' ); |
167
|
|
|
|
168
|
|
|
try { |
169
|
|
|
$item = $manager->findItem( $email ); |
170
|
|
|
} catch( \Aimeos\MShop\Exception $e ) { |
171
|
|
|
$item = $manager->createItem(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$item->setCode( $email ); |
175
|
|
|
$item->setLabel( $email ); |
176
|
|
|
$item->getPaymentAddress()->setEmail( $email ); |
177
|
|
|
$item->setPassword( $password ); |
178
|
|
|
$item->setStatus( 1 ); |
179
|
|
|
|
180
|
|
|
$manager->saveItem( $item ); |
181
|
|
|
|
182
|
|
|
return $item; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get the console command arguments. |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
protected function getArguments() |
192
|
|
|
{ |
193
|
|
|
return array( |
194
|
|
|
array( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' ), |
195
|
|
|
array( 'site', InputArgument::OPTIONAL, 'Site code to create the account for', 'default' ), |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Returns the customer group item for the given code |
202
|
|
|
* |
203
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
204
|
|
|
* @param string $code Unique customer group code |
205
|
|
|
* @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object |
206
|
|
|
*/ |
207
|
|
|
protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code ) |
208
|
|
|
{ |
209
|
|
|
$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'group' ); |
210
|
|
|
|
211
|
|
|
try |
212
|
|
|
{ |
213
|
|
|
$item = $manager->findItem( $code ); |
214
|
|
|
} |
215
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
216
|
|
|
{ |
217
|
|
|
$item = $manager->createItem(); |
218
|
|
|
$item->setLabel( $code ); |
219
|
|
|
$item->setCode( $code ); |
220
|
|
|
|
221
|
|
|
$manager->saveItem( $item ); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $item; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get the console command options. |
230
|
|
|
* |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
|
|
protected function getOptions() |
234
|
|
|
{ |
235
|
|
|
return array( |
236
|
|
|
array( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' ), |
237
|
|
|
array( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' ), |
238
|
|
|
array( 'api', null, InputOption::VALUE_NONE, 'If account should be able to access the APIs' ), |
239
|
|
|
array( 'editor', null, InputOption::VALUE_NONE, 'If account should have limited editor privileges' ), |
240
|
|
|
array( 'viewer', null, InputOption::VALUE_NONE, 'If account should only have view privileges' ), |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
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.