You are here

class PasswordStrength in Password Strength 8.2

Same name in this branch
  1. 8.2 src/PasswordStrength.php \Drupal\password_strength\PasswordStrength
  2. 8.2 src/Plugin/PasswordConstraint/PasswordStrength.php \Drupal\password_strength\Plugin\PasswordConstraint\PasswordStrength

Enforces a specific character length for passwords.

Plugin annotation


@PasswordConstraint(
  id = "password_strength_constraint",
  title = @Translation("Password Strength"),
  description = @Translation("PasswordStrength is a password strength estimator using pattern matching and minimum entropy calculation. Scores range from 0 to 4, 4 being the strongest password."),
  error_message = @Translation("Your password lacks strength and has too many common patterns."),
)

Hierarchy

Expanded class hierarchy of PasswordStrength

File

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

Namespace

Drupal\password_strength\Plugin\PasswordConstraint
View source
class PasswordStrength extends PasswordConstraintBase {
  public $strength_scores = [
    '0' => 'Very weak (0)',
    '1' => 'Weak (1)',
    '2' => 'Average (2)',
    '3' => 'Strong (3)',
    '4' => 'Very strong (4)',
  ];

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'strength_score' => 3,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['strength_score'] = array(
      '#type' => 'select',
      '#title' => t('Password Strength Minimum Score'),
      '#options' => $this->strength_scores,
      '#default_value' => $this
        ->getConfiguration()['strength_score'],
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['strength_score'] = $form_state
      ->getValue('strength_score');
  }

  /**
   * {@inheritdoc}
   */
  public function getSummary() {
    return $this
      ->t('Password Strength minimum score of @score', array(
      '@score' => $this->configuration['strength_score'],
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PasswordConstraintBase::calculateDependencies public function
PasswordConstraintBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
PasswordConstraintBase::getDescription public function Returns a translated description for the constraint description. Overrides PasswordConstraintInterface::getDescription
PasswordConstraintBase::getErrorMessage public function Returns a translated error message for the constraint. Overrides PasswordConstraintInterface::getErrorMessage
PasswordConstraintBase::getTitle public function Returns a translated string for the constraint title. Overrides PasswordConstraintInterface::getTitle
PasswordConstraintBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
PasswordConstraintBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 6
PasswordConstraintBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
PasswordStrength::$strength_scores public property
PasswordStrength::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
PasswordStrength::defaultConfiguration public function Gets default configuration for this plugin. Overrides PasswordConstraintBase::defaultConfiguration
PasswordStrength::getSummary public function Returns a human-readable summary of the constraint. Overrides PasswordConstraintInterface::getSummary
PasswordStrength::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
PasswordStrength::validate function Returns a true/false status if the password meets the constraint. Overrides PasswordConstraintInterface::validate
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.