Skip to content

Commit 19b6d8a

Browse files
author
Dmytro Voskoboinikov
committed
Merge branch 'MAGETWO-92721' into 2.1.15-bugfixes-120718
2 parents c8b4c7b + fca9652 commit 19b6d8a

File tree

6 files changed

+147
-721
lines changed

6 files changed

+147
-721
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

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

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

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

Lines changed: 115 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Magento\Framework\Exception\AuthenticationException;
1212
use Magento\Store\Model\Store;
1313
use Magento\User\Api\Data\UserInterface;
14+
use Magento\Framework\App\DeploymentConfig;
15+
use Magento\Framework\Exception\MailException;
16+
use Magento\Framework\App\ObjectManager;
1417

1518
/**
1619
* Admin user model
@@ -113,6 +116,11 @@ class User extends AbstractModel implements StorageInterface, UserInterface
113116
*/
114117
protected $validationRules;
115118

119+
/**
120+
* @var DeploymentConfig
121+
*/
122+
private $deploymentConfig;
123+
116124
/**
117125
* @param \Magento\Framework\Model\Context $context
118126
* @param \Magento\Framework\Registry $registry
@@ -126,6 +134,7 @@ class User extends AbstractModel implements StorageInterface, UserInterface
126134
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
127135
* @param UserValidationRules $validationRules
128136
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
137+
* @param DeploymentConfig|null $deploymentConfig
129138
* @param array $data
130139
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
131140
*/
@@ -142,7 +151,8 @@ public function __construct(
142151
UserValidationRules $validationRules,
143152
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
144153
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
145-
array $data = []
154+
array $data = [],
155+
DeploymentConfig $deploymentConfig = null
146156
) {
147157
$this->_encryptor = $encryptor;
148158
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
@@ -153,6 +163,8 @@ public function __construct(
153163
$this->_transportBuilder = $transportBuilder;
154164
$this->_storeManager = $storeManager;
155165
$this->validationRules = $validationRules;
166+
$this->deploymentConfig = $deploymentConfig
167+
?: ObjectManager::getInstance()->get(DeploymentConfig::class);
156168
}
157169

158170
/**
@@ -382,23 +394,59 @@ public function roleUserExists()
382394
return is_array($result) && count($result) > 0 ? true : false;
383395
}
384396

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+
385432
/**
386433
* Send email with reset password confirmation link
387434
*
435+
* @throws MailException
388436
* @return $this
389437
*/
390438
public function sendPasswordResetConfirmationEmail()
391439
{
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+
);
400449

401-
$transport->sendMessage();
402450
return $this;
403451
}
404452

@@ -414,20 +462,59 @@ public function sendPasswordResetNotificationEmail()
414462
return $this;
415463
}
416464

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+
417499
/**
418500
* Check changes and send notification emails
419501
*
502+
* @throws MailException
420503
* @return $this
421504
*/
422505
public function sendNotificationEmailsIfRequired()
423506
{
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');
429516
}
430-
$this->sendUserNotificationEmail($changes);
517+
$this->sendUserNotificationEmail($changes, $email);
431518
}
432519

433520
return $this;
@@ -464,30 +551,23 @@ protected function createChangesDescriptionString()
464551
*
465552
* @param string $changes
466553
* @param string $email
554+
* @throws MailException
467555
* @return $this
468556
*/
469557
protected function sendUserNotificationEmail($changes, $email = null)
470558
{
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+
);
489570

490-
$transport->sendMessage();
491571
return $this;
492572
}
493573

0 commit comments

Comments
 (0)