Merge pull request #2076 from halkyon/passwordvalidator_translate

Allow PasswordValidator to be translated
This commit is contained in:
Will Rossiter 2013-06-07 16:19:40 -07:00
commit 31cfcdb08e

View File

@ -60,12 +60,18 @@ class PasswordValidator extends Object {
*/ */
public function validate($password, $member) { public function validate($password, $member) {
$valid = new ValidationResult(); $valid = new ValidationResult();
if($this->minLength) { if($this->minLength) {
if(strlen($password) < $this->minLength) { if(strlen($password) < $this->minLength) {
$valid->error( $valid->error(
sprintf("Password is too short, it must be %s or more characters long.", $this->minLength), sprintf(
"TOO_SHORT" _t(
'PasswordValidator.TOOSHORT',
'Password is too short, it must be %s or more characters long'
),
$this->minLength
),
'TOO_SHORT'
); );
} }
} }
@ -74,32 +80,47 @@ class PasswordValidator extends Object {
$score = 0; $score = 0;
$missedTests = array(); $missedTests = array();
foreach($this->testNames as $name) { foreach($this->testNames as $name) {
if(preg_match(self::config()->character_strength_tests[$name], $password)) $score++; if(preg_match(self::config()->character_strength_tests[$name], $password)) {
else $missedTests[] = $name; $score++;
} else {
$missedTests[] = _t(
'PasswordValidator.STRENGTHTEST' . strtoupper($name),
$name,
'The user needs to add this to their password for more complexity'
);
}
} }
if($score < $this->minScore) { if($score < $this->minScore) {
$valid->error( $valid->error(
"You need to increase the strength of your passwords by adding some of the following characters: " sprintf(
. implode(", ", $missedTests), _t(
"LOW_CHARACTER_STRENGTH" 'PasswordValidator.LOWCHARSTRENGTH',
'Please increase password strength by adding some of the following characters: %s'
),
implode(', ', $missedTests)
),
'LOW_CHARACTER_STRENGTH'
); );
} }
} }
if($this->historicalPasswordCount) { if($this->historicalPasswordCount) {
$previousPasswords = DataObject::get( $previousPasswords = DataObject::get(
"MemberPassword", "MemberPassword",
"\"MemberID\" = $member->ID", "\"MemberID\" = $member->ID",
"\"Created\" DESC, \"ID\" Desc", "\"Created\" DESC, \"ID\" DESC",
"", "",
$this->historicalPasswordCount $this->historicalPasswordCount
); );
if($previousPasswords) foreach($previousPasswords as $previousPasswords) { if($previousPasswords) foreach($previousPasswords as $previousPasswords) {
if($previousPasswords->checkPassword($password)) { if($previousPasswords->checkPassword($password)) {
$valid->error( $valid->error(
"You've already used that password in the past, please choose a new password", _t(
"PREVIOUS_PASSWORD" 'PasswordValidator.PREVPASSWORD',
'You\'ve already used that password in the past, please choose a new password'
),
'PREVIOUS_PASSWORD'
); );
break; break;
} }