diff --git a/password_policy_character_types/src/Plugin/PasswordConstraint/CharacterTypes.php b/password_policy_character_types/src/Plugin/PasswordConstraint/CharacterTypes.php index fa03de3..f6ad690 100644 --- a/password_policy_character_types/src/Plugin/PasswordConstraint/CharacterTypes.php +++ b/password_policy_character_types/src/Plugin/PasswordConstraint/CharacterTypes.php @@ -5,7 +5,6 @@ * Contains Drupal\password_policy_character_types\Constraints\CharacterTypes. */ - namespace Drupal\password_policy_character_types\Plugin\PasswordConstraint; use Drupal\Core\Form\FormStateInterface; @@ -13,7 +12,7 @@ use Drupal\password_policy\PasswordConstraintBase; use Drupal\password_policy\PasswordPolicyValidation; /** - * Enforces a specific character length for passwords. <-- FIXME + * Enforces a minimum number of character types for passwords. * * @PasswordConstraint( * id = "character_types", @@ -27,10 +26,10 @@ class CharacterTypes extends PasswordConstraintBase { /** * {@inheritdoc} */ - function validate($password, $user_context) { + public function validate($password, $user_context) { $validation = new PasswordPolicyValidation(); $types = $this->getConfiguration()['character_types']; - if ($types > 4 || $types < 1) { + if ($types < 2 || $types > 4) { $validation->setErrorMessage($this->t('Invalid plugin configuration.')); } $character_sets = count(array_filter([ @@ -40,7 +39,7 @@ class CharacterTypes extends PasswordConstraintBase { preg_match('[^a-zA-Z0-9]', $password), ])); if ($character_sets < $types) { - $validation->setErrorMessage($this->t('The password is required to contain at least @types types of characters from the character sets: lowercase alphabetic characters (a–z), uppercase alphabetic characters (A–Z), numeric characters (0–9), special characters.', ['@types' => $types])); + $validation->setErrorMessage($this->t('The password is required to contain at least @types types of characters from the character types: lowercase alphabetic characters (a–z), uppercase alphabetic characters (A–Z), numeric characters (0–9), special characters.', ['@types' => $types])); } return $validation; } @@ -61,7 +60,7 @@ class CharacterTypes extends PasswordConstraintBase { $form['character_types'] = [ '#type' => 'number', '#title' => $this->t('Number of character types'), - '#description' => $this->t('Select the number of character sets which must be found in a password. The supported character sets are given as: lowercase alphabetic characters (a–z), uppercase alphabetic characters (A–Z), numeric characters (0–9), special characters.'), + '#description' => $this->t('Select the minimum number of character types which must be found in a password. The four supported character types are given as: lowercase alphabetic characters (a–z), uppercase alphabetic characters (A–Z), numeric characters (0–9), special characters.'), '#default_value' => $this->getConfiguration()['character_types'], ]; return $form; @@ -72,8 +71,8 @@ class CharacterTypes extends PasswordConstraintBase { */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { $types = $form_state->getValue('character_types'); - if (!is_numeric($types) || $types < 1 || $types > 4) { - $form_state->setErrorByName('character_types', $this->t('The number of character types must be between 1 and 4.')); + if (!is_numeric($types) || $types < 2 || $types > 4) { + $form_state->setErrorByName('character_types', $this->t('The number of character types must be between 2 and 4.')); } } diff --git a/password_policy_character_types/tests/src/Unit/CharacterTypesTest.php b/password_policy_character_types/tests/src/Unit/CharacterTypesTest.php index 79dc003..5ebf661 100644 --- a/password_policy_character_types/tests/src/Unit/CharacterTypesTest.php +++ b/password_policy_character_types/tests/src/Unit/CharacterTypesTest.php @@ -1,5 +1,10 @@