Issues (44)

src/Controller/Frontend/Customer/Standard.php (3 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2024
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 Iface, \Aimeos\Controller\Frontend\Common\Iface
23
{
24
	/** controller/frontend/customer/name
25
	 * Class name of the used customer frontend controller implementation
26
	 *
27
	 * Each default frontend controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Frontend\Customer\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Frontend\Customer\Mycustomer
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/frontend/customer/name = Mycustomer
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyCustomer"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 * @category Developer
56
	 */
57
58
	/** controller/frontend/customer/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the customer frontend controllers
60
	 *
61
	 * Decorators extend the functionality of a class by adding new aspects
62
	 * (e.g. log what is currently done), executing the methods of the underlying
63
	 * class only in certain conditions (e.g. only for logged in users) or
64
	 * modify what is returned to the caller.
65
	 *
66
	 * This option allows you to remove a decorator added via
67
	 * "controller/frontend/common/decorators/default" before they are wrapped
68
	 * around the frontend controller.
69
	 *
70
	 *  controller/frontend/customer/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\Controller\Frontend\Common\Decorator\*") added via
74
	 * "controller/frontend/common/decorators/default" for the customer frontend controller.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2014.03
78
	 * @category Developer
79
	 * @see controller/frontend/common/decorators/default
80
	 * @see controller/frontend/customer/decorators/global
81
	 * @see controller/frontend/customer/decorators/local
82
	 */
83
84
	/** controller/frontend/customer/decorators/global
85
	 * Adds a list of globally available decorators only to the customer frontend controllers
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\Controller\Frontend\Common\Decorator\*") around the frontend controller.
94
	 *
95
	 *  controller/frontend/customer/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator1" only to the frontend controller.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2014.03
102
	 * @category Developer
103
	 * @see controller/frontend/common/decorators/default
104
	 * @see controller/frontend/customer/decorators/excludes
105
	 * @see controller/frontend/customer/decorators/local
106
	 */
107
108
	/** controller/frontend/customer/decorators/local
109
	 * Adds a list of local decorators only to the customer frontend controllers
110
	 *
111
	 * Decorators extend the functionality of a class by adding new aspects
112
	 * (e.g. log what is currently done), executing the methods of the underlying
113
	 * class only in certain conditions (e.g. only for logged in users) or
114
	 * modify what is returned to the caller.
115
	 *
116
	 * This option allows you to wrap local decorators
117
	 * ("\Aimeos\Controller\Frontend\Customer\Decorator\*") around the frontend controller.
118
	 *
119
	 *  controller/frontend/customer/decorators/local = array( 'decorator2' )
120
	 *
121
	 * This would add the decorator named "decorator2" defined by
122
	 * "\Aimeos\Controller\Frontend\Customer\Decorator\Decorator2" only to the frontend
123
	 * controller.
124
	 *
125
	 * @param array List of decorator names
126
	 * @since 2014.03
127
	 * @category Developer
128
	 * @see controller/frontend/common/decorators/default
129
	 * @see controller/frontend/customer/decorators/excludes
130
	 * @see controller/frontend/customer/decorators/global
131
	 */
132
133
	private array $domains = [];
134
	private \Aimeos\MShop\Customer\Item\Iface $item;
135
	private \Aimeos\MShop\Common\Manager\Iface $manager;
136
137
138
	/**
139
	 * Initializes the controller
140
	 *
141
	 * @param \Aimeos\MShop\ContextIface $context Common MShop context object
142
	 */
143
	public function __construct( \Aimeos\MShop\ContextIface $context )
144
	{
145
		parent::__construct( $context );
146
147
		$this->manager = \Aimeos\MShop::create( $context, 'customer' );
148
149
		if( ( $userid = $context->user() ) === null )
150
		{
151
			/** controller/frontend/customer/groupids
152
			 * List of groups new customers should be assigned to
153
			 *
154
			 * Newly created customers will be assigned automatically to the groups
155
			 * given by their IDs. This is especially useful if those groups limit
156
			 * functionality for those users.
157
			 *
158
			 * @param array List of group IDs
159
			 * @since 2017.07
160
			 * @category User
161
			 * @category Developer
162
			 */
163
			$groupIds = (array) $context->config()->get( 'controller/frontend/customer/groupids', [] );
164
			$this->item = $this->manager->create()->setGroups( $groupIds );
165
		}
166
		else
167
		{
168
			$this->item = $this->manager->get( $userid, [], true );
169
		}
170
	}
171
172
173
	/**
174
	 * Clones objects in controller and resets values
175
	 */
176
	public function __clone()
177
	{
178
		$this->item = clone $this->item;
179
		parent::__clone();
180
	}
181
182
183
	/**
184
	 * Creates a new customer item object pre-filled with the given values but not yet stored
185
	 *
186
	 * @param array $values Values added to the customer item (new or existing) like "customer.code"
187
	 * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface
188
	 * @since 2019.04
189
	 */
190
	public function add( array $values ) : Iface
191
	{
192
		foreach( $values as $key => $value )
193
		{
194
			if( is_scalar( $value ) ) {
195
				$values[$key] = strip_tags( (string) $value ); // prevent XSS
196
			}
197
		}
198
199
		$addrItem = $this->item->getPaymentAddress();
200
201
		if( $code = $values['customer.code'] ?? null ) {
202
			$this->item->setCode( $code );
203
		}
204
205
		if( $password = $values['customer.password'] ?? null ) {
206
			$this->item = $this->item->setPassword( $password );
207
		}
208
209
		if( $this->item->getLabel() === '' )
210
		{
211
			$label = $addrItem->getLastname();
212
213
			if( ( $firstName = $addrItem->getFirstname() ) !== '' ) {
214
				$label = $firstName . ' ' . $label;
215
			}
216
217
			if( ( $company = $addrItem->getCompany() ) !== '' ) {
218
				$label .= ' (' . $company . ')';
219
			}
220
221
			$this->item->setLabel( $label );
222
		}
223
224
		$this->item->fromArray( $values );
225
		return $this;
226
	}
227
228
229
	/**
230
	 * Adds the given address item to the customer object (not yet stored)
231
	 *
232
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $item Address item to add
233
	 * @param int|null $idx Key in the list of address items or null to add the item at the end
234
	 * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface
235
	 * @since 2019.04
236
	 */
237
	public function addAddressItem( \Aimeos\MShop\Common\Item\Address\Iface $item, int $idx = null ) : Iface
238
	{
239
		$this->item = $this->item->addAddressItem( $item, $idx );
240
		return $this;
241
	}
242
243
244
	/**
245
	 * Adds the given list item to the customer object (not yet stored)
246
	 *
247
	 * @param string $domain Domain name the referenced item belongs to
248
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface $item List item to add
249
	 * @param \Aimeos\MShop\Common\Item\Iface|null $refItem Referenced item to add or null if list item contains refid value
250
	 * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface
251
	 * @since 2019.04
252
	 */
253
	public function addListItem( string $domain, \Aimeos\MShop\Common\Item\Lists\Iface $item,
254
		\Aimeos\MShop\Common\Item\Iface $refItem = null ) : Iface
255
	{
256
		if( $domain === 'group' ) {
257
			throw new Exception( sprintf( 'You are not allowed to manage groups' ) );
258
		}
259
260
		$this->item = $this->item->addListItem( $domain, $item, $refItem );
261
		return $this;
262
	}
263
264
265
	/**
266
	 * Adds the given property item to the customer object (not yet stored)
267
	 *
268
	 * @param \Aimeos\MShop\Common\Item\Property\Iface $item Property item to add
269
	 * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface
270
	 * @since 2019.04
271
	 */
272
	public function addPropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item ) : Iface
273
	{
274
		$this->item = $this->item->addPropertyItem( $item );
275
		return $this;
276
	}
277
278
279
	/**
280
	 * Creates a new address item object pre-filled with the given values
281
	 *
282
	 * @param array $values Associative list of key/value pairs for populating the item
283
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Address item
284
	 * @since 2019.04
285
	 */
286
	public function createAddressItem( array $values = [] ) : \Aimeos\MShop\Customer\Item\Address\Iface
287
	{
288
		return $this->manager->createAddressItem()->fromArray( $values );
0 ignored issues
show
The method createAddressItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

288
		return $this->manager->/** @scrutinizer ignore-call */ createAddressItem()->fromArray( $values );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
289
	}
290
291
292
	/**
293
	 * Creates a new list item object pre-filled with the given values
294
	 *
295
	 * @param array $values Associative list of key/value pairs for populating the item
296
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface List item
297
	 * @since 2019.04
298
	 */
299
	public function createListItem( array $values = [] ) : \Aimeos\MShop\Common\Item\Lists\Iface
300
	{
301
		return $this->manager->createListItem()->fromArray( $values );
0 ignored issues
show
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

301
		return $this->manager->/** @scrutinizer ignore-call */ createListItem()->fromArray( $values );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
302
	}
303
304
305
	/**
306
	 * Creates a new property item object pre-filled with the given values
307
	 *
308
	 * @param array $values Associative list of key/value pairs for populating the item
309
	 * @return \Aimeos\MShop\Common\Item\Property\Iface Property item
310
	 * @since 2019.04
311
	 */
312
	public function createPropertyItem( array $values = [] ) : \Aimeos\MShop\Common\Item\Property\Iface
313
	{
314
		return $this->manager->createPropertyItem()->fromArray( $values );
0 ignored issues
show
The method createPropertyItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

314
		return $this->manager->/** @scrutinizer ignore-call */ createPropertyItem()->fromArray( $values );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
315
	}
316
317
318
	/**
319
	 * Deletes a customer item that belongs to the current authenticated user
320
	 *
321
	 * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface
322
	 * @since 2019.04
323
	 */
324
	public function delete() : Iface
325
	{
326
		if( $this->item && $this->item->getId() ) {
327
			\Aimeos\MShop::create( $this->context(), 'customer' )->delete( $this->item->getId() );
328
		}
329
330
		return $this;
331
	}
332
333
334
	/**
335
	 * Removes the given address item from the customer object (not yet stored)
336
	 *
337
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $item Address item to remove
338
	 * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface
339
	 */
340
	public function deleteAddressItem( \Aimeos\MShop\Common\Item\Address\Iface $item ) : Iface
341
	{
342
		$this->item = $this->item->deleteAddressItem( $item );
343
		return $this;
344
	}
345
346
347
	/**
348
	 * Removes the given list item from the customer object (not yet stored)
349
	 *
350
	 * @param string $domain Domain name the referenced item belongs to
351
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface $item List item to remove
352
	 * @param \Aimeos\MShop\Common\Item\Iface|null $refItem Referenced item to remove or null if only list item should be removed
353
	 * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface
354
	 */
355
	public function deleteListItem( string $domain, \Aimeos\MShop\Common\Item\Lists\Iface $listItem,
356
		\Aimeos\MShop\Common\Item\Iface $refItem = null ) : Iface
357
	{
358
		if( $domain === 'group' ) {
359
			throw new Exception( sprintf( 'You are not allowed to manage groups' ) );
360
		}
361
362
		$this->item = $this->item->deleteListItem( $domain, $listItem, $refItem );
363
		return $this;
364
	}
365
366
367
	/**
368
	 * Removes the given property item from the customer object (not yet stored)
369
	 *
370
	 * @param \Aimeos\MShop\Common\Item\Property\Iface $item Property item to remove
371
	 * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface
372
	 */
373
	public function deletePropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item ) : Iface
