PasswordLength.php in Password Policy 8.3
File
password_policy_length/src/Plugin/PasswordConstraint/PasswordLength.php
View source
<?php
namespace Drupal\password_policy_length\Plugin\PasswordConstraint;
use Drupal\Core\Form\FormStateInterface;
use Drupal\password_policy\PasswordConstraintBase;
use Drupal\password_policy\PasswordPolicyValidation;
use Drupal\user\UserInterface;
class PasswordLength extends PasswordConstraintBase {
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;
}
public function defaultConfiguration() {
return [
'character_length' => 1,
'character_operation' => 'minimum',
];
}
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;
}
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.'));
}
}
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');
}
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'],
]);
}
}