Skip to content

Commit dbef5af

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-92722: [Backport for 2.2.x] E-mail admin users when a new administrator is created.
1 parent 8dfaa62 commit dbef5af

File tree

5 files changed

+151
-36
lines changed

5 files changed

+151
-36
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: 121 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;
@@ -40,10 +42,14 @@ class User extends AbstractModel implements StorageInterface, UserInterface
4042
*/
4143
const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'admin/emails/forgot_email_template';
4244

45+
const XML_PATH_NEW_USER_EMAIL_TEMPLATE = 'admin/emails/new_user_notification_template';
46+
4347
const XML_PATH_FORGOT_EMAIL_IDENTITY = 'admin/emails/forgot_email_identity';
4448

4549
const XML_PATH_USER_NOTIFICATION_TEMPLATE = 'admin/emails/user_notification_template';
4650

51+
const DEPLOYMENT_CONFIG_ADMIN_EMAIL = 'user_admin_email';
52+
4753
/** @deprecated */
4854
const XML_PATH_RESET_PASSWORD_TEMPLATE = 'admin/emails/reset_password_template';
4955

@@ -121,6 +127,11 @@ class User extends AbstractModel implements StorageInterface, UserInterface
121127
*/
122128
private $serializer;
123129

130+
/**
131+
* @var DeploymentConfig
132+
*/
133+
private $deploymentConfig;
134+
124135
/**
125136
* @param \Magento\Framework\Model\Context $context
126137
* @param \Magento\Framework\Registry $registry
@@ -136,6 +147,7 @@ class User extends AbstractModel implements StorageInterface, UserInterface
136147
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
137148
* @param array $data
138149
* @param Json $serializer
150+
* @param DeploymentConfig|null $deploymentConfig
139151
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
140152
*/
141153
public function __construct(
@@ -152,7 +164,8 @@ public function __construct(
152164
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
153165
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
154166
array $data = [],
155-
Json $serializer = null
167+
Json $serializer = null,
168+
DeploymentConfig $deploymentConfig = null
156169
) {
157170
$this->_encryptor = $encryptor;
158171
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
@@ -164,6 +177,8 @@ public function __construct(
164177
$this->_storeManager = $storeManager;
165178
$this->validationRules = $validationRules;
166179
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
180+
$this->deploymentConfig = $deploymentConfig
181+
?? ObjectManager::getInstance()->get(DeploymentConfig::class);
167182
}
168183

169184
/**
@@ -395,23 +410,58 @@ public function roleUserExists()
395410
return is_array($result) && count($result) > 0 ? true : false;
396411
}
397412

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+
398448
/**
399449
* Send email with reset password confirmation link
400450
*
401451
* @return $this
402452
*/
403453
public function sendPasswordResetConfirmationEmail()
404454
{
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+
);
413464

414-
$transport->sendMessage();
415465
return $this;
416466
}
417467

@@ -427,19 +477,63 @@ public function sendPasswordResetNotificationEmail()
427477
return $this;
428478
}
429479

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+
430517
/**
431518
* Check changes and send notification emails
432519
*
520+
* @throws MailException
433521
* @return $this
434522
* @since 100.1.0
435523
*/
436524
public function sendNotificationEmailsIfRequired()
437525
{
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+
);
443537
}
444538
$this->sendUserNotificationEmail($changes);
445539
}
@@ -479,31 +573,24 @@ protected function createChangesDescriptionString()
479573
*
480574
* @param string $changes
481575
* @param string $email
576+
* @throws MailException
482577
* @return $this
483578
* @since 100.1.0
484579
*/
485580
protected function sendUserNotificationEmail($changes, $email = null)
486581
{
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+
);
505593

506-
$transport->sendMessage();
507594
return $this;
508595
}
509596

app/code/Magento/User/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<admin>
1111
<emails>
1212
<forgot_email_template>admin_emails_forgot_email_template</forgot_email_template>
13+
<new_user_notification_template>admin_emails_new_user_notification_template</new_user_notification_template>
1314
<forgot_email_identity>general</forgot_email_identity>
1415
<user_notification_template>admin_emails_user_notification_template</user_notification_template>
1516
</emails>

app/code/Magento/User/etc/email_templates.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
99
<template id="admin_emails_forgot_email_template" label="Forgot Admin Password" file="password_reset_confirmation.html" type="text" module="Magento_User" area="adminhtml"/>
1010
<template id="admin_emails_user_notification_template" label="User Notification" file="user_notification.html" type="text" module="Magento_User" area="adminhtml"/>
11+
<template id="admin_emails_new_user_notification_template"
12+
label="New User Notification"
13+
file="new_user_notification.html"
14+
type="text"
15+
module="Magento_User"
16+
area="adminhtml"/>
1117
</config>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<!--@subject {{trans "New admin user '%user_name' created" user_name=$user.name}} @-->
8+
<!--@vars {
9+
"var store.getFrontendName()|escape":"Store Name"
10+
} @-->
11+
12+
{{trans "Hello,"}}
13+
14+
{{trans "A new admin account was created for %first_name, %last_name using %email." first_name=$user.first_name last_name=$user.last_name email=$user.email}}
15+
{{trans "If you have not authorized this action, please contact us immediately at %store_email" store_email=$store_email |escape}}{{depend store_phone}} {{trans "or call us at %store_phone" store_phone=$store_phone |escape}}{{/depend}}.
16+
17+
{{trans "Thanks,"}}
18+
{{var store.getFrontendName()}}

0 commit comments

Comments
 (0)