Skip to content

Commit 85d547b

Browse files
author
Dmytro Voskoboinikov
committed
Merge branch 'MAGETWO-92720' into 2.3-bugfixes-150918
2 parents 8487750 + a11533b commit 85d547b

File tree

13 files changed

+443
-800
lines changed

13 files changed

+443
-800
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
*/
77
namespace Magento\User\Controller\Adminhtml\Auth;
88

9-
use Magento\Security\Model\SecurityManager;
9+
use Magento\Framework\App\Action\HttpGetActionInterface;
10+
use Magento\Framework\App\Action\HttpPostActionInterface;
1011
use Magento\Framework\App\ObjectManager;
12+
use Magento\Security\Model\SecurityManager;
1113
use Magento\Backend\App\Action\Context;
1214
use Magento\User\Model\UserFactory;
1315
use Magento\User\Model\ResourceModel\User\CollectionFactory;
@@ -16,14 +18,25 @@
1618
use Magento\Framework\Exception\SecurityViolationException;
1719
use Magento\User\Controller\Adminhtml\Auth;
1820
use Magento\Backend\Helper\Data;
21+
use Magento\User\Model\Spi\NotificatorInterface;
1922

20-
class Forgotpassword extends Auth
23+
/**
24+
* Initiate forgot-password process.
25+
*
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
*/
28+
class Forgotpassword extends Auth implements HttpGetActionInterface, HttpPostActionInterface
2129
{
2230
/**
2331
* @var SecurityManager
2432
*/
2533
protected $securityManager;
2634

35+
/**
36+
* @var NotificatorInterface
37+
*/
38+
private $notificator;
39+
2740
/**
2841
* User model factory
2942
*
@@ -41,20 +54,25 @@ class Forgotpassword extends Auth
4154
* @param UserFactory $userFactory
4255
* @param SecurityManager $securityManager
4356
* @param CollectionFactory $userCollectionFactory
57+
* @param Data $backendDataHelper
58+
* @param NotificatorInterface|null $notificator
4459
*/
4560
public function __construct(
4661
Context $context,
4762
UserFactory $userFactory,
4863
SecurityManager $securityManager,
4964
CollectionFactory $userCollectionFactory = null,
50-
Data $backendDataHelper = null
65+
Data $backendDataHelper = null,
66+
?NotificatorInterface $notificator = null
5167
) {
5268
parent::__construct($context, $userFactory);
5369
$this->securityManager = $securityManager;
5470
$this->userCollectionFactory = $userCollectionFactory ?:
5571
ObjectManager::getInstance()->get(CollectionFactory::class);
5672
$this->backendDataHelper = $backendDataHelper ?:
5773
ObjectManager::getInstance()->get(Data::class);
74+
$this->notificator = $notificator
75+
?? ObjectManager::getInstance()->get(NotificatorInterface::class);
5876
}
5977

6078
/**
@@ -96,7 +114,7 @@ public function execute()
96114
$newPassResetToken = $this->backendDataHelper->generateResetPasswordLinkToken();
97115
$user->changeResetPasswordLinkToken($newPassResetToken);
98116
$user->save();
99-
$user->sendPasswordResetConfirmationEmail();
117+
$this->notificator->sendForgotPassword($user);
100118
}
101119
break;
102120
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
use Magento\Framework\Exception\AuthenticationException;
1111
use Magento\Framework\Exception\State\UserLockedException;
1212
use Magento\Security\Model\SecurityCookie;
13+
use Magento\User\Model\Spi\NotificationExceptionInterface;
1314

1415
/**
16+
* Save admin user.
17+
*
1518
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1619
*/
1720
class Save extends \Magento\User\Controller\Adminhtml\User implements HttpPostActionInterface
@@ -37,7 +40,7 @@ private function getSecurityCookie()
3740
}
3841

3942
/**
40-
* @return void
43+
* @inheritDoc
4144
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
4245
* @SuppressWarnings(PHPMD.NPathComplexity)
4346
*/
@@ -92,17 +95,19 @@ public function execute()
9295
$currentUser->performIdentityCheck($data[$currentUserPasswordField]);
9396
$model->save();
9497

