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

Issue #3453146: Add the validators for "Default gateway" value validation

parent 3600c155
No related branches found
No related tags found
No related merge requests found
......@@ -51,4 +51,11 @@ final class MySqlFormatConstraint extends Constraint {
*/
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>.';
/**
* Message when the default gateway is not walid.
*
* @var string
*/
public string $defaultGateway = 'Invalid default gateway. The value should be between @firstIP and @lastIP.';
}
......@@ -19,7 +19,7 @@ final class MySqlFormatConstraintValidator extends ConstraintValidator {
* Validate the IP address.
*/
public function validate(mixed $field, Constraint $constraint): void {
$value = trim($field->value);
$value = $field->value;
try {
/** @var \IPTools\IP $ipAddress */
......@@ -56,6 +56,37 @@ final class MySqlFormatConstraintValidator extends ConstraintValidator {
}
}
// Validate the Default gateway if it was enabled in the field settings.
// The "Default Gateway" is an IP Address that is within the subnet of the
// "Network Interface". If the value of the "Network Interface" is
// 192.168.100.10/24, then the valid value of "Default Gateway" should be
// between 192.168.100.1 and 192.168.100.254.
if (!$settings['default_gateway_enabled']) {
return;
}
/** @var \IPTools\Range $hosts */
$hosts = Network::parse($value)->hosts;
try {
/** @var \IPTools\IP $ipAddress */
$defaultGateway = Network::parse($field->gateway_value)->getIP();
}
catch (\Exception $e) {
$this->context->addViolation($constraint->defaultGateway, [
'@firstIP' => (string) $hosts->getFirstIP(),
'@lastIP' => (string) $hosts->getLastIP(),
]);
return;
}
if (!$hosts->contains($defaultGateway)) {
$this->context->addViolation($constraint->defaultGateway, [
'@firstIP' => (string) $hosts->getFirstIP(),
'@lastIP' => (string) $hosts->getLastIP(),
]);
}
}
}
......@@ -49,6 +49,6 @@ final class PostgreSqlFormatConstraint extends Constraint {
*
* @var string
*/
public string $defaultGateway = 'Invalid default gateway.';
public string $defaultGateway = 'Invalid default gateway. The value should be between @firstIP and @lastIP.';
}
......@@ -6,6 +6,7 @@ namespace Drupal\field_ipaddress_pgsql\Plugin\Validation\Constraint;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use IPTools\Network;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
......@@ -77,24 +78,37 @@ final class PostgreSqlFormatConstraintValidator extends ConstraintValidator impl
return;
}
$defaultGateway = $field->default_gateway;
// Validate the Default gateway if it was enabled in the field settings.
// The "Default Gateway" is an IP Address that is within the subnet of the
// "Network Interface". If the value of the "Network Interface" is
// 192.168.100.10/24, then the valid value of "Default Gateway" should be
// between 192.168.100.1 and 192.168.100.254.
if (!$settings['default_gateway_enabled']) {
return;
}
try {
$query = $this->connection->query('SELECT inet(:ipAddressString) >>= inet(:defaultGateway)', [
':ipAddressString' => $value,
':defaultGateway' => $defaultGateway,
]);
$gatewayIsValid = $query->fetchField();
/** @var \IPTools\Range $hosts */
$hosts = Network::parse($value)->hosts;
if (!$gatewayIsValid) {
$this->context->addViolation($constraint->defaultGateway);
}
try {
/** @var \IPTools\IP $ipAddress */
$defaultGateway = Network::parse($field->default_gateway)->getIP();
}
catch (\Exception $e) {
$this->context->addViolation($constraint->defaultGateway);
$this->context->addViolation($constraint->defaultGateway, [
'@firstIP' => (string) $hosts->getFirstIP(),
'@lastIP' => (string) $hosts->getLastIP(),
]);
return;
}
if (!$hosts->contains($defaultGateway)) {
$this->context->addViolation($constraint->defaultGateway, [
'@firstIP' => (string) $hosts->getFirstIP(),
'@lastIP' => (string) $hosts->getLastIP(),
]);
}
}
}
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