MVC: migrate CSVListField types to more sensible fields where possible (#7118)

Extend the MacAddressField as a list type for usage in Captive Portal. Also set MaskPerItem to "Y" on Netflow destinations for now.
This commit is contained in:
Stephan de Wit 2024-01-11 13:08:15 +01:00 committed by GitHub
parent 57312292ee
commit 340b314f57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 17 deletions

View File

@ -33,8 +33,60 @@ use OPNsense\Base\Validators\CallbackValidator;
/**
* Class MacAddressField
*/
class MacAddressField extends TextField
class MacAddressField extends BaseField
{
/**
* @var bool marks if this is a data node or a container
*/
protected $internalIsContainer = false;
/**
* @var string when multiple values could be provided at once, specify the split character
*/
protected $internalFieldSeparator = ',';
/**
* @var bool when set, results are returned as list (with all options enabled)
*/
protected $internalAsList = false;
/**
* trim MAC addresses
* @param string $value
*/
public function setValue($value)
{
parent::setValue(trim($value));
}
/**
* get valid options, descriptions and selected value
* @return array
*/
public function getNodeData()
{
if ($this->internalAsList) {
// return result as list
$result = array();
foreach (explode($this->internalFieldSeparator, $this->internalValue) as $address) {
$result[$address] = array("value" => $address, "selected" => 1);
}
return $result;
} else {
// normal, single field response
return $this->internalValue;
}
}
/**
* select if multiple addresses may be selected at once
* @param $value string value Y/N
*/
public function setAsList($value)
{
$this->internalAsList = trim(strtoupper($value)) == "Y";
}
/**
* {@inheritdoc}
*/
@ -51,8 +103,10 @@ class MacAddressField extends TextField
$validators = parent::getValidators();
if ($this->internalValue != null) {
$validators[] = new CallbackValidator(["callback" => function ($data) {
if (empty(filter_var($data, FILTER_VALIDATE_MAC))) {
return [$this->getValidationMessage()];
foreach ($this->internalAsList ? explode($this->internalFieldSeparator, $data) : [$data] as $address) {
if (empty(filter_var($address, FILTER_VALIDATE_MAC))) {
return [$this->getValidationMessage()];
}
}
return [];
}

View File

@ -58,14 +58,13 @@
<Mask>/^([0-9a-zA-Z.,_\-]){0,1024}$/u</Mask>
<ValidationMessage>Please enter a valid servername, ip address or leave this option blank</ValidationMessage>
</servername>
<allowedAddresses type="CSVListField">
<Mask>/^([\/0-9.,])*/u</Mask>
<ValidationMessage>Please enter valid addresses</ValidationMessage>
<allowedAddresses type="NetworkField">
<FieldSeparator>,</FieldSeparator>
<asList>Y</asList>
<WildcardEnabled>N</WildcardEnabled>
</allowedAddresses>
<allowedMACAddresses type="CSVListField">
<ChangeCase>lower</ChangeCase>
<Mask>/^((([0-9a-fA-F]{2}:){5}([0-9a-fA-F]{2})([,]){0,1}))*/u</Mask>
<ValidationMessage>Please enter valid mac addresses</ValidationMessage>
<allowedMACAddresses type="MacAddressField">
<asList>Y</asList>
</allowedMACAddresses>
<transparentHTTPProxy type="BooleanField">
<Default>0</Default>

View File

@ -25,8 +25,9 @@
</OptionValues>
</version>
<targets type="CSVListField">
<Mask>/^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):(6553[0-5]|655[0-2][0-9]|65[0-4](\d){2}|6[0-4](\d){3}|[1-5](\d){4}|[1-9](\d){0,3})([,]){0,1})*/u</Mask>
<Mask>/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):(6553[0-5]|655[0-2][0-9]|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3})$/u</Mask>
<ValidationMessage>Please enter valid targets (e.g. 192.168.0.1:2055).</ValidationMessage>
<MaskPerItem>Y</MaskPerItem>
</targets>
</capture>
<collect>

View File

@ -22,12 +22,11 @@
<MaximumValue>86400</MaximumValue>
<ValidationMessage>Start Delay needs to be an integer value between 0 and 86400</ValidationMessage>
</startdelay>
<mailserver type="CSVListField">
<mailserver type="HostnameField">
<Default>127.0.0.1</Default>
<Required>Y</Required>
<Multiple>Y</Multiple>
<Mask>/^([0-9a-zA-Z\.,_\-:]){0,1024}$/u</Mask>
<ChangeCase>lower</ChangeCase>
<asList>Y</asList>
<FieldSeparator>,</FieldSeparator>
<ValidationMessage>Please specify a valid servername or IP address.</ValidationMessage>
</mailserver>
<port type="IntegerField">

View File

@ -39,8 +39,11 @@
<MinimumValue>1</MinimumValue>
<MaximumValue>9300</MaximumValue>
</mtu>
<dns type="CSVListField">
<Mask>/^([a-fA-F0-9\.:\[\]]*?,)*([a-fA-F0-9\.:\[\]]*)$/</Mask>
<dns type="NetworkField">
<FieldSeparator>,</FieldSeparator>
<asList>Y</asList>
<NetMaskAllowed>N</NetMaskAllowed>
<WildcardEnabled>N</WildcardEnabled>
<ValidationMessage>Please use valid IPv4 or IPv6 addresses.</ValidationMessage>
</dns>
<tunneladdress type="NetworkField">