95-
$model->sendNotificationEmailsIfRequired();
96-
9798
$this->messageManager->addSuccess(__('You saved the user.'));
9899
$this->_getSession()->setUserData(false);
99100
$this->_redirect('adminhtml/*/');
101+
102+
$model->sendNotificationEmailsIfRequired();
100103
} catch (UserLockedException $e) {
101104
$this->_auth->logout();
102105
$this->getSecurityCookie()->setLogoutReasonCookie(
103106
\Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED
104107
);
105108
$this->_redirect('adminhtml/*/');
109+
} catch (NotificationExceptionInterface $exception) {
110+
$this->messageManager->addErrorMessage($exception->getMessage());
106111
} catch (\Magento\Framework\Exception\AuthenticationException $e) {
107112
$this->messageManager->addError(
108113
__('The password entered for the current user is invalid. Verify the password and try again.')
@@ -121,6 +126,8 @@ public function execute()
121126
}
122127

123128
/**
129+
* Redirect to Edit form.
130+
*
124131
* @param \Magento\User\Model\User $model
125132
* @param array $data
126133
* @return void
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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+
use Magento\Email\Model\BackendTemplate;
22+
23+
/**
24+
* @inheritDoc
25+
*
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
*/
28+
class Notificator implements NotificatorInterface
29+
{
30+
/**
31+
* @var TransportBuilder
32+
*/
33+
private $transportBuilder;
34+
35+
/**
36+
* @var ConfigInterface
37+
*/
38+
private $config;
39+
40+
/**
41+
* @var DeploymentConfig
42+
*/
43+
private $deployConfig;
44+
45+
/**
46+
* @var StoreManagerInterface
47+
*/
48+
private $storeManager;
49+
50+
/**
51+
* @param TransportBuilder $transportBuilder
52+
* @param ConfigInterface $config
53+
* @param DeploymentConfig $deployConfig
54+
* @param StoreManagerInterface $storeManager
55+
*/
56+
public function __construct(
57+
TransportBuilder $transportBuilder,
58+
ConfigInterface $config,
59+
DeploymentConfig $deployConfig,
60+
StoreManagerInterface $storeManager
61+
) {
62+
$this->transportBuilder = $transportBuilder;
63+
$this->config = $config;
64+
$this->deployConfig = $deployConfig;
65+
$this->storeManager = $storeManager;
66+
}
67+
68+
/**
69+
* Send a notification.
70+
*
71+
* @param string $templateConfigId
72+
* @param array $templateVars
73+
* @param string $toEmail
74+
* @param string $toName
75+
* @throws MailException
76+
*
77+
* @return void
78+
*/
79+
private function sendNotification(
80+
string $templateConfigId,
81+
array $templateVars,
82+
string $toEmail,
83+
string $toName
84+
): void {
85+
$transport = $this->transportBuilder
86+
->setTemplateIdentifier($this->config->getValue($templateConfigId))
87+
->setTemplateModel(BackendTemplate::class)
88+
->setTemplateOptions([
89+
'area' => FrontNameResolver::AREA_CODE,
90+
'store' => Store::DEFAULT_STORE_ID
91+
])
92+
->setTemplateVars($templateVars)
93+
->setFrom(
94+
$this->config->getValue('admin/emails/forgot_email_identity')
95+
)
96+
->addTo($toEmail, $toName)
97+
->getTransport();
98+
$transport->sendMessage();
99+
}
100+
101+
/**
102+
* @inheritDoc
103+
*/
104+
public function sendForgotPassword(UserInterface $user): void
105+
{
106+
try {
107+
$this->sendNotification(
108+
'admin/emails/forgot_email_template',
109+
[
110+
'user' => $user,
111+
'store' => $this->storeManager->getStore(
112+
Store::DEFAULT_STORE_ID
113+
)
114+
],
115+
$user->getEmail(),
116+
$user->getFirstName().' '.$user->getLastName()
117+
);
118+
} catch (LocalizedException $exception) {
119+
throw new NotificatorException(
120+
__($exception->getMessage()),
121+
$exception
122+
);
123+
}
124+
}
125+
126+
/**
127+
* @inheritDoc
128+
*/
129+
public function sendCreated(UserInterface $user): void
130+
{
131+
$toEmails = [];
132+
$generalEmail = $this->config->getValue(
133+
'trans_email/ident_general/email'
134+
);
135+
if ($generalEmail) {
136+
$toEmails[] = $generalEmail;
137+
}
138+
if ($adminEmail = $this->deployConfig->get('user_admin_email')) {
139+
$toEmails[] = $adminEmail;
140+
}
141+
142+
try {
143+
foreach ($toEmails as $toEmail) {
144+
$this->sendNotification(
145+
'admin/emails/new_user_notification_template',
146+
[
147+
'user' => $user,
148+
'store' => $this->storeManager->getStore(
149+
Store::DEFAULT_STORE_ID
150+
)
151+
],
152+
$toEmail,
153+
__('Administrator')->getText()
154+
);
155+
}
156+
} catch (LocalizedException $exception) {
157+
throw new NotificatorException(
158+
__($exception->getMessage()),
159+
$exception
160+
);
161+
}
162+
}
163+
164+
/**
165+
* @inheritDoc
166+
*/
167+
public function sendUpdated(UserInterface $user, array $changed): void
168+
{
169+
$email = $user->getEmail();
170+
if ($user instanceof User) {
171+
$email = $user->getOrigData('email');
172+
}
173+
174+
try {
175+
$this->sendNotification(
176+
'admin/emails/user_notification_template',
177+
[
178+
'user' => $user,
179+
'store' => $this->storeManager->getStore(
180+
Store::DEFAULT_STORE_ID
181+
),
182+
'changes' => implode(', ', $changed)
183+
],
184+
$email,
185+
$user->getFirstName().' '.$user->getLastName()
186+
);
187+
} catch (LocalizedException $exception) {
188+
throw new NotificatorException(
189+
__($exception->getMessage()),
190+
$exception
191+
);
192+
}
193+
}
194+
}
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\NotificationExceptionInterface;
13+
14+
/**
15+
* When notificator cannot send an email.
16+
*/
17+
class NotificatorException extends MailException implements NotificationExceptionInterface
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 NotificationExceptionInterface extends \Throwable
15+
{
16+
17+
}

0 commit comments

Comments
 (0)