diff --git a/src/Plugin/Validation/Constraint/UsernameFormatConstraint.php b/src/Plugin/Validation/Constraint/UsernameFormatConstraint.php index a230784..8602e9a 100644 --- a/src/Plugin/Validation/Constraint/UsernameFormatConstraint.php +++ b/src/Plugin/Validation/Constraint/UsernameFormatConstraint.php @@ -14,39 +14,10 @@ use Symfony\Component\Validator\Constraint; */ class UsernameFormatConstraint extends Constraint { - /** - * Message when the username begins with a space. - * - * @var string - */ - public $beginSpace = 'The username cannot begin with a space.'; - - /** - * Message when the username ends with a space. - * - * @var string - */ - public $endSpace = 'The username cannot end with a space.'; - - /** - * Message when the username contains multiple spaces in a row. - * - * @var string - */ - public $multipleSpace = 'The username cannot contain multiple spaces in a row.'; - - /** - * Message when the username contains an invalid character. - * - * @var string - */ - public $invalidCharacter = "The username contains punctuation, only periods, hyphens, apostrophes, and underscores are allowed."; - - /** - * Message when the username contains an illegal character. - * - * @var string - */ - public $illegalCharacter = "The username contains an illegal character."; + public $spaceBeginMessage = 'The username cannot begin with a space.'; + public $spaceEndMessage = 'The username cannot end with a space.'; + public $multipleSpacesMessage = 'The username cannot contain multiple spaces in a row.'; + public $illegalMessage = 'The username contains an illegal character.'; + public $tooLongMessage = 'The username %name is too long: it must be %max characters or less.'; } diff --git a/src/Plugin/Validation/Constraint/UsernameFormatConstraintValidator.php b/src/Plugin/Validation/Constraint/UsernameFormatConstraintValidator.php index b235578..9708dfd 100644 --- a/src/Plugin/Validation/Constraint/UsernameFormatConstraintValidator.php +++ b/src/Plugin/Validation/Constraint/UsernameFormatConstraintValidator.php @@ -2,6 +2,7 @@ namespace Drupal\do_username\Plugin\Validation\Constraint; +use Drupal\user\UserInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; @@ -13,21 +14,43 @@ class UsernameFormatConstraintValidator extends ConstraintValidator { /** * {@inheritdoc} */ - public function validate($item, Constraint $constraint) { - if (substr($item->value, 0, 1) == ' ') { - $this->context->addViolation($constraint->beginSpace, ['%value' => $item->value]); + public function validate($items, Constraint $constraint) { + $name = $items->first()->value; + if (substr($name, 0, 1) == ' ') { + $this->context->addViolation($constraint->spaceBeginMessage); } - if (substr($item->value, -1) == ' ') { - $this->context->addViolation($constraint->endSpace, ['%value' => $item->value]); + if (substr($name, -1) == ' ') { + $this->context->addViolation($constraint->spaceEndMessage); } - if (strpos($item->value, ' ') !== FALSE) { - $this->context->addViolation($constraint->multipleSpace, ['%value' => $item->value]); + if (strpos($name, ' ') !== FALSE) { + $this->context->addViolation($constraint->multipleSpacesMessage); } - if (preg_match('/[^\\x{80}-\\x{F7} a-z0-9@+_.\'-]/i', $item->value)) { - $this->context->addViolation($constraint->invalidCharacter, ['%value' => $item->value]); + if (preg_match('/[^\x{80}-\x{F7} a-z0-9@+_.\'-]/i', $name) + || preg_match( + // Non-printable ISO-8859-1 + NBSP + '/[\x{80}-\x{A0}' . + // Soft-hyphen + '\x{AD}' . + // Various space characters + '\x{2000}-\x{200F}' . + // Bidirectional text overrides + '\x{2028}-\x{202F}' . + // Various text hinting characters + '\x{205F}-\x{206F}' . + // Byte order mark + '\x{FEFF}' . + // Full-width latin + '\x{FF01}-\x{FF60}' . + // Replacement characters + '\x{FFF9}-\x{FFFD}' . + // NULL byte and control characters + '\x{0}-\x{1F}]/u', + $name) + ) { + $this->context->addViolation($constraint->illegalMessage); } - if (preg_match('/[\\x{80}-\\x{A0}' . '\\x{AD}' . '\\x{2000}-\\x{200F}' . '\\x{2028}-\\x{202F}' . '\\x{205F}-\\x{206F}' . '\\x{FEFF}' . '\\x{FF01}-\\x{FF60}' . '\\x{FFF9}-\\x{FFFD}' . '\\x{0}-\\x{1F}]/u', $item->value)) { - $this->context->addViolation($constraint->illegalCharacter, ['%value' => $item->value]); + if (mb_strlen($name) > UserInterface::USERNAME_MAX_LENGTH) { + $this->context->addViolation($constraint->tooLongMessage, ['%name' => $name, '%max' => UserInterface::USERNAME_MAX_LENGTH]); } }