Commit 336a87ad authored by sunnavy's avatar sunnavy
Browse files

IPAddress only accepts one IP

parent e8c30250
......@@ -75,7 +75,22 @@ sub Create {
my $cf_as_sys = RT::CustomField->new(RT->SystemUser);
$cf_as_sys->Load($args{'CustomField'});
if($cf_as_sys->Type eq 'IPAddress' || $cf_as_sys->Type eq 'IPAddressRange') {
if($cf_as_sys->Type eq 'IPAddress') {
unless ( defined $args{'Content'} ) {
return
wantarray
? ( 0, $self->loc("Content can't be empty for IPAddress") )
: 0;
}
if ( $args{'Content'} =~ /^\s*$RE{net}{IPv4}\s*$/o ) {
$args{'Content'} = sprintf "%03d.%03d.%03d.%03d", split /\./,
$args{'Content'};
}
}
if($cf_as_sys->Type eq 'IPAddressRange') {
if ($args{'Content'}) {
($args{'Content'}, $args{'LargeContent'}) = $self->ParseIPRange( $args{'Content'} );
}
......@@ -83,12 +98,11 @@ sub Create {
unless ( defined $args{'Content'} ) {
return
wantarray
? ( 0, $self->loc("Content can't be empty for IPAddress(Range)") )
? ( 0, $self->loc("Content can't be empty for IPAddressRange") )
: 0;
}
}
if ( defined $args{'Content'} && length( Encode::encode_utf8($args{'Content'}) ) > 255 ) {
if ( defined $args{'LargeContent'} && length $args{'LargeContent'} ) {
$RT::Logger->error("Content is longer than 255 bytes and LargeContent specified");
......@@ -226,27 +240,31 @@ sub Content {
my $self = shift;
my $content = $self->_Value('Content');
if ( $self->CustomFieldObj->Type eq 'IPAddress' ||
$self->CustomFieldObj->Type eq 'IPAddressRange') {
if ( $self->CustomFieldObj->Type eq 'IPAddress'
|| $self->CustomFieldObj->Type eq 'IPAddressRange' )
{
if ( $content =~ /^\s*($re_ip_serialized)\s*$/o ) {
$content = sprintf "%d.%d.%d.%d", split /\./, $1;
}
if ($content =~ /^\s*($re_ip_serialized)\s*$/o ) {
$content = sprintf "%d.%d.%d.%d", split /\./, $1;
}
return $content if $self->CustomFieldObj->Type eq 'IPAddress';
my $large_content = $self->__Value('LargeContent');
if ( $large_content =~ /^\s*($re_ip_serialized)\s*$/o ) {
my $eIP = sprintf "%d.%d.%d.%d", split /\./, $1;
if ( $content eq $eIP ) {
return $content;
}
else {
return $content . "-".$eIP;
}
} else {
if ( $large_content =~ /^\s*($re_ip_serialized)\s*$/o ) {
my $eIP = sprintf "%d.%d.%d.%d", split /\./, $1;
if ( $content eq $eIP ) {
return $content;
}
else {
return $content . "-" . $eIP;
}
}
else {
return $content;
}
}
if ( !(defined $content && length $content) && $self->ContentType && $self->ContentType eq 'text/plain' ) {
return $self->LargeContent;
} else {
......
......@@ -1379,8 +1379,15 @@ sub _CustomFieldLimit {
return 'NOT MATCHES' if $op eq '!=';
return $op;
};
if ( $cf
&& $cf->Type eq 'IPAddress'
&& $value =~ /^\s*$RE{net}{IPv4}\s*$/o )
{
$value = sprintf "%03d.%03d.%03d.%03d", split /\./, $value;
}
if ( $cf
&& ( $cf->Type eq 'IPAddress' || $cf->Type eq 'IPAddressRange' )
&& $value =~ /^\s*$RE{net}{CIDR}{IPv4}{-keep}\s*$/o )
{
......@@ -1420,7 +1427,7 @@ sub _CustomFieldLimit {
) if $CFs;
$self->_CloseParen;
}
elsif ( $op !~ /^[<>]=?$/ && ( $cf && ($cf->Type eq 'IPAddress' || $cf->Type eq 'IPAddressRange'))) {
elsif ( $op !~ /^[<>]=?$/ && ( $cf && $cf->Type eq 'IPAddressRange')) {
$value =~ /^\s*($RE{net}{IPv4})\s*(?:-\s*($RE{net}{IPv4})\s*)?$/o;
my ($start_ip, $end_ip) = ($1, ($2 || $1));
......
......@@ -82,17 +82,27 @@ while ( my $CF = $CustomFields->Next ) {
@values = ('') unless @values;
for my $value( @values ) {
if ( $value
&& ( $CF->Type eq 'IPAddress' || $CF->Type eq 'IPAddressRange' ) )
{
my ( $start_ip, $end_ip ) =
RT::ObjectCustomFieldValue->ParseIPRange($value);
unless ( $start_ip && $end_ip ) {
my $msg =
loc( "Input can not be parsed to " . loc( $CF->Type ) );
$m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
push @res, $msg;
$valid = 0;
if ($value) {
if ( $CF->Type eq 'IPAddress' ) {
use Regexp::Common qw(RE_net_IPv4);
unless ( $value =~ /^$RE{net}{IPv4}$/ ) {
my $msg =
loc( "Input can not be parsed to " . loc( $CF->Type ) );
$m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
push @res, $msg;
$valid = 0;
}
}
elsif ( $CF->Type eq 'IPAddressRange' ) {
my ( $start_ip, $end_ip ) =
RT::ObjectCustomFieldValue->ParseIPRange($value);
unless ( $start_ip && $end_ip ) {
my $msg =
loc( "Input can not be parsed to " . loc( $CF->Type ) );
$m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
push @res, $msg;
$valid = 0;
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment