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

Issue #3451886: Add a option to restrict the IP address to IPv4 or IPv6 on the field creation form

parent f681d5df
No related branches found
No related tags found
No related merge requests found
......@@ -49,6 +49,6 @@ final class MySqlFormatConstraint extends Constraint {
*
* @var string
*/
public string $messageCidr = 'The IP address value must follow <a href="https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing" target="_blank">CIDR notation</a>.';
public string $messageCidr = 'The IP address value must follow <a href="https://www.postgresql.org/docs/16/datatype-net-types.html#DATATYPE-CIDR" target="_blank">CIDR notation</a>.';
}
......@@ -19,9 +19,6 @@ final class MySqlFormatConstraintValidator extends ConstraintValidator {
* Validate the IP address.
*/
public function validate(mixed $field, Constraint $constraint): void {
/** @var \Drupal\Core\Field\FieldItemListInterface $field */
$settings = $field->getFieldDefinition()->getSettings();
$value = trim($field->value);
try {
......@@ -37,22 +34,27 @@ final class MySqlFormatConstraintValidator extends ConstraintValidator {
return;
}
// If IPv6.
/*
if ($ipAddress->getVersion() !== $ipAddress::IP_V6) {
$this->context->addViolation($constraint->messageIpV6);
}
*/
/** @var \Drupal\Core\Field\FieldItemListInterface $field */
$settings = $field->getFieldDefinition()->getSettings();
// If CIDR.
/*
$network = (string) Network::parse($value)->getNetwork();
$ipAddress = (string) $ipAddress;
if ($settings['family'] == 4) {
if ($ipAddress->getVersion() !== $ipAddress::IP_V4) {
$this->context->addViolation($constraint->messageIpV4);
return;
}
}
elseif ($ipAddress->getVersion() !== $ipAddress::IP_V6) {
$this->context->addViolation($constraint->messageIpV6);
return;
}
if ($ipAddress !== $network) {
$this->context->addViolation($constraint->messageCidr);
if ($settings['cidr_format']) {
$network = (string) Network::parse($value)->getNetwork();
$ipAddress = (string) $ipAddress;
if ($ipAddress !== $network) {
$this->context->addViolation($constraint->messageCidr);
}
}
*/
}
......
......@@ -21,6 +21,27 @@ final class PostgreSqlFormatConstraint extends Constraint {
*
* @var string
*/
public string $message = 'Invalid IP address format.';
public string $messageIpAddressFormat = 'Invalid IP address format.';
/**
* Message when the value isn't in the IPv4 format.
*
* @var string
*/
public string $messageIpV4 = 'The IP address is not in the IPv4 format.';
/**
* Message when the value isn't in the IPv6 format.
*
* @var string
*/
public string $messageIpV6 = 'The IP address is not in the IPv6 format.';
/**
* Message when the value isn't in the CIDR format.
*
* @var string
*/
public string $messageCidr = 'The IP address value must follow <a href="https://www.postgresql.org/docs/16/datatype-net-types.html#DATATYPE-CIDR" target="_blank">CIDR notation</a>.';
}
......@@ -34,16 +34,43 @@ final class PostgreSqlFormatConstraintValidator extends ConstraintValidator impl
/**
* {@inheritdoc}
*/
public function validate(mixed $value, Constraint $constraint): void {
$value = trim($value->value);
public function validate(mixed $field, Constraint $constraint): void {
$value = trim($field->value);
try {
$this->connection->query('SELECT inet(:ipAddressString)', [':ipAddressString' => $value]);
}
catch (\Exception $e) {
$this->context->addViolation($constraint->message);
$this->context->addViolation($constraint->messageIpAddressFormat);
return;
}
$select = "SELECT family(inet :ipAddressString)";
$query = $this->connection->query($select, [':ipAddressString' => $value]);
$family = $query->fetchField();
/** @var \Drupal\Core\Field\FieldItemListInterface $field */
$settings = $field->getFieldDefinition()->getSettings();
if ($family != $settings['family']) {
if ($settings['family'] == 4) {
$this->context->addViolation($constraint->messageIpV4);
}
else {
$this->context->addViolation($constraint->messageIpV6);
}
return;
}
if ($settings['cidr_format']) {
try {
$this->connection->query('SELECT cidr(:ipAddressString)', [':ipAddressString' => $value]);
}
catch (\Exception $e) {
$this->context->addViolation($constraint->messageCidr);
return;
}
}
}
}
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