1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2021 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Frontend |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Frontend\Customer; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default implementation of the customer frontend controller |
16
|
|
|
* |
17
|
|
|
* @package Controller |
18
|
|
|
* @subpackage Frontend |
19
|
|
|
*/ |
20
|
|
|
class Standard |
21
|
|
|
extends \Aimeos\Controller\Frontend\Base |
22
|
|
|
implements \Aimeos\Controller\Frontend\Customer\Iface, \Aimeos\Controller\Frontend\Common\Iface |
23
|
|
|
{ |
24
|
|
|
private $domains = []; |
25
|
|
|
private $manager; |
26
|
|
|
|
27
|
|
|
/** @var Iface */ |
28
|
|
|
private $item; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initializes the controller |
33
|
|
|
* |
34
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object |
35
|
|
|
*/ |
36
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
37
|
|
|
{ |
38
|
|
|
parent::__construct( $context ); |
39
|
|
|
|
40
|
|
|
$this->manager = \Aimeos\MShop::create( $context, 'customer' ); |
41
|
|
|
|
42
|
|
|
if( ( $userid = $context->getUserId() ) === null ) |
43
|
|
|
{ |
44
|
|
|
/** controller/frontend/customer/groupids |
45
|
|
|
* List of groups new customers should be assigned to |
46
|
|
|
* |
47
|
|
|
* Newly created customers will be assigned automatically to the groups |
48
|
|
|
* given by their IDs. This is especially useful if those groups limit |
49
|
|
|
* functionality for those users. |
50
|
|
|
* |
51
|
|
|
* @param array List of group IDs |
52
|
|
|
* @since 2017.07 |
53
|
|
|
* @category User |
54
|
|
|
* @category Developer |
55
|
|
|
*/ |
56
|
|
|
$groupIds = (array) $context->getConfig()->get( 'controller/frontend/customer/groupids', [] ); |
57
|
|
|
$this->item = $this->manager->create()->setGroups( $groupIds ); |
58
|
|
|
} |
59
|
|
|
else |
60
|
|
|
{ |
61
|
|
|
$this->item = $this->manager->get( $userid, [], true ); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Clones objects in controller and resets values |
68
|
|
|
*/ |
69
|
|
|
public function __clone() |
70
|
|
|
{ |
71
|
|
|
$this->item = clone $this->item; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Creates a new customer item object pre-filled with the given values but not yet stored |
77
|
|
|
* |
78
|
|
|
* @param array $values Values added to the customer item (new or existing) like "customer.code" |
79
|
|
|
* @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
80
|
|
|
* @since 2019.04 |
81
|
|
|
*/ |
82
|
|
|
public function add( array $values ) : \Aimeos\Controller\Frontend\Customer\Iface |
83
|
|
|
{ |
84
|
|
|
foreach( $values as $key => $value ) |
85
|
|
|
{ |
86
|
|
|
if( is_scalar( $value ) ) { |
87
|
|
|
$values[$key] = strip_tags( (string) $value ); // prevent XSS |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$addrItem = $this->item->getPaymentAddress(); |
|
|
|
|
92
|
|
|
|
93
|
|
|
if( $code = $values['customer.code'] ?? null ) { |
94
|
|
|
$this->item->setCode( $code ); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ( $oldPassword = $values['customer.oldpassword'] ?? null) { |
98
|
|
|
$confirmed = $values['customer.newpassword'] === $values['customer.confirmnewpassword']; |
99
|
|
|
$isNew = $values['customer.newpassword'] !== $values['customer.oldpassword']; |
100
|
|
|
if ($this->item->verifyPassword($oldPassword) && $confirmed && $isNew) { |
|
|
|
|
101
|
|
|
$this->item = $this->item->setPassword( $values['customer.newpassword'] ); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if( $password = $values['customer.password'] ?? null ) { |
106
|
|
|
$this->item = $this->item->setPassword( $password ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if( $this->item->getLabel() === '' ) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$label = $addrItem->getLastname(); |
112
|
|
|
|
113
|
|
|
if( ( $firstName = $addrItem->getFirstname() ) !== '' ) { |
114
|
|
|
$label = $firstName . ' ' . $label; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if( ( $company = $addrItem->getCompany() ) !== '' ) { |
118
|
|
|
$label .= ' (' . $company . ')'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->item->setLabel( $label ); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->item->fromArray( $values ); |
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Adds the given address item to the customer object (not yet stored) |
131
|
|
|
* |
132
|
|
|
* @param \Aimeos\MShop\Common\Item\Address\Iface $item Address item to add |
133
|
|
|
* @param int|null $idx Key in the list of address items or null to add the item at the end |
134
|
|
|
* @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
135
|
|
|
* @since 2019.04 |
136
|
|
|
*/ |
137
|
|
|
public function addAddressItem( \Aimeos\MShop\Common\Item\Address\Iface $item, int $idx = null ) : \Aimeos\Controller\Frontend\Customer\Iface |
138
|
|
|
{ |
139
|
|
|
$this->item = $this->item->addAddressItem( $item, $idx ); |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Adds the given list item to the customer object (not yet stored) |
146
|
|
|
* |
147
|
|
|
* @param string $domain Domain name the referenced item belongs to |
148
|
|
|
* @param \Aimeos\MShop\Common\Item\Lists\Iface $item List item to add |
149
|
|
|
* @param \Aimeos\MShop\Common\Item\Iface|null $refItem Referenced item to add or null if list item contains refid value |
150
|
|
|
* @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
151
|
|
|
* @since 2019.04 |
152
|
|
|
*/ |
153
|
|
|
public function addListItem( string $domain, \Aimeos\MShop\Common\Item\Lists\Iface $item, |
154
|
|
|
\Aimeos\MShop\Common\Item\Iface $refItem = null ) : \Aimeos\Controller\Frontend\Customer\Iface |
155
|
|
|
{ |
156
|
|
|
if( $domain === 'customer/group' ) { |
157
|
|
|
throw new Exception( sprintf( 'You are not allowed to manage groups' ) ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->item = $this->item->addListItem( $domain, $item, $refItem ); |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Adds the given property item to the customer object (not yet stored) |
167
|
|
|
* |
168
|
|
|
* @param \Aimeos\MShop\Common\Item\Property\Iface $item Property item to add |
169
|
|
|
* @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
170
|
|
|
* @since 2019.04 |
171
|
|
|
*/ |
172
|
|
|
public function addPropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item ) : \Aimeos\Controller\Frontend\Customer\Iface |
173
|
|
|
{ |
174
|
|
|
$this->item = $this->item->addPropertyItem( $item ); |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Creates a new address item object pre-filled with the given values |
181
|
|
|
* |
182
|
|
|
* @param array $values Associative list of key/value pairs for populating the item |
183
|
|
|
* @return \Aimeos\MShop\Customer\Item\Address\Iface Address item |
184
|
|
|
* @since 2019.04 |
185
|
|
|
*/ |
186
|
|
|
public function createAddressItem( array $values = [] ) : \Aimeos\MShop\Customer\Item\Address\Iface |
187
|
|
|
{ |
188
|
|
|
return $this->manager->createAddressItem()->fromArray( $values ); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Creates a new list item object pre-filled with the given values |
194
|
|
|
* |
195
|
|
|
* @param array $values Associative list of key/value pairs for populating the item |
196
|
|
|
* @return \Aimeos\MShop\Common\Item\Lists\Iface List item |
197
|
|
|
* @since 2019.04 |
198
|
|
|
*/ |
199
|
|
|
public function createListItem( array $values = [] ) : \Aimeos\MShop\Common\Item\Lists\Iface |
200
|
|
|
{ |
201
|
|
|
return $this->manager->createListItem()->fromArray( $values ); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Creates a new property item object pre-filled with the given values |
207
|
|
|
* |
208
|
|
|
* @param array $values Associative list of key/value pairs for populating the item |
209
|
|
|
* @return \Aimeos\MShop\Common\Item\Property\Iface Property item |
210
|
|
|
* @since 2019.04 |
211
|
|
|
*/ |
212
|
|
|
public function createPropertyItem( array $values = [] ) : \Aimeos\MShop\Common\Item\Property\Iface |
213
|
|
|
{ |
214
|
|
|
return $this->manager->createPropertyItem()->fromArray( $values ); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Deletes a customer item that belongs to the current authenticated user |
220
|
|
|
* |
221
|
|
|
* @return Iface Customer controller for fluent interface |
222
|
|
|
* @since 2019.04 |
223
|
|
|
*/ |
224
|
|
|
public function delete() : Iface |
225
|
|
|
{ |
226
|
|
|
if( $this->item && $this->item->getId() ) { |
|
|
|
|
227
|
|
|
\Aimeos\MShop::create( $this->getContext(), 'customer' )->delete( $this->item->getId() ); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Removes the given address item from the customer object (not yet stored) |
236
|
|
|
* |
237
|
|
|
* @param \Aimeos\MShop\Common\Item\Address\Iface $item Address item to remove |
238
|
|
|
* @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
239
|
|
|
*/ |
240
|
|
|
public function deleteAddressItem( \Aimeos\MShop\Common\Item\Address\Iface $item ) : \Aimeos\Controller\Frontend\Customer\Iface |
241
|
|
|
{ |
242
|
|
|
$this->item = $this->item->deleteAddressItem( $item ); |
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Removes the given list item from the customer object (not yet stored) |
249
|
|
|
* |
250
|
|
|
* @param string $domain Domain name the referenced item belongs to |
251
|
|
|
* @param \Aimeos\MShop\Common\Item\Lists\Iface $item List item to remove |
252
|
|
|
* @param \Aimeos\MShop\Common\Item\Iface|null $refItem Referenced item to remove or null if only list item should be removed |
253
|
|
|
* @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
254
|
|
|
*/ |
255
|
|
|
public function deleteListItem( string $domain, \Aimeos\MShop\Common\Item\Lists\Iface $listItem, |
256
|
|
|
\Aimeos\MShop\Common\Item\Iface $refItem = null ) : \Aimeos\Controller\Frontend\Customer\Iface |
257
|
|
|
{ |
258
|
|
|
if( $domain === 'customer/group' ) { |
259
|
|
|
throw new Exception( sprintf( 'You are not allowed to manage groups' ) ); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$this->item = $this->item->deleteListItem( $domain, $listItem, $refItem ); |
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Removes the given property item from the customer object (not yet stored) |
269
|
|
|
* |
270
|
|
|
* @param \Aimeos\MShop\Common\Item\Property\Iface $item Property item to remove |
271
|
|
|
* @return Iface Customer controller for fluent interface |
272
|
|
|
*/ |
273
|
|
|
public function deletePropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item ) : Iface |
274
|
|
|
{ |
275
|
|
|
$this->item = $this->item->deletePropertyItem( $item ); |
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Returns the customer item for the given customer code (usually e-mail address) |
282
|
|
|
* |
283
|
|
|
* This method doesn't check if the customer item belongs to the logged in user! |
284
|
|
|
* |
285
|
|
|
* @param string $code Unique customer code |
286
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items |
287
|
|
|
* @since 2019.04 |
288
|
|
|
*/ |
289
|
|
|
public function find( string $code ) : \Aimeos\MShop\Customer\Item\Iface |
290
|
|
|
{ |
291
|
|
|
return $this->manager->find( $code, $this->domains, 'customer', null, true ); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Returns the customer item for the current authenticated user |
297
|
|
|
* |
298
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items |
299
|
|
|
* @since 2019.04 |
300
|
|
|
*/ |
301
|
|
|
public function get() : \Aimeos\MShop\Customer\Item\Iface |
302
|
|
|
{ |
303
|
|
|
return $this->item; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Adds or updates a modified customer item in the storage |
309
|
|
|
* |
310
|
|
|
* @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
311
|
|
|
* @since 2019.04 |
312
|
|
|
*/ |
313
|
|
|
public function store() : \Aimeos\Controller\Frontend\Customer\Iface |
314
|
|
|
{ |
315
|
|
|
( $id = $this->item->getId() ) !== null ? $this->checkId( $id ) : $this->checkLimit(); |
316
|
|
|
$context = $this->getContext(); |
317
|
|
|
|
318
|
|
|
if( $id === null ) |
319
|
|
|
{ |
320
|
|
|
$msg = $this->item->toArray(); |
|
|
|
|
321
|
|
|
|
322
|
|
|
// Show only generated passwords in account creation e-mails |
323
|
|
|
if( $this->item->getPassword() === '' ) |
|
|
|
|
324
|
|
|
{ |
325
|
|
|
$msg['customer.password'] = substr( sha1( microtime( true ) . getmypid() . rand() ), -8 ); |
326
|
|
|
$this->item->setPassword( $msg['customer.password'] ); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$context->getMessageQueue( 'mq-email', 'customer/email/account' )->add( json_encode( $msg ) ); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
$this->item = $this->manager->save( $this->item ); |
|
|
|
|
333
|
|
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Sets the domains that will be used when working with the customer item |
339
|
|
|
* |
340
|
|
|
* @param array $domains Domain names of the referenced items that should be fetched too |
341
|
|
|
* @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
342
|
|
|
* @since 2019.04 |
343
|
|
|
*/ |
344
|
|
|
public function uses( array $domains ) : \Aimeos\Controller\Frontend\Customer\Iface |
345
|
|
|
{ |
346
|
|
|
$this->domains = $domains; |
347
|
|
|
|
348
|
|
|
if( ( $id = $this->getContext()->getUserId() ) !== null ) { |
349
|
|
|
$this->item = $this->manager->get( $id, $domains, true ); |
|
|
|
|
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
return $this; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Checks if the current user is allowed to create more customer accounts |
358
|
|
|
* |
359
|
|
|
* @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed |
360
|
|
|
*/ |
361
|
|
|
protected function checkLimit() |
362
|
|
|
{ |
363
|
|
|
$total = 0; |
364
|
|
|
$context = $this->getContext(); |
365
|
|
|
$config = $context->getConfig(); |
366
|
|
|
|
367
|
|
|
/** controller/frontend/customer/limit-count |
368
|
|
|
* Maximum number of customers within the time frame |
369
|
|
|
* |
370
|
|
|
* Creating new customers is limited to avoid abuse and mitigate denial of |
371
|
|
|
* service attacks. The number of customer accountss created within the |
372
|
|
|
* time frame configured by "controller/frontend/customer/limit-seconds" |
373
|
|
|
* are counted before a new customer account (identified by the IP address) |
374
|
|
|
* is created. If the number of accounts is higher than the configured value, |
375
|
|
|
* an error message will be shown to the user instead of creating a new account. |
376
|
|
|
* |
377
|
|
|
* @param integer Number of customer accounts allowed within the time frame |
378
|
|
|
* @since 2017.07 |
379
|
|
|
* @category Developer |
380
|
|
|
* @see controller/frontend/customer/limit-seconds |
381
|
|
|
*/ |
382
|
|
|
$count = $config->get( 'controller/frontend/customer/limit-count', 3 ); |
383
|
|
|
|
384
|
|
|
/** controller/frontend/customer/limit-seconds |
385
|
|
|
* Customer account limitation time frame in seconds |
386
|
|
|
* |
387
|
|
|
* Creating new customer accounts is limited to avoid abuse and mitigate |
388
|
|
|
* denial of service attacks. Within the configured time frame, only a |
389
|
|
|
* limited number of customer accounts can be created. All accounts from |
390
|
|
|
* the same source (identified by the IP address) within the last X |
391
|
|
|
* seconds are counted. If the total value is higher then the number |
392
|
|
|
* configured in "controller/frontend/customer/limit-count", an error |
393
|
|
|
* message will be shown to the user instead of creating a new account. |
394
|
|
|
* |
395
|
|
|
* @param integer Number of seconds to check customer accounts within |
396
|
|
|
* @since 2017.07 |
397
|
|
|
* @category Developer |
398
|
|
|
* @see controller/frontend/customer/limit-count |
399
|
|
|
*/ |
400
|
|
|
$seconds = $config->get( 'controller/frontend/customer/limit-seconds', 14400 ); |
401
|
|
|
|
402
|
|
|
$search = $this->manager->filter()->slice( 0, 0 ); |
403
|
|
|
$expr = [ |
404
|
|
|
$search->compare( '==', 'customer.editor', $context->getEditor() ), |
405
|
|
|
$search->compare( '>=', 'customer.ctime', date( 'Y-m-d H:i:s', time() - $seconds ) ), |
406
|
|
|
]; |
407
|
|
|
$search->setConditions( $search->and( $expr ) ); |
408
|
|
|
|
409
|
|
|
$this->manager->search( $search, [], $total ); |
410
|
|
|
|
411
|
|
|
if( $total >= $count ) { |
412
|
|
|
throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'Temporary limit reached' ) ); |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Checks if the current user is allowed to retrieve the customer data for the given ID |
419
|
|
|
* |
420
|
|
|
* @param string $id Unique customer ID |
421
|
|
|
* @return string Unique customer ID |
422
|
|
|
* @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed |
423
|
|
|
*/ |
424
|
|
|
protected function checkId( string $id ) : string |
425
|
|
|
{ |
426
|
|
|
if( $id != $this->getContext()->getUserId() ) |
427
|
|
|
{ |
428
|
|
|
$msg = sprintf( 'Not allowed to access customer data for ID "%1$s"', $id ); |
429
|
|
|
throw new \Aimeos\Controller\Frontend\Customer\Exception( $msg ); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
return $id; |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..