You are here

function PasswordStrength::validate in Password Strength 8.2

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

src/Plugin/PasswordConstraint/PasswordStrength.php, line 35

Class

PasswordStrength
Enforces a specific character length for passwords.

Namespace

Drupal\password_strength\Plugin\PasswordConstraint

Code

function validate($password, UserInterface $user) {
  $userData = array_merge($user
    ->getAccountName() ? [
    $user
      ->getAccountName(),
  ] : [], $user
    ->getEmail() ? [
    $user
      ->getEmail(),
  ] : []);
  $configuration = $this
    ->getConfiguration();
  $validation = new PasswordPolicyValidation();
  $password_strength = new \Drupal\password_strength\PasswordStrength();
  $strength = $password_strength
    ->passwordStrength($password, $userData);
  if ($strength['score'] < $configuration['strength_score']) {
    $validation
      ->setErrorMessage($this
      ->t('The password has a score of @password-score but the policy requires a score of at least @policy-score', array(
      '@password-score' => $strength['score'],
      '@policy-score' => $configuration['strength_score'],
    )));
  }
  return $validation;
}