Skip to content

Commit 158d1ee

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-92720: E-mail admin users when a new administrator is created.
1 parent 5146696 commit 158d1ee

File tree

12 files changed

+382
-763
lines changed

12 files changed

+382
-763
lines changed

app/code/Magento/User/Controller/Adminhtml/Auth/Forgotpassword.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
*/
77
namespace Magento\User\Controller\Adminhtml\Auth;
88

9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Security\Model\SecurityManager;
11+
use Magento\User\Model\Spi\NotificatorInterface;
1012

1113
class Forgotpassword extends \Magento\User\Controller\Adminhtml\Auth
1214
{
@@ -15,18 +17,27 @@ class Forgotpassword extends \Magento\User\Controller\Adminhtml\Auth
1517
*/
1618
protected $securityManager;
1719

20+
/**
21+
* @var NotificatorInterface
22+
*/
23+
private $notificator;
24+
1825
/**
1926
* @param \Magento\Backend\App\Action\Context $context
2027
* @param \Magento\User\Model\UserFactory $userFactory
2128
* @param \Magento\Security\Model\SecurityManager $securityManager
29+
* @param NotificatorInterface|null $notificator
2230
*/
2331
public function __construct(
2432
\Magento\Backend\App\Action\Context $context,
2533
\Magento\User\Model\UserFactory $userFactory,
26-
\Magento\Security\Model\SecurityManager $securityManager
34+
\Magento\Security\Model\SecurityManager $securityManager,
35+
?NotificatorInterface $notificator = null
2736
) {
2837
parent::__construct($context, $userFactory);
2938
$this->securityManager = $securityManager;
39+
$this->notificator = $notificator
40+
?? ObjectManager::getInstance()->get(NotificatorInterface::class);
3041
}
3142

3243
/**
@@ -70,7 +81,7 @@ public function execute()
7081
)->generateResetPasswordLinkToken();
7182
$user->changeResetPasswordLinkToken($newPassResetToken);
7283
$user->save();
73-
$user->sendPasswordResetConfirmationEmail();
84+
$this->notificator->sendForgotPassword($user);
7485
}
7586
break;
7687
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\Exception\AuthenticationException;
1010
use Magento\Framework\Exception\State\UserLockedException;
1111
use Magento\Security\Model\SecurityCookie;
12+
use Magento\User\Model\Spi\NotificationException;
1213

1314
/**
1415
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -87,17 +88,19 @@ public function execute()
8788
$currentUser->performIdentityCheck($data[$currentUserPasswordField]);
8889
$model->save();
8990

90-
$model->sendNotificationEmailsIfRequired();
91-
9291
$this->messageManager->addSuccess(__('You saved the user.'));
9392
$this->_getSession()->setUserData(false);
9493
$this->_redirect('adminhtml/*/');
94+
95+
$model->sendNotificationEmailsIfRequired();
9596
} catch (UserLockedException $e) {
9697
$this->_auth->logout();
9798
$this->getSecurityCookie()->setLogoutReasonCookie(
9899
\Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED
99100
);
100101
$this->_redirect('adminhtml/*/');
102+
} catch (NotificationException $exception) {
103+
$this->messageManager->addErrorMessage($exception->getMessage());
101104
} catch (\Magento\Framework\Exception\AuthenticationException $e) {
102105
$this->messageManager->addError(
103106
__('The password entered for the current user is invalid. Verify the password and try again.')
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\User\Model;
10+
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Exception\MailException;
13+
use Magento\Store\Model\Store;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
use Magento\User\Api\Data\UserInterface;
16+
use Magento\User\Model\Spi\NotificatorInterface;
17+
use Magento\Backend\App\ConfigInterface;
18+
use Magento\Framework\Mail\Template\TransportBuilder;
19+
use Magento\Framework\App\DeploymentConfig;
20+
use Magento\Backend\App\Area\FrontNameResolver;
21+
22+
/**
23+
* @inheritDoc
24+
*/
25+
class Notificator implements NotificatorInterface
26+
{
27+
/**
28+
* @var TransportBuilder
29+
*/
30+
private $transportBuilder;
31+
32+
/**
33+
* @var ConfigInterface
34+
*/
35+
private $config;
36+
37+
/**
38+
* @var DeploymentConfig
39+
*/
40+
private $deployConfig;
41+
42+
/**
43+
* @var StoreManagerInterface
44+
*/
45+
private $storeManager;
46+
47+
/**
48+
* @param TransportBuilder $transportBuilder
49+
* @param ConfigInterface $config
50+
* @param DeploymentConfig $deployConfig
51+
* @param StoreManagerInterface $storeManager
52+
*/
53+
public function __construct(
54+
TransportBuilder $transportBuilder,
55+
ConfigInterface $config,
56+
DeploymentConfig $deployConfig,
57+
StoreManagerInterface $storeManager
58+
) {
59+
$this->transportBuilder = $transportBuilder;
60+
$this->config = $config;
61+
$this->deployConfig = $deployConfig;
62+
$this->storeManager = $storeManager;
63+
}
64+
65+
/**
66+
* Send a notification.
67+
*
68+
* @param string $templateConfigId
69+
* @param array $templateVars
70+
* @param string $toEmail
71+
* @param string $toName
72+
* @throws MailException
73+
*
74+
* @return void
75+
*/
76+
private function sendNotification(
77+
string $templateConfigId,
78+
array $templateVars,
79+
string $toEmail,
80+
string $toName
81+
): void {
82+
$transport = $this->transportBuilder
83+
->setTemplateIdentifier($this->config->getValue($templateConfigId))
84+
->setTemplateModel(\Magento\Email\Model\BackendTemplate::class)
85+
->setTemplateOptions([
86+
'area' => FrontNameResolver::AREA_CODE,
87+
'store' => Store::DEFAULT_STORE_ID
88+
])
89+
->setTemplateVars($templateVars)
90+
->setFrom(
91+
$this->config->getValue('admin/emails/forgot_email_identity')
92+
)
93+
->addTo($toEmail, $toName)
94+
->getTransport();
95+
$transport->sendMessage();
96+
}
97+
98+
/**
99+
* @inheritDoc
100+
*/
101+
public function sendForgotPassword(UserInterface $user): void
102+
{
103+
try {
104+
$this->sendNotification(
105+
'admin/emails/forgot_email_template',
106+
[
107+
'user' => $user,
108+
'store' => $this->storeManager->getStore(
109+
Store::DEFAULT_STORE_ID
110+
)
111+
],
112+
$user->getEmail(),
113+
$user->getFirstName().' '.$user->getLastName()
114+
);
115+
} catch (LocalizedException $exception) {
116+
throw new NotificatorException(
117+
__($exception->getMessage()),
118+
$exception
119+
);
120+
}
121+
}
122+
123+
/**
124+
* @inheritDoc
125+
*/
126+
public function sendCreated(UserInterface $user): void
127+
{
128+
$toEmails = [];
129+
$generalEmail = $this->config->getValue(
130+
'trans_email/ident_general/email'
131+
);
132+
if ($generalEmail) {
133+
$toEmails[] = $generalEmail;
134+
}
135+
if ($adminEmail = $this->deployConfig->get('user_admin_email')) {
136+
$toEmails[] = $adminEmail;
137+
}
138+
139+
try {
140+
foreach ($toEmails as $toEmail) {
141+
$this->sendNotification(
142+
'admin/emails/new_user_notification_template',
143+
[
144+
'user' => $user,
145+
'store' => $this->storeManager->getStore(
146+
Store::DEFAULT_STORE_ID
147+
)
148+
],
149+
$toEmail,
150+
__('Administrator')->getText()
151+
);
152+
}
153+
} catch (LocalizedException $exception) {
154+
throw new NotificatorException(
155+
__($exception->getMessage()),
156+
$exception
157+
);
158+
}
159+
}
160+
161+
/**
162+
* @inheritDoc
163+
*/
164+
public function sendUpdated(UserInterface $user, array $changed): void
165+
{
166+
$email = $user->getEmail();
167+
if ($user instanceof User) {
168+
$email = $user->getOrigData('email');
169+
}
170+
171+
try {
172+
$this->sendNotification(
173+
'admin/emails/user_notification_template',
174+
[
175+
'user' => $user,
176+
'store' => $this->storeManager->getStore(
177+
Store::DEFAULT_STORE_ID
178+
),
179+
'changes' => implode(', ', $changed)
180+
],
181+
$email,
182+
$user->getFirstName().' '.$user->getLastName()
183+
);
184+
} catch (LocalizedException $exception) {
185+
throw new NotificatorException(
186+
__($exception->getMessage()),
187+
$exception
188+
);
189+
}
190+
}
191+
192+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\User\Model;
10+
11+
use Magento\Framework\Exception\MailException;
12+
use Magento\User\Model\Spi\NotificationException;
13+
14+
/**
15+
* When notificator cannot send an email.
16+
*/
17+
class NotificatorException extends MailException implements NotificationException
18+
{
19+
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\User\Model\Spi;
10+
11+
/**
12+
* When a notification cannot be sent.
13+
*/
14+
interface NotificationException extends \Throwable
15+
{
16+
17+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\User\Model\Spi;
10+
11+
use Magento\User\Api\Data\UserInterface;
12+
13+
/**
14+
* Use to send out notifications about user related events.
15+
*/
16+
interface NotificatorInterface
17+
{
18+
/**
19+
* Send notification when a user requests password reset.
20+
*
21+
* @param UserInterface $user User that requested password reset.
22+
* @throws NotificationException
23+
*
24+
* @return void
25+
*/
26+
public function sendForgotPassword(UserInterface $user): void;
27+
28+
/**
29+
* Send a notification when a new user is created.
30+
*
31+
* @param UserInterface $user The new user.
32+
* @throws NotificationException
33+
*
34+
* @return void
35+
*/
36+
public function sendCreated(UserInterface $user): void;
37+
38+
/**
39+
* Send a notification when a user is updated.
40+
*
41+
* @param UserInterface $user The user updated.
42+
* @param string[] List of changed properties.
43+
* @throws NotificationException
44+
*
45+
* @return void
46+
*/
47+
public function sendUpdated(UserInterface $user, array $changed): void;
48+
}

0 commit comments

Comments
 (0)