mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
eb60b67732
http://svn.silverstripe.com/open/modules/sapphire/branches/govtsecurity ........ r52121 | sminnee | 2008-04-03 22:04:33 +1300 (Thu, 03 Apr 2008) | 4 lines Added DataObject::validate() for specifying DataObject-level validators. Added DataObject::onAfterWrite(), a complement of DataObject::onBeforeWrite() Added password strength testing to security system Added password expiry to security system ........ git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@53465 467b73ca-7a2a-4603-9d3b-597d59a354a9
85 lines
2.5 KiB
PHP
85 lines
2.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* This class represents a validator for member passwords.
|
|
*
|
|
* <code>
|
|
* $pwdVal = new PasswordValidator();
|
|
* $pwdValidator->minLength(7);
|
|
* $pwdValidator->checkHistoricalPasswords(6);
|
|
* $pwdValidator->characterStrength('lowercase','uppercase','digits','punctuation');
|
|
*
|
|
* Member::set_password_validator($pwdValidator);
|
|
* </code>
|
|
*/
|
|
class PasswordValidator extends Object {
|
|
static $character_strength_tests = array(
|
|
'lowercase' => '/[a-z]/',
|
|
'uppercase' => '/[A-Z]/',
|
|
'digits' => '/[0-9]/',
|
|
'punctuation' => '/[^A-Za-z0-9]/',
|
|
);
|
|
|
|
protected $minLength, $minScore, $testNames, $historicalPasswordCount;
|
|
|
|
/**
|
|
* Minimum password length
|
|
*/
|
|
function minLength($minLength) {
|
|
$this->minLength = $minLength;
|
|
}
|
|
|
|
/**
|
|
* Check the character strength of the password.
|
|
*
|
|
* Eg: $this->characterStrength(3, array("lowercase", "uppercase", "digits", "punctuation"))
|
|
*
|
|
* @param $minScore The minimum number of character tests that must pass
|
|
* @param $testNames The names of the tests to perform
|
|
*/
|
|
function characterStrength($minScore, $testNames) {
|
|
$this->minScore = $minScore;
|
|
$this->testNames = $testNames;
|
|
}
|
|
|
|
/**
|
|
* Check a number of previous passwords that the user has used, and don't let them change to that.
|
|
*/
|
|
function checkHistoricalPasswords($count) {
|
|
$this->historicalPasswordCount = $count;
|
|
}
|
|
|
|
function validate($password, $member) {
|
|
$valid = new ValidationResult();
|
|
|
|
if($this->minLength) {
|
|
if(strlen($password) < $this->minLength) $valid->error("Password is too short, it must be 7 or more characters long.", "TOO_SHORT");
|
|
}
|
|
|
|
if($this->minScore) {
|
|
$score = 0;
|
|
$missedTests = array();
|
|
foreach($this->testNames as $name) {
|
|
if(preg_match(self::$character_strength_tests[$name], $password)) $score++;
|
|
else $missedTests[] = $name;
|
|
}
|
|
|
|
if($score < $this->minScore) {
|
|
$valid->error("You need to increase the strength of your passwords by adding some of the following characters: " . implode(", ", $missedTests), "LOW_CHARACTER_STRENGTH");
|
|
}
|
|
}
|
|
|
|
if($this->historicalPasswordCount) {
|
|
$previousPasswords = DataObject::get("MemberPassword", "MemberID = $member->ID", "Created DESC, ID Desc", "", $this->historicalPasswordCount);
|
|
if($previousPasswords) foreach($previousPasswords as $previousPasswords) {
|
|
if($previousPasswords->checkPassword($password)) {
|
|
$valid->error("You've already used that password in the past, please choose a new password", "PREVIOUS_PASSWORD");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $valid;
|
|
}
|
|
|
|
} |