class PasswordLength in Password Policy 8.3
Enforces a specific character length for passwords.
Plugin annotation
@PasswordConstraint(
id = "password_length",
title = @Translation("Password character length"),
description = @Translation("Verifying that a password has a minimum character length"),
errorMessage = @Translation("The length of your password is too short.")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\password_policy\PasswordConstraintBase implements PasswordConstraintInterface uses StringTranslationTrait
- class \Drupal\password_policy_length\Plugin\PasswordConstraint\PasswordLength
- class \Drupal\password_policy\PasswordConstraintBase implements PasswordConstraintInterface uses StringTranslationTrait
Expanded class hierarchy of PasswordLength
File
- password_policy_length/
src/ Plugin/ PasswordConstraint/ PasswordLength.php, line 20
Namespace
Drupal\password_policy_length\Plugin\PasswordConstraintView source
class PasswordLength extends PasswordConstraintBase {
/**
* {@inheritdoc}
*/
public function validate($password, UserInterface $user) {
$configuration = $this
->getConfiguration();
$validation = new PasswordPolicyValidation();
switch ($configuration['character_operation']) {
case 'minimum':
if (strlen($password) < $configuration['character_length']) {
$validation
->setErrorMessage($this
->formatPlural($configuration['character_length'], 'Password length must be at least 1 character.', 'Password length must be at least @count characters.'));
}
break;
case 'maximum':
if (strlen($password) > $configuration['character_length']) {
$validation
->setErrorMessage($this
->formatPlural($configuration['character_length'], 'Password length must not exceed 1 character.', 'Password length must not exceed @count characters.'));
}
break;
}
return $validation;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'character_length' => 1,
'character_operation' => 'minimum',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['character_length'] = [
'#type' => 'textfield',
'#title' => $this
->t('Number of characters'),
'#default_value' => $this
->getConfiguration()['character_length'],
];
$form['character_operation'] = [
'#type' => 'select',
'#title' => $this
->t('Operation'),
'#options' => [
'minimum' => $this
->t('Minimum length'),
'maximum' => $this
->t('Maximum length'),
],
'#default_value' => $this
->getConfiguration()['character_operation'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!is_numeric($form_state
->getValue('character_length')) or $form_state
->getValue('character_length') <= 0) {
$form_state
->setErrorByName('character_length', $this
->t('The character length must be a positive number.'));
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['character_length'] = $form_state
->getValue('character_length');
$this->configuration['character_operation'] = $form_state
->getValue('character_operation');
}
/**
* {@inheritdoc}
*/
public function getSummary() {
switch ($this->configuration['character_operation']) {
case 'minimum':
$operation = $this
->t('at least');
break;
case 'maximum':
$operation = $this
->t('at most');
break;
}
return $this
->formatPlural($this->configuration['character_length'], 'Password character length of @operation 1 character', 'Password character length of @operation @characters characters', [
'@operation' => $operation,
'@characters' => $this->configuration['character_length'],
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PasswordConstraintBase:: |
public | function | ||
PasswordConstraintBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PasswordConstraintBase:: |
public | function |
Returns a translated description for the constraint description. Overrides PasswordConstraintInterface:: |
|
PasswordConstraintBase:: |
public | function |
Returns a translated error message for the constraint. Overrides PasswordConstraintInterface:: |
|
PasswordConstraintBase:: |
public | function |
Returns a translated string for the constraint title. Overrides PasswordConstraintInterface:: |
|
PasswordConstraintBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
PasswordConstraintBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
PasswordLength:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
PasswordLength:: |
public | function |
Gets default configuration for this plugin. Overrides PasswordConstraintBase:: |
|
PasswordLength:: |
public | function |
Returns a human-readable summary of the constraint. Overrides PasswordConstraintInterface:: |
|
PasswordLength:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
PasswordLength:: |
public | function |
Returns a true/false status if the password meets the constraint. Overrides PasswordConstraintInterface:: |
|
PasswordLength:: |
public | function |
Form validation handler. Overrides PasswordConstraintBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |