Skip to content

Commit e21f4a1

Browse files
author
Dmytro Voskoboinikov
committed
Merge branch 'MAGETWO-92722' into 2.2.6-bugfixes-160718
2 parents 721c91d + 667f7cd commit e21f4a1

File tree

6 files changed

+148
-736
lines changed

6 files changed

+148
-736
lines changed

app/code/Magento/User/Controller/Adminhtml/User/Save.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\User\Controller\Adminhtml\User;
77

88
use Magento\Framework\Exception\AuthenticationException;
9+
use Magento\Framework\Exception\MailException;
910
use Magento\Framework\Exception\State\UserLockedException;
1011
use Magento\Security\Model\SecurityCookie;
1112

@@ -84,17 +85,19 @@ public function execute()
8485
$currentUser->performIdentityCheck($data[$currentUserPasswordField]);
8586
$model->save();
8687

87-
$model->sendNotificationEmailsIfRequired();
88-
8988
$this->messageManager->addSuccess(__('You saved the user.'));
9089
$this->_getSession()->setUserData(false);
9190
$this->_redirect('adminhtml/*/');
91+
92+
$model->sendNotificationEmailsIfRequired();
9293
} catch (UserLockedException $e) {
9394
$this->_auth->logout();
9495
$this->getSecurityCookie()->setLogoutReasonCookie(
9596
\Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED
9697
);
9798
$this->_redirect('adminhtml/*/');
99+
} catch (MailException $exception) {
100+
$this->messageManager->addErrorMessage($exception->getMessage());
98101
} catch (\Magento\Framework\Exception\AuthenticationException $e) {
99102
$this->messageManager->addError(__('You have entered an invalid password for current user.'));
100103
$this->redirectToEdit($model, $data);

app/code/Magento/User/Model/User.php

Lines changed: 116 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
use Magento\Backend\App\Area\FrontNameResolver;
99
use Magento\Backend\Model\Auth\Credential\StorageInterface;
10+
use Magento\Framework\App\DeploymentConfig;
1011
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Exception\MailException;
1113
use Magento\Framework\Model\AbstractModel;
1214
use Magento\Framework\Exception\AuthenticationException;
1315
use Magento\Framework\Serialize\Serializer\Json;
@@ -123,6 +125,11 @@ class User extends AbstractModel implements StorageInterface, UserInterface
123125
*/
124126
private $serializer;
125127

128+
/**
129+
* @var DeploymentConfig
130+
*/
131+
private $deploymentConfig;
132+
126133
/**
127134
* @param \Magento\Framework\Model\Context $context
128135
* @param \Magento\Framework\Registry $registry
@@ -138,6 +145,7 @@ class User extends AbstractModel implements StorageInterface, UserInterface
138145
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
139146
* @param array $data
140147
* @param Json $serializer
148+
* @param DeploymentConfig|null $deploymentConfig
141149
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
142150
*/
143151
public function __construct(
@@ -154,7 +162,8 @@ public function __construct(
154162
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
155163
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
156164
array $data = [],
157-
Json $serializer = null
165+
Json $serializer = null,
166+
DeploymentConfig $deploymentConfig = null
158167
) {
159168
$this->_encryptor = $encryptor;
160169
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
@@ -166,6 +175,8 @@ public function __construct(
166175
$this->_storeManager = $storeManager;
167176
$this->validationRules = $validationRules;
168177
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
178+
$this->deploymentConfig = $deploymentConfig
179+
?? ObjectManager::getInstance()->get(DeploymentConfig::class);
169180
}
170181

171182
/**
@@ -397,23 +408,58 @@ public function roleUserExists()
397408
return is_array($result) && count($result) > 0 ? true : false;
398409
}
399410

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+
400446
/**
401447
* Send email with reset password confirmation link
402448
*
403449
* @return $this
404450
*/
405451
public function sendPasswordResetConfirmationEmail()
406452
{
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+
);
415462

416-
$transport->sendMessage();
417463
return $this;
418464
}
419465

@@ -429,19 +475,62 @@ public function sendPasswordResetNotificationEmail()
429475
return $this;
430476
}
431477

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+
432514
/**
433515
* Check changes and send notification emails
434516
*
517+
* @throws MailException
435518
* @return $this
436519
* @since 100.1.0
437520
*/
438521
public function sendNotificationEmailsIfRequired()
439522
{
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+
);
445534
}
446535
$this->sendUserNotificationEmail($changes);
447536
}
@@ -481,31 +570,24 @@ protected function createChangesDescriptionString()
481570
*
482571
* @param string $changes
483572
* @param string $email
573+
* @throws MailException
484574
* @return $this
485575
* @since 100.1.0
486576
*/
487577
protected function sendUserNotificationEmail($changes, $email = null)
488578
{
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+
);
507590

508-
$transport->sendMessage();
509591
return $this;
510592
}
511593

0 commit comments

Comments
 (0)