Skip to content

Commit 0b8db7e

Browse files
author
Oleksii Korshenko
authored
Merge pull request #363 from magento-fearless-kiwis/FearlessKiwis-MAGETWO-57065-admin-stuck-on-changing-password-2.0
Fixed issue: - MAGETWO-57065: [Github] admin stuck on " It's time to change your password." #4331- fix for 2.0
2 parents 368f241 + a4cf7c7 commit 0b8db7e

File tree

7 files changed

+42
-56
lines changed

7 files changed

+42
-56
lines changed

app/code/Magento/Backend/Controller/Adminhtml/System/Account/Save.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,19 @@ public function execute()
5252
$user->setPassword($password);
5353
$user->setPasswordConfirmation($passwordConfirmation);
5454
}
55-
$user->save();
56-
/** Send password reset email notification only when password was changed */
57-
if ($password !== '') {
58-
$user->sendPasswordResetNotificationEmail();
55+
$errors = $user->validate();
56+
if ($errors !== true && !empty($errors)) {
57+
foreach ($errors as $error) {
58+
$this->messageManager->addError($error);
59+
}
60+
} else {
61+
$user->save();
62+
/** Send password reset email notification only when password was changed */
63+
if ($password !== '') {
64+
$user->sendPasswordResetNotificationEmail();
65+
}
66+
$this->messageManager->addSuccess(__('You saved the account.'));
5967
}
60-
$this->messageManager->addSuccess(__('You saved the account.'));
6168
} catch (ValidatorException $e) {
6269
$this->messageManager->addMessages($e->getMessages());
6370
if ($e->getMessage()) {

app/code/Magento/Backend/Test/Unit/Controller/Adminhtml/System/Account/SaveTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ protected function setUp()
7676
$this->_userMock = $this->getMockBuilder('Magento\User\Model\User')
7777
->disableOriginalConstructor()
7878
->setMethods(
79-
['load', 'save', 'sendPasswordResetNotificationEmail', 'verifyIdentity', '__sleep', '__wakeup']
79+
[
80+
'load', 'save', 'sendPasswordResetNotificationEmail',
81+
'verifyIdentity', 'validate', '__sleep', '__wakeup'
82+
]
8083
)
8184
->getMock();
8285

@@ -193,6 +196,7 @@ public function testSaveAction()
193196

194197
$this->_userMock->expects($this->once())->method('save');
195198
$this->_userMock->expects($this->once())->method('verifyIdentity')->will($this->returnValue(true));
199+
$this->_userMock->expects($this->once())->method('validate')->willReturn(true);
196200
$this->_userMock->expects($this->once())->method('sendPasswordResetNotificationEmail');
197201

198202
$this->_requestMock->setParams($requestParams);

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,22 @@ public function execute()
4040
$user->setRpToken(null);
4141
$user->setRpTokenCreatedAt(null);
4242
try {
43-
$user->save();
44-
$this->messageManager->addSuccess(__('You updated your password.'));
45-
$this->getResponse()->setRedirect(
46-
$this->_objectManager->get('Magento\Backend\Helper\Data')->getHomePageUrl()
47-
);
43+
$errors = $user->validate();
44+
if ($errors !== true && !empty($errors)) {
45+
foreach ($errors as $error) {
46+
$this->messageManager->addError($error);
47+
$this->_redirect(
48+
'adminhtml/auth/resetpassword',
49+
['_nosecret' => true, '_query' => ['id' => $userId, 'token' => $passwordResetToken]]
50+
);
51+
}
52+
} else {
53+
$user->save();
54+
$this->messageManager->addSuccess(__('You updated your password.'));
55+
$this->getResponse()->setRedirect(
56+
$this->_objectManager->get(\Magento\Backend\Helper\Data::class)->getHomePageUrl()
57+
);
58+
}
4859
} catch (\Magento\Framework\Validator\Exception $exception) {
4960
$this->messageManager->addMessages($exception->getMessages());
5061
$this->_redirect(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,8 @@ protected function validatePasswordChange()
293293
}
294294

295295
// Check whether password was used before
296-
$passwordHash = $this->_encryptor->getHash($password, false);
297296
foreach ($this->getResource()->getOldPasswords($this) as $oldPasswordHash) {
298-
if ($passwordHash === $oldPasswordHash) {
297+
if ($this->_encryptor->isValidHash($password, $oldPasswordHash)) {
299298
return [$errorMessage];
300299
}
301300
}

app/code/Magento/User/Observer/Backend/TrackAdminNewPasswordObserver.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ class TrackAdminNewPasswordObserver implements ObserverInterface
3535
*/
3636
protected $authSession;
3737

38-
/**
39-
* Encryption model
40-
*
41-
* @var \Magento\Framework\Encryption\EncryptorInterface
42-
*/
43-
protected $encryptor;
44-
4538
/**
4639
* Message manager interface
4740
*
@@ -53,20 +46,17 @@ class TrackAdminNewPasswordObserver implements ObserverInterface
5346
* @param \Magento\User\Model\Backend\Config\ObserverConfig $observerConfig
5447
* @param \Magento\User\Model\ResourceModel\User $userResource
5548
* @param \Magento\Backend\Model\Auth\Session $authSession
56-
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
5749
* @param \Magento\Framework\Message\ManagerInterface $messageManager
5850
*/
5951
public function __construct(
6052
\Magento\User\Model\Backend\Config\ObserverConfig $observerConfig,
6153
\Magento\User\Model\ResourceModel\User $userResource,
6254
\Magento\Backend\Model\Auth\Session $authSession,
63-
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
6455
\Magento\Framework\Message\ManagerInterface $messageManager
6556
) {
6657
$this->observerConfig = $observerConfig;
6758
$this->userResource = $userResource;
6859
$this->authSession = $authSession;
69-
$this->encryptor = $encryptor;
7060
$this->messageManager = $messageManager;
7161
}
7262

@@ -81,10 +71,9 @@ public function execute(EventObserver $observer)
8171
/* @var $user \Magento\User\Model\User */
8272
$user = $observer->getEvent()->getObject();
8373
if ($user->getId()) {
84-
$password = $user->getCurrentPassword();
74+
$passwordHash = $user->getPassword();
8575
$passwordLifetime = $this->observerConfig->getAdminPasswordLifetime();
86-
if ($passwordLifetime && $password && !$user->getForceNewPassword()) {
87-
$passwordHash = $this->encryptor->getHash($password, false);
76+
if ($passwordLifetime && $passwordHash && !$user->getForceNewPassword()) {
8877
$this->userResource->trackPassword($user, $passwordHash, $passwordLifetime);
8978
$this->messageManager->getMessages()->deleteMessageByIdentifier('magento_user_password_expired');
9079
$this->authSession->unsPciAdminUserIsPasswordExpired();

app/code/Magento/User/Test/Unit/Model/UserTest.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -658,15 +658,9 @@ public function testCheckPasswordChangeEqualToPrevious()
658658
$this->model->setPassword($newPassword)
659659
->setId(1)
660660
->setOrigData('password', $oldPassword);
661-
$this->encryptorMock->expects($this->once())
661+
$this->encryptorMock->expects($this->atLeastOnce())
662662
->method('isValidHash')
663-
->with($newPassword, $oldPassword)
664-
->willReturn(false);
665-
666-
$this->encryptorMock->expects($this->once())
667-
->method('getHash')
668-
->with($newPassword, false)
669-
->willReturn($newPasswordHash);
663+
->will($this->onConsecutiveCalls(false, true));
670664

671665
$this->resourceMock->expects($this->once())->method('getOldPasswords')->willReturn(['hash1', $newPasswordHash]);
672666

@@ -690,20 +684,13 @@ public function testCheckPasswordChangeValid()
690684
$validatorMock->expects($this->once())->method('isValid')->willReturn(true);
691685

692686
$newPassword = "NEWmYn3wpassw0rd";
693-
$newPasswordHash = "new password hash";
694687
$oldPassword = "OLDmYn3wpassw0rd";
695688
$this->model->setPassword($newPassword)
696689
->setId(1)
697690
->setOrigData('password', $oldPassword);
698-
$this->encryptorMock->expects($this->once())
691+
$this->encryptorMock->expects($this->atLeastOnce())
699692
->method('isValidHash')
700-
->with($newPassword, $oldPassword)
701-
->willReturn(false);
702-
703-
$this->encryptorMock->expects($this->once())
704-
->method('getHash')
705-
->with($newPassword, false)
706-
->willReturn($newPasswordHash);
693+
->will($this->onConsecutiveCalls(false, false, false));
707694

708695
$this->resourceMock->expects($this->once())->method('getOldPasswords')->willReturn(['hash1', 'hash2']);
709696

app/code/Magento/User/Test/Unit/Observer/Backend/TrackAdminNewPasswordObserverTest.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class TrackAdminNewPasswordObserverTest extends \PHPUnit_Framework_TestCase
2424
/** @var \Magento\Backend\Model\Auth\Session|\PHPUnit_Framework_MockObject_MockObject */
2525
protected $authSessionMock;
2626

27-
/** @var \Magento\Framework\Encryption\EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject */
28-
protected $encryptorMock;
29-
3027
/** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
3128
protected $managerInterfaceMock;
3229

@@ -57,11 +54,6 @@ public function setUp()
5754
]
5855
)->getMock();
5956

60-
$this->encryptorMock = $this->getMockBuilder('\Magento\Framework\Encryption\EncryptorInterface')
61-
->disableOriginalConstructor()
62-
->setMethods([])
63-
->getMock();
64-
6557
$this->managerInterfaceMock = $this->getMockBuilder('Magento\Framework\Message\ManagerInterface')
6658
->disableOriginalConstructor()
6759
->setMethods([])
@@ -82,7 +74,6 @@ public function setUp()
8274
'observerConfig' => $this->observerConfig,
8375
'userResource' => $this->userMock,
8476
'authSession' => $this->authSessionMock,
85-
'encryptor' => $this->encryptorMock,
8677
'messageManager' => $this->managerInterfaceMock,
8778
]
8879
);
@@ -91,7 +82,6 @@ public function setUp()
9182
public function testTrackAdminPassword()
9283
{
9384
$newPW = "mYn3wpassw0rd";
94-
$oldPW = "notsecure";
9585
$uid = 123;
9686
/** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserverMock */
9787
$eventObserverMock = $this->getMockBuilder('Magento\Framework\Event\Observer')
@@ -108,19 +98,18 @@ public function testTrackAdminPassword()
10898
/** @var \Magento\User\Model\User|\PHPUnit_Framework_MockObject_MockObject $userMock */
10999
$userMock = $this->getMockBuilder('Magento\User\Model\User')
110100
->disableOriginalConstructor()
111-
->setMethods(['getId', 'getCurrentPassword', 'getForceNewPassword'])
101+
->setMethods(['getId', 'getPassword', 'getForceNewPassword'])
112102
->getMock();
113103

114104
$eventObserverMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
115105
$eventMock->expects($this->once())->method('getObject')->willReturn($userMock);
116106
$userMock->expects($this->once())->method('getId')->willReturn($uid);
117-
$userMock->expects($this->once())->method('getCurrentPassword')->willReturn($newPW);
107+
$userMock->expects($this->once())->method('getPassword')->willReturn($newPW);
118108
$this->configInterfaceMock
119109
->expects($this->atLeastOnce())
120110
->method('getValue')
121111
->willReturn(1);
122112
$userMock->expects($this->once())->method('getForceNewPassword')->willReturn(false);
123-
$this->encryptorMock->expects($this->once())->method('getHash')->willReturn(md5($oldPW));
124113

125114
/** @var \Magento\Framework\Message\Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
126115
$collectionMock = $this->getMockBuilder('Magento\Framework\Message\Collection')

0 commit comments

Comments
 (0)