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