Skip to content

Commit ddf9598

Browse files
author
Mike Weis
committed
MAGETWO-57065: [Github] admin stuck on " It's time to change your password." #4331- fix for 2.0
- ported mainline fix
1 parent a669a95 commit ddf9598

File tree

6 files changed

+38
-45
lines changed

6 files changed

+38
-45
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/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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct(
6666
$this->observerConfig = $observerConfig;
6767
$this->userResource = $userResource;
6868
$this->authSession = $authSession;
69-
$this->encryptor = $encryptor;
69+
$this->encryptor = $encryptor; // no longer used
7070
$this->messageManager = $messageManager;
7171
}
7272

@@ -81,10 +81,9 @@ public function execute(EventObserver $observer)
8181
/* @var $user \Magento\User\Model\User */
8282
$user = $observer->getEvent()->getObject();
8383
if ($user->getId()) {
84-
$password = $user->getCurrentPassword();
84+
$passwordHash = $user->getPassword();
8585
$passwordLifetime = $this->observerConfig->getAdminPasswordLifetime();
86-
if ($passwordLifetime && $password && !$user->getForceNewPassword()) {
87-
$passwordHash = $this->encryptor->getHash($password, false);
86+
if ($passwordLifetime && $passwordHash && !$user->getForceNewPassword()) {
8887
$this->userResource->trackPassword($user, $passwordHash, $passwordLifetime);
8988
$this->messageManager->getMessages()->deleteMessageByIdentifier('magento_user_password_expired');
9089
$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 & 12 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
);
@@ -108,19 +99,18 @@ public function testTrackAdminPassword()
10899
/** @var \Magento\User\Model\User|\PHPUnit_Framework_MockObject_MockObject $userMock */
109100
$userMock = $this->getMockBuilder('Magento\User\Model\User')
110101
->disableOriginalConstructor()
111-
->setMethods(['getId', 'getCurrentPassword', 'getForceNewPassword'])
102+
->setMethods(['getId', 'getPassword', 'getForceNewPassword'])
112103
->getMock();
113104

114105
$eventObserverMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
115106
$eventMock->expects($this->once())->method('getObject')->willReturn($userMock);
116107
$userMock->expects($this->once())->method('getId')->willReturn($uid);
117-
$userMock->expects($this->once())->method('getCurrentPassword')->willReturn($newPW);
108+
$userMock->expects($this->once())->method('getPassword')->willReturn($newPW);
118109
$this->configInterfaceMock
119110
->expects($this->atLeastOnce())
120111
->method('getValue')
121112
->willReturn(1);
122113
$userMock->expects($this->once())->method('getForceNewPassword')->willReturn(false);
123-
$this->encryptorMock->expects($this->once())->method('getHash')->willReturn(md5($oldPW));
124114

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

0 commit comments

Comments
 (0)