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