You are here

public function ConsecutiveCharacters::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_consecutive/src/Plugin/PasswordConstraint/ConsecutiveCharacters.php, line 25

Class

ConsecutiveCharacters
Enforces a maximum number of consecutive identical characters.

Namespace

Drupal\password_policy_consecutive\Plugin\PasswordConstraint

Code

public function validate($password, UserInterface $user) {
  $validation = new PasswordPolicyValidation();
  $max = $this
    ->getConfiguration()['max_consecutive_characters'];
  if ($max < 2) {
    $validation
      ->setErrorMessage($this
      ->t('Invalid plugin configuration.'));
  }
  $pattern = '/(.)\\1{' . ($max - 1) . '}/';
  if (preg_match($pattern, $password)) {
    $validation
      ->setErrorMessage($this
      ->t('Password must have fewer than @max consecutive identical characters. This is case sensitive.', [
      '@max' => $max,
    ]));
  }
  return $validation;
}