374
	{
375
		$this->item = $this->item->deletePropertyItem( $item );
376
		return $this;
377
	}
378
379
380
	/**
381
	 * Returns the customer item for the given customer code (usually e-mail address)
382
	 *
383
	 * This method doesn't check if the customer item belongs to the logged in user!
384
	 *
385
	 * @param string $code Unique customer code
386
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items
387
	 * @since 2019.04
388
	 */
389
	public function find( string $code ) : \Aimeos\MShop\Customer\Item\Iface
390
	{
391
		return $this->manager->find( $code, $this->domains, 'customer', null, null );
392
	}
393
394
395
	/**
396
	 * Returns the customer item for the current authenticated user
397
	 *
398
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items
399
	 * @since 2019.04
400
	 */
401
	public function get() : \Aimeos\MShop\Customer\Item\Iface
402
	{
403
		return $this->item;
404
	}
405
406
407
	/**
408
	 * Adds or updates a modified customer item in the storage
409
	 *
410
	 * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface
411
	 * @since 2019.04
412
	 */
413
	public function store() : Iface
414
	{
415
		( $id = $this->item->getId() ) !== null ? $this->checkId( $id ) : $this->checkLimit();
416
		$context = $this->context();
417
418
		if( $id === null )
419
		{
420
			$msg = $this->item->toArray();
421
422
			// Show only generated passwords in account creation e-mails
423
			if( $this->item->getPassword() === '' )
424
			{
425
				$msg['customer.password'] = substr( sha1( microtime( true ) . getmypid() . rand() ), -8 );
426
				$this->item->setPassword( $msg['customer.password'] );
427
			}
428
429
			$context->queue( 'mq-email', 'customer/email/account' )->add( json_encode( $msg ) );
430
		}
431
432
		$this->item = $this->manager->save( $this->item );
433
		return $this;
434
	}
