Skip to content

Commit c1d9d5c

Browse files
committed
added helper methods, suggested by @Hanmac
1 parent ad90358 commit c1d9d5c

File tree

3 files changed

+60
-47
lines changed

3 files changed

+60
-47
lines changed

app/code/core/Mage/Admin/Model/User.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @package Mage_Admin
88
*/
99

10+
use Symfony\Component\Validator\ConstraintViolationListInterface;
11+
1012
/**
1113
* Admin user model
1214
*
@@ -631,14 +633,8 @@ public function validate()
631633
}
632634

633635
if (isset($password)) {
634-
$minAdminPasswordLength = $this->getMinAdminPasswordLength();
635636
$violations = new ArrayObject();
636-
$violations->append($validator->validatePassword(
637-
value: $password,
638-
minLength: $minAdminPasswordLength,
639-
message: Mage::helper('adminhtml')->__('Password must include both numeric and alphabetic characters.'),
640-
minMessage: Mage::helper('adminhtml')->__('Password must be at least of %d characters.', $minAdminPasswordLength),
641-
));
637+
$violations->append($this->getPasswordValidator(value: $password));
642638

643639
if ($this->hasPasswordConfirmation()) {
644640
$violations->append($validator->validateIdentical(
@@ -671,6 +667,19 @@ public function validate()
671667
return (array) $errors;
672668
}
673669

670+
public function getPasswordValidator(mixed $value): ConstraintViolationListInterface
671+
{
672+
$min = $this->getMinAdminPasswordLength();
673+
$validator = $this->getValidationHelper();
674+
675+
return $validator->validatePassword(
676+
value: $value,
677+
min: $min,
678+
minMessage: Mage::helper('adminhtml')->__('Password must be at least of %d characters.', $min),
679+
regexMessage: Mage::helper('adminhtml')->__('Password must include both numeric and alphabetic characters.'),
680+
);
681+
}
682+
674683
/**
675684
* Validate password against current user password
676685
* Returns TRUE or array of errors.
@@ -681,8 +690,7 @@ public function validate()
681690
*/
682691
public function validateCurrentPassword($password)
683692
{
684-
/** @var Mage_Core_Helper_Validate $validator */
685-
$validator = Mage::helper('core/validate');
693+
$validator = $this->getValidationHelper();
686694
$result = [];
687695

688696
if ($validator->validateNotEmpty($password)->count() > 0) {

app/code/core/Mage/Core/Helper/Validate.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ public function validateNotEmpty(
471471
mixed $payload = null
472472
): ConstraintViolationListInterface {
473473
$validator = self::getValidator();
474+
474475
return $validator->validate($value, [
475476
new Constraints\NotBlank(
476477
options: $options,
@@ -483,17 +484,33 @@ public function validateNotEmpty(
483484
]);
484485
}
485486

486-
public function validatePassword(mixed $value, int $minLength, string $message, string $minMessage): ConstraintViolationListInterface
487-
{
487+
/**
488+
* Validates that a password meets given constraints.
489+
*/
490+
public function validatePassword(
491+
mixed $value,
492+
?int $min = null,
493+
?int $max = null,
494+
?string $emptyMessage = null,
495+
?string $minMessage = null,
496+
?string $maxMessage = null,
497+
?string $regexMessage = null,
498+
): ConstraintViolationListInterface {
488499
$validator = self::getValidator();
500+
489501
return $validator->validate($value, [
502+
new Constraints\NotBlank(
503+
message: $emptyMessage,
504+
),
490505
new Constraints\Length(
491-
min: $minLength,
506+
min: $min,
507+
max: $max,
492508
minMessage: $minMessage,
509+
maxMessage: $maxMessage,
493510
),
494511
new Constraints\Regex(
495512
pattern: '/^(?=.*[a-z])(?=.*[0-9]).+$/iu',
496-
message: $message,
513+
message: $regexMessage,
497514
),
498515
]);
499516
}

app/code/core/Mage/Customer/Model/Customer.php

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @package Mage_Customer
88
*/
99

10+
use Symfony\Component\Validator\ConstraintViolationListInterface;
11+
1012
/**
1113
* Customer model
1214
*
@@ -1096,26 +1098,11 @@ public function validate()
10961098
message: Mage::helper('customer')->__('Invalid email address "%s".', $email),
10971099
));
10981100

1099-
$password = $this->getPassword();
1100-
$minPasswordLength = $this->getMinPasswordLength();
1101+
$violations->append($this->getPasswordValidator(value: $this->getPassword()));
11011102

1102-
$violations->append($validator->validateNotEmpty(
1103-
value: $password,
1104-
message: Mage::helper('customer')->__('The password cannot be empty.'),
1105-
));
1106-
1107-
$violations->append($validator->validateLength(
1108-
value: $password,
1109-
min: $minPasswordLength,
1110-
max: self::MAXIMUM_PASSWORD_LENGTH,
1111-
minMessage: Mage::helper('customer')->__('The minimum password length is %s', $minPasswordLength),
1112-
maxMessage: Mage::helper('customer')->__('Please enter a password with at most %s characters.', self::MAXIMUM_PASSWORD_LENGTH),
1113-
));
1114-
1115-
$confirmation = $this->getPasswordConfirmation();
11161103
$violations->append($validator->validateIdentical(
1117-
value: $confirmation,
1118-
compare: $password,
1104+
value: $this->getPasswordConfirmation(),
1105+
compare: $this->getPassword(),
11191106
message: Mage::helper('customer')->__('Please make sure your passwords match.'),
11201107
));
11211108

@@ -1159,25 +1146,11 @@ public function validateResetPassword()
11591146
$validator = $this->getValidationHelper();
11601147
$violations = new ArrayObject();
11611148

1162-
$password = $this->getPassword();
1163-
$minPasswordLength = $this->getMinPasswordLength();
1164-
1165-
$violations->append($validator->validateNotEmpty(
1166-
value: $password,
1167-
message: Mage::helper('customer')->__('The password cannot be empty.'),
1168-
));
1169-
1170-
$violations->append($validator->validateLength(
1171-
value: $password,
1172-
min: $minPasswordLength,
1173-
max: self::MAXIMUM_PASSWORD_LENGTH,
1174-
minMessage: Mage::helper('customer')->__('The minimum password length is %s', $minPasswordLength),
1175-
maxMessage: Mage::helper('customer')->__('Please enter a password with at most %s characters.', self::MAXIMUM_PASSWORD_LENGTH),
1176-
));
1149+
$violations->append($this->getPasswordValidator(value: $this->getPassword()));
11771150

11781151
$violations->append($validator->validateIdentical(
11791152
value: $this->getPasswordConfirmation(),
1180-
compare: $password,
1153+
compare: $this->getPassword(),
11811154
message: Mage::helper('customer')->__('Please make sure your passwords match.'),
11821155
));
11831156

@@ -1189,6 +1162,21 @@ public function validateResetPassword()
11891162
return (array) $errors;
11901163
}
11911164

1165+
public function getPasswordValidator(mixed $value): ConstraintViolationListInterface
1166+
{
1167+
$min = $this->getMinPasswordLength();
1168+
$validator = $this->getValidationHelper();
1169+
1170+
return $validator->validatePassword(
1171+
value: $value,
1172+
min: $min,
1173+
max: self::MAXIMUM_PASSWORD_LENGTH,
1174+
emptyMessage: Mage::helper('customer')->__('The password cannot be empty.'),
1175+
minMessage: Mage::helper('customer')->__('The minimum password length is %s', $min),
1176+
maxMessage: Mage::helper('customer')->__('Please enter a password with at most %s characters.', self::MAXIMUM_PASSWORD_LENGTH),
1177+
);
1178+
}
1179+
11921180
/**
11931181
* Import customer data from text array
11941182
*

0 commit comments

Comments
 (0)