7
7
8
8
use Magento \Backend \App \Area \FrontNameResolver ;
9
9
use Magento \Backend \Model \Auth \Credential \StorageInterface ;
10
+ use Magento \Framework \App \DeploymentConfig ;
10
11
use Magento \Framework \App \ObjectManager ;
12
+ use Magento \Framework \Exception \MailException ;
11
13
use Magento \Framework \Model \AbstractModel ;
12
14
use Magento \Framework \Exception \AuthenticationException ;
13
15
use Magento \Framework \Serialize \Serializer \Json ;
@@ -40,10 +42,14 @@ class User extends AbstractModel implements StorageInterface, UserInterface
40
42
*/
41
43
const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'admin/emails/forgot_email_template ' ;
42
44
45
+ const XML_PATH_NEW_USER_EMAIL_TEMPLATE = 'admin/emails/new_user_notification_template ' ;
46
+
43
47
const XML_PATH_FORGOT_EMAIL_IDENTITY = 'admin/emails/forgot_email_identity ' ;
44
48
45
49
const XML_PATH_USER_NOTIFICATION_TEMPLATE = 'admin/emails/user_notification_template ' ;
46
50
51
+ const DEPLOYMENT_CONFIG_ADMIN_EMAIL = 'user_admin_email ' ;
52
+
47
53
/** @deprecated */
48
54
const XML_PATH_RESET_PASSWORD_TEMPLATE = 'admin/emails/reset_password_template ' ;
49
55
@@ -121,6 +127,11 @@ class User extends AbstractModel implements StorageInterface, UserInterface
121
127
*/
122
128
private $ serializer ;
123
129
130
+ /**
131
+ * @var DeploymentConfig
132
+ */
133
+ private $ deploymentConfig ;
134
+
124
135
/**
125
136
* @param \Magento\Framework\Model\Context $context
126
137
* @param \Magento\Framework\Registry $registry
@@ -136,6 +147,7 @@ class User extends AbstractModel implements StorageInterface, UserInterface
136
147
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
137
148
* @param array $data
138
149
* @param Json $serializer
150
+ * @param DeploymentConfig|null $deploymentConfig
139
151
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
140
152
*/
141
153
public function __construct (
@@ -152,7 +164,8 @@ public function __construct(
152
164
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
153
165
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
154
166
array $ data = [],
155
- Json $ serializer = null
167
+ Json $ serializer = null ,
168
+ DeploymentConfig $ deploymentConfig = null
156
169
) {
157
170
$ this ->_encryptor = $ encryptor ;
158
171
parent ::__construct ($ context , $ registry , $ resource , $ resourceCollection , $ data );
@@ -164,6 +177,8 @@ public function __construct(
164
177
$ this ->_storeManager = $ storeManager ;
165
178
$ this ->validationRules = $ validationRules ;
166
179
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
180
+ $ this ->deploymentConfig = $ deploymentConfig
181
+ ?? ObjectManager::getInstance ()->get (DeploymentConfig::class);
167
182
}
168
183
169
184
/**
@@ -395,23 +410,58 @@ public function roleUserExists()
395
410
return is_array ($ result ) && count ($ result ) > 0 ? true : false ;
396
411
}
397
412
413
+ /**
414
+ * Send a notification to an admin.
415
+ *
416
+ * @param string $templateConfigId
417
+ * @param array $templateVars
418
+ * @param string|null $toEmail
419
+ * @param string|null $toName
420
+ * @throws MailException
421
+ *
422
+ * @return void
423
+ */
424
+ private function sendNotification (
425
+ string $ templateConfigId ,
426
+ array $ templateVars ,
427
+ string $ toEmail = null ,
428
+ string $ toName = null
429
+ ) {
430
+ $ toEmail = $ toEmail ?? $ this ->getEmail ();
431
+ $ toName = $ toName ?? $ this ->getName ();
432
+ $ this ->_transportBuilder
433
+ ->setTemplateIdentifier ($ this ->_config ->getValue ($ templateConfigId ))
434
+ ->setTemplateModel (\Magento \Email \Model \BackendTemplate::class)
435
+ ->setTemplateOptions ([
436
+ 'area ' => FrontNameResolver::AREA_CODE ,
437
+ 'store ' => Store::DEFAULT_STORE_ID
438
+ ])
439
+ ->setTemplateVars ($ templateVars )
440
+ ->setFrom (
441
+ $ this ->_config ->getValue (self ::XML_PATH_FORGOT_EMAIL_IDENTITY )
442
+ )
443
+ ->addTo ($ toEmail , $ toName )
444
+ ->getTransport ()
445
+ ->sendMessage ();
446
+ }
447
+
398
448
/**
399
449
* Send email with reset password confirmation link
400
450
*
401
451
* @return $this
402
452
*/
403
453
public function sendPasswordResetConfirmationEmail ()
404
454
{
405
- $ templateId = $ this ->_config ->getValue (self ::XML_PATH_FORGOT_EMAIL_TEMPLATE );
406
- $ transport = $ this ->_transportBuilder ->setTemplateIdentifier ($ templateId )
407
- ->setTemplateModel (\Magento \Email \Model \BackendTemplate::class)
408
- ->setTemplateOptions (['area ' => FrontNameResolver::AREA_CODE , 'store ' => Store::DEFAULT_STORE_ID ])
409
- ->setTemplateVars (['user ' => $ this , 'store ' => $ this ->_storeManager ->getStore (Store::DEFAULT_STORE_ID )])
410
- ->setFrom ($ this ->_config ->getValue (self ::XML_PATH_FORGOT_EMAIL_IDENTITY ))
411
- ->addTo ($ this ->getEmail (), $ this ->getName ())
412
- ->getTransport ();
455
+ $ this ->sendNotification (
456
+ self ::XML_PATH_FORGOT_EMAIL_TEMPLATE ,
457
+ [
458
+ 'user ' => $ this ,
459
+ 'store ' => $ this ->_storeManager ->getStore (
460
+ Store::DEFAULT_STORE_ID
461
+ )
462
+ ]
463
+ );
413
464
414
- $ transport ->sendMessage ();
415
465
return $ this ;
416
466
}
417
467
@@ -427,19 +477,63 @@ public function sendPasswordResetNotificationEmail()
427
477
return $ this ;
428
478
}
429
479
480
+ /**
481
+ * Send notification about a new user created.
482
+ *
483
+ * @throws MailException
484
+ * @return void
485
+ */
486
+ private function sendNewUserNotificationEmail ()
487
+ {
488
+ $ toEmails = [];
489
+ $ generalEmail = $ this ->_config ->getValue (
490
+ 'trans_email/ident_general/email '
491
+ );
492
+ if ($ generalEmail ) {
493
+ $ toEmails [] = $ generalEmail ;
494
+ }
495
+ $ adminEmail = $ this ->deploymentConfig ->get (
496
+ static ::DEPLOYMENT_CONFIG_ADMIN_EMAIL
497
+ );
498
+ if ($ adminEmail ) {
499
+ $ toEmails [] = $ adminEmail ;
500
+ }
501
+
502
+ foreach ($ toEmails as $ toEmail ) {
503
+ $ this ->sendNotification (
504
+ self ::XML_PATH_NEW_USER_EMAIL_TEMPLATE ,
505
+ [
506
+ 'user ' => $ this ,
507
+ 'store ' => $ this ->_storeManager ->getStore (
508
+ Store::DEFAULT_STORE_ID
509
+ )
510
+ ],
511
+ $ toEmail ,
512
+ 'Administrator '
513
+ );
514
+ }
515
+ }
516
+
430
517
/**
431
518
* Check changes and send notification emails
432
519
*
520
+ * @throws MailException
433
521
* @return $this
434
522
* @since 100.1.0
435
523
*/
436
524
public function sendNotificationEmailsIfRequired ()
437
525
{
438
- $ changes = $ this ->createChangesDescriptionString ();
439
-
440
- if ($ changes ) {
441
- if ($ this ->getEmail () != $ this ->getOrigData ('email ' ) && $ this ->getOrigData ('email ' )) {
442
- $ this ->sendUserNotificationEmail ($ changes , $ this ->getOrigData ('email ' ));
526
+ if ($ this ->isObjectNew ()) {
527
+ //Notification about a new user
528
+ $ this ->sendNewUserNotificationEmail ();
529
+ } elseif ($ changes = $ this ->createChangesDescriptionString ()) {
530
+ if ($ this ->getEmail () != $ this ->getOrigData ('email ' )
531
+ && $ this ->getOrigData ('email ' )
532
+ ) {
533
+ $ this ->sendUserNotificationEmail (
534
+ $ changes ,
535
+ $ this ->getOrigData ('email ' )
536
+ );
443
537
}
444
538
$ this ->sendUserNotificationEmail ($ changes );
445
539
}
@@ -479,31 +573,24 @@ protected function createChangesDescriptionString()
479
573
*
480
574
* @param string $changes
481
575
* @param string $email
576
+ * @throws MailException
482
577
* @return $this
483
578
* @since 100.1.0
484
579
*/
485
580
protected function sendUserNotificationEmail ($ changes , $ email = null )
486
581
{
487
- if ($ email === null ) {
488
- $ email = $ this ->getEmail ();
489
- }
490
-
491
- $ transport = $ this ->_transportBuilder
492
- ->setTemplateIdentifier ($ this ->_config ->getValue (self ::XML_PATH_USER_NOTIFICATION_TEMPLATE ))
493
- ->setTemplateModel (\Magento \Email \Model \BackendTemplate::class)
494
- ->setTemplateOptions (['area ' => FrontNameResolver::AREA_CODE , 'store ' => Store::DEFAULT_STORE_ID ])
495
- ->setTemplateVars (
496
- [
497
- 'user ' => $ this ,
498
- 'store ' => $ this ->_storeManager ->getStore (Store::DEFAULT_STORE_ID ),
499
- 'changes ' => $ changes
500
- ]
501
- )
502
- ->setFrom ($ this ->_config ->getValue (self ::XML_PATH_FORGOT_EMAIL_IDENTITY ))
503
- ->addTo ($ email , $ this ->getName ())
504
- ->getTransport ();
582
+ $ this ->sendNotification (
583
+ self ::XML_PATH_USER_NOTIFICATION_TEMPLATE ,
584
+ [
585
+ 'user ' => $ this ,
586
+ 'store ' => $ this ->_storeManager ->getStore (
587
+ Store::DEFAULT_STORE_ID
588
+ ),
589
+ 'changes ' => $ changes
590
+ ],
591
+ $ email
592
+ );
505
593
506
- $ transport ->sendMessage ();
507
594
return $ this ;
508
595
}
509
596
0 commit comments