435
436
437
	/**
438
	 * Sets the domains that will be used when working with the customer item
439
	 *
440
	 * @param array $domains Domain names of the referenced items that should be fetched too
441
	 * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface
442
	 * @since 2019.04
443
	 */
444
	public function uses( array $domains ) : Iface
445
	{
446
		$this->domains = $domains;
447
448
		if( ( $id = $this->context()->user() ) !== null ) {
449
			$this->item = $this->manager->get( $id, $domains, true );
450
		}
451
452
		return $this;
453
	}
454
455
456
	/**
457
	 * Checks if the current user is allowed to create more customer accounts
458
	 *
459
	 * @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed
460
	 */
461
	protected function checkLimit()
462
	{
463
		$total = 0;
464
		$context = $this->context();
465
		$config = $context->config();
466
467
		/** controller/frontend/customer/limit-count
468
		 * Maximum number of customers within the time frame
469
		 *
470
		 * Creating new customers is limited to avoid abuse and mitigate denial of
471
		 * service attacks. The number of customer accountss created within the
472
		 * time frame configured by "controller/frontend/customer/limit-seconds"
473
		 * are counted before a new customer account (identified by the IP address)
474
		 * is created. If the number of accounts is higher than the configured value,
475
		 * an error message will be shown to the user instead of creating a new account.
476
		 *
477
		 * @param integer Number of customer accounts allowed within the time frame
478
		 * @since 2017.07
479
		 * @category Developer
480
		 * @see controller/frontend/customer/limit-seconds
481
		 */
482
		$count = $config->get( 'controller/frontend/customer/limit-count', 3 );
483
484
		/** controller/frontend/customer/limit-seconds
485
		 * Customer account limitation time frame in seconds
486
		 *
487
		 * Creating new customer accounts is limited to avoid abuse and mitigate
488
		 * denial of service attacks. Within the configured time frame, only a
489
		 * limited number of customer accounts can be created. All accounts from
490
		 * the same source (identified by the IP address) within the last X
491
		 * seconds are counted. If the total value is higher then the number
492
		 * configured in "controller/frontend/customer/limit-count", an error
493
		 * message will be shown to the user instead of creating a new account.
494
		 *
495
		 * @param integer Number of seconds to check customer accounts within
496
		 * @since 2017.07
497
		 * @category Developer
498
		 * @see controller/frontend/customer/limit-count
499
		 */
500
		$seconds = $config->get( 'controller/frontend/customer/limit-seconds', 14400 );
501
502
		$search = $this->manager->filter()->slice( 0, 0 );
503
		$expr = [
504
			$search->compare( '==', 'customer.editor', $context->editor() ),
505
			$search->compare( '>=', 'customer.ctime', date( 'Y-m-d H:i:s', time() - $seconds ) ),
506
		];
507
		$search->add( $search->and( $expr ) );
508
509
		$this->manager->search( $search, [], $total );
510
511
		if( $total >= $count ) {
512
			throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'Temporary limit reached' ) );
513
		}
514
	}
515
516
517
	/**
518
	 * Checks if the current user is allowed to retrieve the customer data for the given ID
519
	 *
520
	 * @param string $id Unique customer ID
521
	 * @return string Unique customer ID
522
	 * @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed
523
	 */
524
	protected function checkId( string $id ) : string
525
	{
526
		if( $id != $this->context()->user() )
527
		{
528
			$msg = sprintf( 'Not allowed to access customer data for ID "%1$s"', $id );
529
			throw new \Aimeos\Controller\Frontend\Customer\Exception( $msg );
530
		}
531
532
		return $id;
533
	}
534
535
536
	/**
537
	 * Returns the manager used by the controller
538
	 *
539
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
540
	 */
541
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
542
	{
543
		return $this->manager;
544
	}
545
}
546