11
11
use Magento \Framework \Exception \AuthenticationException ;
12
12
use Magento \Store \Model \Store ;
13
13
use Magento \User \Api \Data \UserInterface ;
14
+ use Magento \Framework \App \DeploymentConfig ;
15
+ use Magento \Framework \Exception \MailException ;
16
+ use Magento \Framework \App \ObjectManager ;
14
17
15
18
/**
16
19
* Admin user model
@@ -113,6 +116,11 @@ class User extends AbstractModel implements StorageInterface, UserInterface
113
116
*/
114
117
protected $ validationRules ;
115
118
119
+ /**
120
+ * @var DeploymentConfig
121
+ */
122
+ private $ deploymentConfig ;
123
+
116
124
/**
117
125
* @param \Magento\Framework\Model\Context $context
118
126
* @param \Magento\Framework\Registry $registry
@@ -126,6 +134,7 @@ class User extends AbstractModel implements StorageInterface, UserInterface
126
134
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
127
135
* @param UserValidationRules $validationRules
128
136
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
137
+ * @param DeploymentConfig|null $deploymentConfig
129
138
* @param array $data
130
139
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
131
140
*/
@@ -142,7 +151,8 @@ public function __construct(
142
151
UserValidationRules $ validationRules ,
143
152
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
144
153
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
145
- array $ data = []
154
+ array $ data = [],
155
+ DeploymentConfig $ deploymentConfig = null
146
156
) {
147
157
$ this ->_encryptor = $ encryptor ;
148
158
parent ::__construct ($ context , $ registry , $ resource , $ resourceCollection , $ data );
@@ -153,6 +163,8 @@ public function __construct(
153
163
$ this ->_transportBuilder = $ transportBuilder ;
154
164
$ this ->_storeManager = $ storeManager ;
155
165
$ this ->validationRules = $ validationRules ;
166
+ $ this ->deploymentConfig = $ deploymentConfig
167
+ ?: ObjectManager::getInstance ()->get (DeploymentConfig::class);
156
168
}
157
169
158
170
/**
@@ -382,23 +394,59 @@ public function roleUserExists()
382
394
return is_array ($ result ) && count ($ result ) > 0 ? true : false ;
383
395
}
384
396
397
+ /**
398
+ * Send a notification to an admin.
399
+ *
400
+ * @param string $templateConfigId
401
+ * @param array $templateVars
402
+ * @param string|null $toEmail
403
+ * @param string|null $toName
404
+ * @throws MailException
405
+ *
406
+ * @return void
407
+ */
408
+ private function sendNotification (
409
+ $ templateConfigId ,
410
+ array $ templateVars ,
411
+ $ toEmail = null ,
412
+ $ toName = null
413
+ ) {
414
+ $ toEmail = $ toEmail ?: $ this ->getEmail ();
415
+ $ toName = $ toName ?: $ this ->getName ();
416
+ $ this ->_transportBuilder
417
+ ->setTemplateIdentifier ($ this ->_config ->getValue ($ templateConfigId ))
418
+ ->setTemplateModel (\Magento \Email \Model \BackendTemplate::class)
419
+ ->setTemplateOptions ([
420
+ 'area ' => FrontNameResolver::AREA_CODE ,
421
+ 'store ' => Store::DEFAULT_STORE_ID
422
+ ])
423
+ ->setTemplateVars ($ templateVars )
424
+ ->setFrom (
425
+ $ this ->_config ->getValue (self ::XML_PATH_FORGOT_EMAIL_IDENTITY )
426
+ )
427
+ ->addTo ($ toEmail , $ toName )
428
+ ->getTransport ()
429
+ ->sendMessage ();
430
+ }
431
+
385
432
/**
386
433
* Send email with reset password confirmation link
387
434
*
435
+ * @throws MailException
388
436
* @return $this
389
437
*/
390
438
public function sendPasswordResetConfirmationEmail ()
391
439
{
392
- $ templateId = $ this ->_config ->getValue (self ::XML_PATH_FORGOT_EMAIL_TEMPLATE );
393
- $ transport = $ this ->_transportBuilder ->setTemplateIdentifier ($ templateId )
394
- ->setTemplateModel ('Magento\Email\Model\BackendTemplate ' )
395
- ->setTemplateOptions (['area ' => FrontNameResolver::AREA_CODE , 'store ' => Store::DEFAULT_STORE_ID ])
396
- ->setTemplateVars (['user ' => $ this , 'store ' => $ this ->_storeManager ->getStore (Store::DEFAULT_STORE_ID )])
397
- ->setFrom ($ this ->_config ->getValue (self ::XML_PATH_FORGOT_EMAIL_IDENTITY ))
398
- ->addTo ($ this ->getEmail (), $ this ->getName ())
399
- ->getTransport ();
440
+ $ this ->sendNotification (
441
+ self ::XML_PATH_FORGOT_EMAIL_TEMPLATE ,
442
+ [
443
+ 'user ' => $ this ,
444
+ 'store ' => $ this ->_storeManager ->getStore (
445
+ Store::DEFAULT_STORE_ID
446
+ )
447
+ ]
448
+ );
400
449
401
- $ transport ->sendMessage ();
402
450
return $ this ;
403
451
}
404
452
@@ -414,20 +462,59 @@ public function sendPasswordResetNotificationEmail()
414
462
return $ this ;
415
463
}
416
464
465
+ /**
466
+ * Send notification about a new user created.
467
+ *
468
+ * @throws MailException
469
+ * @return void
470
+ */
471
+ private function sendNewUserNotificationEmail ()
472
+ {
473
+ $ toEmails = [];
474
+ $ generalEmail = $ this ->_config ->getValue (
475
+ 'trans_email/ident_general/email '
476
+ );
477
+ if ($ generalEmail ) {
478
+ $ toEmails [] = $ generalEmail ;
479
+ }
480
+ if ($ adminEmail = $ this ->deploymentConfig ->get ('user_admin_email ' )) {
481
+ $ toEmails [] = $ adminEmail ;
482
+ }
483
+
484
+ foreach ($ toEmails as $ toEmail ) {
485
+ $ this ->sendNotification (
486
+ 'admin/emails/new_user_notification_template ' ,
487
+ [
488
+ 'user ' => $ this ,
489
+ 'store ' => $ this ->_storeManager ->getStore (
490
+ Store::DEFAULT_STORE_ID
491
+ )
492
+ ],
493
+ $ toEmail ,
494
+ __ ('Administrator ' )->render ()
495
+ );
496
+ }
497
+ }
498
+
417
499
/**
418
500
* Check changes and send notification emails
419
501
*
502
+ * @throws MailException
420
503
* @return $this
421
504
*/
422
505
public function sendNotificationEmailsIfRequired ()
423
506
{
424
- $ changes = $ this ->createChangesDescriptionString ();
425
-
426
- if ($ changes ) {
427
- if ($ this ->getEmail () != $ this ->getOrigData ('email ' ) && $ this ->getOrigData ('email ' )) {
428
- $ this ->sendUserNotificationEmail ($ changes , $ this ->getOrigData ('email ' ));
507
+ if ($ this ->isObjectNew ()) {
508
+ //Notification about a new user
509
+ $ this ->sendNewUserNotificationEmail ();
510
+ } elseif ($ changes = $ this ->createChangesDescriptionString ()) {
511
+ $ email = $ this ->getEmail ();
512
+ if ($ this ->getEmail () != $ this ->getOrigData ('email ' )
513
+ && $ this ->getOrigData ('email ' )
514
+ ) {
515
+ $ email = $ this ->getOrigData ('email ' );
429
516
}
430
- $ this ->sendUserNotificationEmail ($ changes );
517
+ $ this ->sendUserNotificationEmail ($ changes, $ email );
431
518
}
432
519
433
520
return $ this ;
@@ -464,30 +551,23 @@ protected function createChangesDescriptionString()
464
551
*
465
552
* @param string $changes
466
553
* @param string $email
554
+ * @throws MailException
467
555
* @return $this
468
556
*/
469
557
protected function sendUserNotificationEmail ($ changes , $ email = null )
470
558
{
471
- if ($ email === null ) {
472
- $ email = $ this ->getEmail ();
473
- }
474
-
475
- $ transport = $ this ->_transportBuilder
476
- ->setTemplateIdentifier ($ this ->_config ->getValue (self ::XML_PATH_USER_NOTIFICATION_TEMPLATE ))
477
- ->setTemplateModel ('Magento\Email\Model\BackendTemplate ' )
478
- ->setTemplateOptions (['area ' => FrontNameResolver::AREA_CODE , 'store ' => Store::DEFAULT_STORE_ID ])
479
- ->setTemplateVars (
480
- [
481
- 'user ' => $ this ,
482
- 'store ' => $ this ->_storeManager ->getStore (Store::DEFAULT_STORE_ID ),
483
- 'changes ' => $ changes
484
- ]
485
- )
486
- ->setFrom ($ this ->_config ->getValue (self ::XML_PATH_FORGOT_EMAIL_IDENTITY ))
487
- ->addTo ($ email , $ this ->getName ())
488
- ->getTransport ();
559
+ $ this ->sendNotification (
560
+ self ::XML_PATH_USER_NOTIFICATION_TEMPLATE ,
561
+ [
562
+ 'user ' => $ this ,
563
+ 'store ' => $ this ->_storeManager ->getStore (
564
+ Store::DEFAULT_STORE_ID
565
+ ),
566
+ 'changes ' => $ changes
567
+ ],
568
+ $ email
569
+ );
489
570
490
- $ transport ->sendMessage ();
491
571
return $ this ;
492
572
}
493
573
0 commit comments