public function PasswordCharacter::validate in Password Policy 8.3
Returns a true/false status if the password meets the constraint.
Parameters
string $password: The password entered by the end user.
\Drupal\user\UserInterface: The user which password is changed.
Return value
PasswordPolicyValidation Whether or not the password meets the constraint in the plugin.
Overrides PasswordConstraintInterface::validate
File
- password_policy_characters/
src/ Plugin/ PasswordConstraint/ PasswordCharacter.php, line 25
Class
- PasswordCharacter
- Enforces a number of a type of character in passwords.
Namespace
Drupal\password_policy_characters\Plugin\PasswordConstraintCode
public function validate($password, UserInterface $user) {
$configuration = $this
->getConfiguration();
$validation = new PasswordPolicyValidation();
$character_distribution = count_chars($password);
$count_upper = 0;
$count_lower = 0;
$count_letter = 0;
$count_special = 0;
$count_numeric = 0;
foreach ($character_distribution as $i => $val) {
if ($val) {
$char = chr($i);
if (is_numeric($char)) {
$count_numeric++;
}
else {
if (ctype_upper($char)) {
$count_upper++;
$count_letter++;
}
else {
if (ctype_lower($char)) {
$count_lower++;
$count_letter++;
}
else {
$count_special++;
}
}
}
}
}
switch ($configuration['character_type']) {
case 'uppercase':
if ($count_upper < $configuration['character_count']) {
$validation
->setErrorMessage($this
->formatPlural($configuration['character_count'], 'Password must contain at least 1 uppercase character.', 'Password must contain at least @count uppercase characters.'));
}
break;
case 'lowercase':
if ($count_lower < $configuration['character_count']) {
$validation
->setErrorMessage($this
->formatPlural($configuration['character_count'], 'Password must contain at least 1 lowercase character.', 'Password must contain at least @count lowercase characters.'));
}
break;
case 'letter':
if ($count_letter < $configuration['character_count']) {
$validation
->setErrorMessage($this
->formatPlural($configuration['character_count'], 'Password must contain at least 1 letter character.', 'Password must contain at least @count letter characters.'));
}
break;
case 'special':
if ($count_special < $configuration['character_count']) {
$validation
->setErrorMessage($this
->formatPlural($configuration['character_count'], 'Password must contain at least 1 special character.', 'Password must contain at least @count special characters.'));
}
break;
case 'numeric':
if ($count_numeric < $configuration['character_count']) {
$validation
->setErrorMessage($this
->formatPlural($configuration['character_count'], 'Password must contain at least 1 numeric character.', 'Password must contain at least @count numeric characters.'));
}
break;
}
return $validation;
}