/
Standard.php
562 lines (453 loc) · 17 KB
/
Standard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?php
/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2017-2018
* @package Controller
* @subpackage Frontend
*/
namespace Aimeos\Controller\Frontend\Customer;
/**
* Default implementation of the customer frontend controller
*
* @package Controller
* @subpackage Frontend
*/
class Standard
extends \Aimeos\Controller\Frontend\Base
implements Iface, \Aimeos\Controller\Frontend\Common\Iface
{
/**
* Adds and returns a new customer item object
*
* @param array $values Values added to the newly created customer item like "customer.birthday"
* @return \Aimeos\MShop\Customer\Item\Iface Customer item
* @since 2017.04
*/
public function addItem( array $values )
{
$list = [];
$context = $this->getContext();
$config = $context->getConfig();
// Show only generated passwords in account creation e-mails
$pass = ( isset( $values['customer.password'] ) ? false : true );
foreach( $values as $key => $val ) {
$list[str_replace( 'order.base.address', 'customer', $key )] = $val;
}
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
$list = $this->addItemDefaults( $list );
try
{
$item = $manager->findItem( $list['customer.code'], [], true );
}
catch( \Aimeos\MShop\Exception $e )
{
$this->checkLimit( $list );
$item = $manager->createItem();
$item->fromArray( $list );
$item->setId( null );
/** controller/frontend/customer/groupids
* List of groups new customers should be assigned to
*
* Newly created customers will be assigned automatically to the groups
* given by their IDs. This is especially useful if those groups limit
* functionality for those users.
*
* @param array List of group IDs
* @since 2017.07
* @category User
* @category Developer
*/
$item->setGroups( (array) $config->get( 'controller/frontend/customer/groupids', [] ) );
$item = $manager->saveItem( $item );
$msg = $item->toArray();
$msg['customer.password'] = ( $pass ? $list['customer.password'] : null );
$context->getMessageQueue( 'mq-email', 'customer/email/account' )->add( json_encode( $msg ) );
}
return $item;
}
/**
* Creates a new customer item object pre-filled with the given values but not yet stored
*
* @return \Aimeos\MShop\Customer\Item\Iface Customer item
*/
public function createItem( array $values = [] )
{
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
$item = $manager->createItem();
$item->fromArray( $values );
$item->setId( null );
$item->setStatus( 1 );
return $item;
}
/**
* Deletes a customer item that belongs to the current authenticated user
*
* @param string $id Unique customer ID
* @since 2017.04
*/
public function deleteItem( $id )
{
$this->checkUser( $id );
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
$manager->deleteItem( $id );
}
/**
* Updates the customer item identified by its ID
*
* @param string $id Unique customer ID
* @param array $values Values added to the customer item like "customer.birthday" or "customer.city"
* @return \Aimeos\MShop\Customer\Item\Iface Customer item
* @since 2017.04
*/
public function editItem( $id, array $values )
{
$this->checkUser( $id );
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
$item = $manager->getItem( $id, ['customer/group'], true );
unset( $values['customer.id'], $values['customer.groups'], $values['customer.status'] );
$item->fromArray( $values );
return $manager->saveItem( $item );
}
/**
* Returns the customer item for the given customer ID
*
* @param string|null $id Unique customer ID or null for current customer item
* @param string[] $domains Domain names of items that are associated with the customers and that should be fetched too
* @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items
* @since 2017.04
*/
public function getItem( $id = null, array $domains = [] )
{
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
if( $id == null ) {
return $manager->getItem( $this->getContext()->getUserId(), $domains, true );
}
$this->checkUser( $id );
return $manager->getItem( $id, $domains, true );
}
/**
* Returns the customer item for the given customer code (usually e-mail address)
*
* This method doesn't check if the customer item belongs to the logged in user!
*
* @param string $code Unique customer code
* @param string[] $domains Domain names of items that are associated with the customers and that should be fetched too
* @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items
* @since 2017.04
*/
public function findItem( $code, array $domains = [] )
{
return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' )->findItem( $code, $domains, true );
}
/**
* Stores a modified customer item
*
* @param \Aimeos\MShop\Customer\Item\Iface $item Customer item
* @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item including the generated ID
*/
public function saveItem( \Aimeos\MShop\Customer\Item\Iface $item )
{
return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' )->saveItem( $item );
}
/**
* Creates and returns a new item object
*
* @param array $values Values added to the newly created customer item like "customer.birthday"
* @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
* @since 2017.04
*/
public function addAddressItem( array $values )
{
$context = $this->getContext();
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/address' );
$item = $manager->createItem();
$item->fromArray( $values );
$item->setId( null );
$item->setParentId( $context->getUserId() );
return $manager->saveItem( $item );
}
/**
* Creates a new customer address item object pre-filled with the given values but not yet stored
*
* @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
*/
public function createAddressItem( array $values = [] )
{
$context = $this->getContext();
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/address' );
$item = $manager->createItem();
$item->fromArray( $values );
$item->setId( null );
$item->setParentId( $context->getUserId() );
return $item;
}
/**
* Deletes a customer item that belongs to the current authenticated user
*
* @param string $id Unique customer address ID
* @since 2017.04
*/
public function deleteAddressItem( $id )
{
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
$this->checkUser( $manager->getItem( $id, [], true )->getParentId() );
$manager->deleteItem( $id );
}
/**
* Saves a modified customer item object
*
* @param string $id Unique customer address ID
* @param array $values Values added to the customer item like "customer.address.city"
* @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
* @since 2017.04
*/
public function editAddressItem( $id, array $values )
{
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
$item = $manager->getItem( $id, [], true );
$this->checkUser( $item->getParentId() );
unset( $values['customer.address.id'] );
$item->fromArray( $values );
return $manager->saveItem( $item );
}
/**
* Returns the customer item for the given customer ID
*
* @param string $id Unique customer address ID
* @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
* @since 2017.04
*/
public function getAddressItem( $id )
{
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
$item = $manager->getItem( $id );
$this->checkUser( $item->getParentId() );
return $item;
}
/**
* Stores a modified customer address item
*
* @param \Aimeos\MShop\Customer\Item\Address\Iface $item Customer address item
* @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item including the generated ID
*/
public function saveAddressItem( \Aimeos\MShop\Customer\Item\Address\Iface $item )
{
return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' )->saveItem( $item );
}
/**
* Creates and returns a new list item object
*
* @param array $values Values added to the newly created customer item like "customer.lists.refid"
* @return \Aimeos\MShop\Common\Item\Lists\Iface Customer lists item
* @since 2017.06
*/
public function addListItem( array $values )
{
$context = $this->getContext();
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
if( !isset( $values['customer.lists.typeid'] ) )
{
if( !isset( $values['customer.lists.type'] ) ) {
throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists type code' ) );
}
if( !isset( $values['customer.lists.domain'] ) ) {
throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists domain' ) );
}
$typeManager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists/type' );
$typeItem = $typeManager->findItem( $values['customer.lists.type'], [], $values['customer.lists.domain'] );
$values['customer.lists.typeid'] = $typeItem->getId();
}
$item = $manager->createItem();
$item->fromArray( $values );
$item->setId( null );
$item->setParentId( $context->getUserId() );
return $manager->saveItem( $item );
}
/**
* Returns a new customer lists filter criteria object
*
* @return \Aimeos\MW\Criteria\Iface New filter object
* @since 2017.06
*/
public function createListsFilter()
{
$context = $this->getContext();
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
$filter = $manager->createSearch();
$filter->setConditions( $filter->compare( '==', 'customer.lists.parentid', $context->getUserId() ) );
return $filter;
}
/**
* Deletes a customer item that belongs to the current authenticated user
*
* @param string $id Unique customer address ID
* @since 2017.06
*/
public function deleteListItem( $id )
{
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/lists' );
$this->checkUser( $manager->getItem( $id )->getParentId() );
$manager->deleteItem( $id );
}
/**
* Saves a modified customer lists item object
*
* @param string $id Unique customer lists ID
* @param array $values Values added to the customer lists item like "customer.lists.refid"
* @return \Aimeos\MShop\Common\Item\Lists\Iface Customer lists item
* @since 2017.06
*/
public function editListItem( $id, array $values )
{
$context = $this->getContext();
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
$item = $manager->getItem( $id, [], true );
$this->checkUser( $item->getParentId() );
if( !isset( $values['customer.lists.typeid'] ) )
{
if( !isset( $values['customer.lists.type'] ) ) {
throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists type code' ) );
}
if( !isset( $values['customer.lists.domain'] ) ) {
throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists domain' ) );
}
$typeManager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists/type' );
$typeItem = $typeManager->findItem( $values['customer.lists.type'], [], $values['customer.lists.domain'], true );
$values['customer.lists.typeid'] = $typeItem->getId();
}
unset( $values['customer.lists.id'] );
$item->fromArray( $values );
return $manager->saveItem( $item );
}
/**
* Returns the customer item for the given customer ID
*
* @param string $id Unique customer address ID
* @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
* @since 2017.06
*/
public function getListItem( $id )
{
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/lists' );
$item = $manager->getItem( $id );
$this->checkUser( $item->getParentId() );
return $item;
}
/**
* Returns the customer lists items filtered by the given criteria
*
* @param \Aimeos\MW\Criteria\Iface $filter Criteria object which contains the filter conditions
* @param integer &$total Parameter where the total number of found attributes will be stored in
* @return \Aimeos\MShop\Common\Item\Lists\Iface[] Customer list items
* @since 2017.06
*/
public function searchListItems( \Aimeos\MW\Criteria\Iface $filter, &$total = null )
{
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/lists' );
return $manager->searchItems( $filter, [], $total );
}
/**
* Adds the default values for customer items if not yet available
*
* @param string[] $values Associative list of customer keys (e.g. "customer.label") and their values
* @return string[] Associative list of customer key/value pairs with default values set
*/
protected function addItemDefaults( array $values )
{
if( !isset( $values['customer.label'] ) || $values['customer.label'] == '' )
{
$label = '';
if( isset( $values['customer.lastname'] ) ) {
$label = $values['customer.lastname'];
}
if( isset( $values['customer.firstname'] ) && $values['customer.firstname'] != '' ) {
$label = $values['customer.firstname'] . ' ' . $label;
}
if( isset( $values['customer.company'] ) && $values['customer.company'] != '' ) {
$label .= ' (' . $values['customer.company'] . ')';
}
$values['customer.label'] = $label;
}
if( !isset( $values['customer.code'] ) && isset( $values['customer.email'] ) ) {
$values['customer.code'] = $values['customer.email'];
}
if( !isset( $values['customer.status'] ) ) {
$values['customer.status'] = 1;
}
if( !isset( $values['customer.password'] ) ) {
$values['customer.password'] = substr( md5( microtime( true ) . getmypid() . rand() ), -8 );
}
return $values;
}
/**
* Checks if the current user is allowed to create more customer accounts
*
* @param string[] $values Associative list of customer keys (e.g. "customer.label") and their values
* @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed
*/
protected function checkLimit( array $values )
{
$total = 0;
$context = $this->getContext();
$config = $context->getConfig();
/** controller/frontend/customer/limit-count
* Maximum number of customers within the time frame
*
* Creating new customers is limited to avoid abuse and mitigate denial of
* service attacks. The number of customer accountss created within the
* time frame configured by "controller/frontend/customer/limit-seconds"
* are counted before a new customer account (identified by the IP address)
* is created. If the number of accounts is higher than the configured value,
* an error message will be shown to the user instead of creating a new account.
*
* @param integer Number of customer accounts allowed within the time frame
* @since 2017.07
* @category Developer
* @see controller/frontend/customer/limit-seconds
*/
$count = $config->get( 'controller/frontend/customer/limit-count', 5 );
/** controller/frontend/customer/limit-seconds
* Customer account limitation time frame in seconds
*
* Creating new customer accounts is limited to avoid abuse and mitigate
* denial of service attacks. Within the configured time frame, only a
* limited number of customer accounts can be created. All accounts from
* the same source (identified by the IP address) within the last X
* seconds are counted. If the total value is higher then the number
* configured in "controller/frontend/customer/limit-count", an error
* message will be shown to the user instead of creating a new account.
*
* @param integer Number of seconds to check customer accounts within
* @since 2017.07
* @category Developer
* @see controller/frontend/customer/limit-count
*/
$seconds = $config->get( 'controller/frontend/customer/limit-seconds', 300 );
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
$search = $manager->createSearch();
$expr = [
$search->compare( '==', 'customer.editor', $context->getEditor() ),
$search->compare( '>=', 'customer.ctime', date( 'Y-m-d H:i:s', time() - $seconds ) ),
];
$search->setConditions( $search->combine( '&&', $expr ) );
$search->setSlice( 0, 0 );
$manager->searchItems( $search, [], $total );
if( $total > $count ) {
throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( 'Temporary limit reached' ) );
}
}
/**
* Checks if the current user is allowed to retrieve the customer data for the given ID
*
* @param string $id Unique customer ID
* @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed
*/
protected function checkUser( $id )
{
if( $id != $this->getContext()->getUserId() )
{
$msg = sprintf( 'Not allowed to access customer data for ID "%1$s"', $id );
throw new \Aimeos\Controller\Frontend\Customer\Exception( $msg );
}
}
}