Skip to content
Snippets Groups Projects
Commit 39d8f0d5 authored by Andrey Vitushkin's avatar Andrey Vitushkin
Browse files

Issue #3446907: The Constraint Validator for the "ipaddress_mysql" field type was created

parent 5ec25ac6
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,6 @@ final class MySqlFormatConstraint extends Constraint { ...@@ -21,6 +21,6 @@ final class MySqlFormatConstraint extends Constraint {
* *
* @var string * @var string
*/ */
public string $message = 'The value is in the wrong format for the IP address.'; public string $message = 'The IP address value must follow <a href="https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing" target="_blank">CIDR notation</a>.';
} }
...@@ -4,53 +4,67 @@ declare(strict_types=1); ...@@ -4,53 +4,67 @@ declare(strict_types=1);
namespace Drupal\field_ipaddress_pgsql\Plugin\Validation\Constraint; namespace Drupal\field_ipaddress_pgsql\Plugin\Validation\Constraint;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\ConstraintValidator;
/** /**
* Validates the MySQL format validation constraint. * Validates the MySQL format validation constraint.
*/ */
final class MySqlFormatConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface { final class MySqlFormatConstraintValidator extends ConstraintValidator {
/** /**
* Constructs the object. * Validate the CIDR notation format (ipv4, ipv6).
*
* Was used the PHP function for validating CIDR notation format (ipv4, ipv6).
*
* @see https://gist.github.com/pavinjosdev/cb1d636ea9dc2bd201d54107d10650c5
*/ */
public function __construct( public function validate(mixed $value, Constraint $constraint): void {
private readonly Connection $connection, $value = trim($value->value);
) {}
/** if (!$this->validateCidr($value)) {
* {@inheritdoc} $this->context->addViolation($constraint->message);
*/ }
public static function create(ContainerInterface $container): self {
return new self(
$container->get('database'),
);
} }
/** /**
* {@inheritdoc} * Validates the format of a CIDR notation string.
*
* @param string $cidr
* The value that needs to be validated.
*
* @return bool
* TRUE if the value is in valid CIDR format, FALSE if not.
*/ */
public function validate(mixed $value, Constraint $constraint): void { private function validateCidr($cidr) {
$value = trim($value->value); $parts = explode('/', $cidr);
/* if (count($parts) != 2) {
if (!filter_var($value, FILTER_VALIDATE_IP)) { return FALSE;
$this->context->addViolation($constraint->message);
} }
*/
/* $ip = $parts[0];
try { $netmask = $parts[1];
$this->connection->query('SELECT inet(:ipAddressString)', [':ipAddressString' => $value]);
if (!preg_match("/^\d+$/", $netmask)) {
return FALSE;
}
$netmask = intval($parts[1]);
if ($netmask < 0) {
return FALSE;
} }
catch (\Exception $e) {
$this->context->addViolation($constraint->message); if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return $netmask <= 32;
}
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return $netmask <= 128;
} }
*/
return FALSE;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment