Skip to content

Commit 1d8157e

Browse files
author
Oleksandr Dubovyk
committed
MAGETWO-62966: Storefront Password Strength validator ignores case when Email = Password
- Refactor after CR
1 parent 6ea2f1d commit 1d8157e

File tree

7 files changed

+35
-11
lines changed

7 files changed

+35
-11
lines changed

Diff for: app/code/Magento/Customer/Model/AccountManagement.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,11 @@ public function createAccount(CustomerInterface $customer, $password = null, $re
666666
if ($password !== null) {
667667
$this->checkPasswordStrength($password);
668668
$customerEmail = $customer->getEmail();
669-
$this->credentialsValidator->checkPasswordDifferentFromEmail($customerEmail, $password);
669+
try {
670+
$this->credentialsValidator->checkPasswordDifferentFromEmail($customerEmail, $password);
671+
} catch (InputException $e) {
672+
throw new LocalizedException(__('Password cannot be the same as email address.'));
673+
}
670674
$hash = $this->createPasswordHash($password);
671675
} else {
672676
$hash = null;
@@ -838,6 +842,8 @@ private function changePasswordForCustomer($customer, $currentPassword, $newPass
838842
} catch (InvalidEmailOrPasswordException $e) {
839843
throw new InvalidEmailOrPasswordException(__('The password doesn\'t match this account.'));
840844
}
845+
$customerEmail = $customer->getEmail();
846+
$this->credentialsValidator->checkPasswordDifferentFromEmail($customerEmail, $newPassword);
841847
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
842848
$customerSecure->setRpToken(null);
843849
$customerSecure->setRpTokenCreatedAt(null);

Diff for: app/code/Magento/Customer/Model/Customer/CredentialsValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class CredentialsValidator
1313
{
1414
/**
15-
* Check that password is different from login.
15+
* Check that password is different from email.
1616
*
1717
* @param string $email
1818
* @param string $password

Diff for: app/code/Magento/Customer/view/frontend/web/change-email-password.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ define([
140140
'data-validate',
141141
'{required:true, ' +
142142
'\'validate-customer-password\':true, ' +
143-
'\'password-not-equal-to-email\':\'' + $(this.options.emailSelector).val() + '\'}'
143+
'\'password-not-equal-to-user-name\':\'' + $(this.options.emailSelector).val() + '\'}'
144144
).prop('disabled', false);
145145
}
146146
});

Diff for: dev/tests/js/jasmine/tests/lib/mage/validation.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,24 @@ define([
163163
)).toBeFalsy();
164164
});
165165
});
166+
167+
describe('Validation of the password against the user name', function () {
168+
it("rejects data, if password is the same as user name", function() {
169+
var password = $('<input id="password" type="password" value="EmailPasswordTheSame" />');
170+
var email = $('<input id="email" type="email" value="EmailPasswordTheSame" />');
171+
172+
expect($.validator.methods['password-not-equal-to-user-name'].call(
173+
$.validator.prototype, password.val(), null, email.val()
174+
)).toEqual(false);
175+
});
176+
177+
it("approves data, if password is different from user name", function() {
178+
var password = $('<input id="password" type="password" value="SomePassword" />');
179+
var email = $('<input id="email" type="email" value="SomeEmail" />');
180+
181+
expect($.validator.methods['password-not-equal-to-user-name'].call(
182+
$.validator.prototype, password.val(), null, email.val()
183+
)).toEqual(true);
184+
});
185+
});
166186
});

Diff for: lib/web/mage/validation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@
15561556
},
15571557
''
15581558
],
1559-
'password-not-equal-to-email': [
1559+
'password-not-equal-to-user-name': [
15601560
function (value, element, params) {
15611561
if (typeof params === 'string') {
15621562
return value.toLowerCase() !== params.toLowerCase();

Diff for: setup/pub/magento/setup/create-admin-account.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,10 @@ angular.module('create-admin-account', ['ngStorage'])
107107
}
108108
};
109109
})
110-
.directive('checkEmailPassword', function() {
110+
.directive('checkUserNamePassword', function() {
111111
return{
112112
require: "ngModel",
113113
link: function(scope, elm, attrs, ctrl){
114-
115114
var validator = function(value){
116115
var password = value,
117116
userName = scope.account.adminUsername.$viewValue;
@@ -120,11 +119,10 @@ angular.module('create-admin-account', ['ngStorage'])
120119
password = password.toLowerCase();
121120
}
122121
if (userName) {
123-
userName = scope.account.adminUsername.$viewValue.toLowerCase();
122+
userName = userName.toLowerCase();
124123
}
125124

126-
ctrl.$setValidity('checkEmailPasswordDifferent', password !== userName);
127-
125+
ctrl.$setValidity('checkUserNamePasswordDifferent', password !== userName);
128126
return value;
129127
};
130128

Diff for: setup/view/magento/setup/create-admin-account.phtml

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ $passwordWizard = sprintf(
162162
ng-class="{'invalid': account.adminPassword.$invalid && account.submitted}"
163163
required
164164
check-Password
165-
check-Email-Password
165+
check-User-Name-Password
166166
>
167167
<div class="error-container">
168168
<span ng-show="account.adminPassword.$error.checkPassword">
169169
Please enter a mix of at least 7 alpha-numeric characters.
170170
</span>
171-
<span ng-show="account.adminPassword.$error.checkEmailPasswordDifferent">
171+
<span ng-show="account.adminPassword.$error.checkUserNamePasswordDifferent">
172172
Password cannot be the same as the user name.
173173
</span>
174174
<span ng-show="account.adminPassword.$error.required">

0 commit comments

Comments
 (0)