Commit 03dfc5e4 authored by Kevin Falcone's avatar Kevin Falcone
Browse files

Merge branch '4.2/ip-address-canonicalization' into 4.2.5-releng

parents 145562b3 cd94a08c
......@@ -1675,12 +1675,6 @@ sub AddValueForObject {
}
}
if (my $canonicalizer = $self->can('_CanonicalizeValue'.$self->Type)) {
$canonicalizer->($self, \%args);
}
my $newval = RT::ObjectCustomFieldValue->new( $self->CurrentUser );
my ($val, $msg) = $newval->Create(
ObjectType => ref($obj),
......@@ -1702,6 +1696,17 @@ sub AddValueForObject {
}
sub _CanonicalizeValue {
my $self = shift;
my $args = shift;
my $type = $self->_Value('Type');
return 1 unless $type;
my $method = '_CanonicalizeValue'. $type;
return 1 unless $self->can($method);
$self->$method($args);
}
sub _CanonicalizeValueDateTime {
my $self = shift;
......@@ -1710,6 +1715,7 @@ sub _CanonicalizeValueDateTime {
$DateObj->Set( Format => 'unknown',
Value => $args->{'Content'} );
$args->{'Content'} = $DateObj->ISO;
return 1;
}
# For date, we need to store Content as ISO date
......@@ -1724,6 +1730,33 @@ sub _CanonicalizeValueDate {
Value => $args->{'Content'},
);
$args->{'Content'} = $DateObj->Date( Timezone => 'user' );
return 1;
}
sub _CanonicalizeValueIPAddress {
my $self = shift;
my $args = shift;
$args->{Content} = RT::ObjectCustomFieldValue->ParseIP( $args->{Content} );
return (0, $self->loc("Content is not a valid IP address"))
unless $args->{Content};
return 1;
}
sub _CanonicalizeValueIPAddressRange {
my $self = shift;
my $args = shift;
my $content = $args->{Content};
$content .= "-".$args->{LargeContent} if $args->{LargeContent};
($args->{Content}, $args->{LargeContent})
= RT::ObjectCustomFieldValue->ParseIPRange( $content );
$args->{ContentType} = 'text/plain';
return (0, $self->loc("Content is not a valid IP address range"))
unless $args->{Content};
return 1;
}
=head2 MatchPattern STRING
......
......@@ -82,36 +82,11 @@ sub Create {
@_,
);
my $cf = RT::CustomField->new( $self->CurrentUser );
$cf->Load( $args{CustomField} );
my $cf_as_sys = RT::CustomField->new(RT->SystemUser);
$cf_as_sys->Load($args{'CustomField'});
if($cf_as_sys->Type eq 'IPAddress') {
if ( $args{'Content'} ) {
$args{'Content'} = $self->ParseIP( $args{'Content'} );
}
unless ( defined $args{'Content'} ) {
return
wantarray
? ( 0, $self->loc("Content is an invalid IP address") )
: 0;
}
}
if($cf_as_sys->Type eq 'IPAddressRange') {
if ($args{'Content'}) {
($args{'Content'}, $args{'LargeContent'}) = $self->ParseIPRange( $args{'Content'} );
}
$args{'ContentType'} = 'text/plain';
unless ( defined $args{'Content'} ) {
return
wantarray
? ( 0, $self->loc("Content is an invalid IP address range") )
: 0;
}
}
my ($val, $msg) = $cf->_CanonicalizeValue(\%args);
return ($val, $msg) unless $val;
if ( defined $args{'Content'} && length( Encode::encode_utf8($args{'Content'}) ) > 255 ) {
if ( defined $args{'LargeContent'} && length $args{'LargeContent'} ) {
......@@ -165,16 +140,10 @@ sub LoadByCols {
if ( $args{CustomField} ) {
$cf = RT::CustomField->new( $self->CurrentUser );
$cf->Load( $args{CustomField} );
if ( $cf->Type && $cf->Type eq 'IPAddressRange' ) {
my ( $sIP, $eIP ) = $cf->ParseIPRange( $args{'Content'} );
if ( $sIP && $eIP ) {
$self->SUPER::LoadByCols( %args,
Content => $sIP,
LargeContent => $eIP
);
}
}
return (0, $self->loc("Cannot load custom field [_1]",$args{CustomField})) unless $cf->id;
my ($ok, $msg) = $cf->_CanonicalizeValue(\%args);
return ($ok, $msg) unless $ok;
}
return $self->SUPER::LoadByCols(%args);
}
......
......@@ -135,12 +135,8 @@ sub HasEntry {
my $args = $canon_value{ $cf->Type };
if ( !$args ) {
$args = { Content => $value, LargeContent => $large_content };
if ( my $canonicalizer =
$cf->can( '_CanonicalizeValue' . $cf->Type ) )
{
$canonicalizer->( $cf, $args );
}
my ($ok, $msg) = $cf->_CanonicalizeValue( $args );
next unless $ok;
$canon_value{ $cf->Type } = $args;
}
......@@ -149,7 +145,7 @@ sub HasEntry {
return $item if lc $item->Content eq lc $args->{Content};
}
else {
if ( $item->Content eq $args->{Content} ) {
if ( $item->_Value('Content') eq $args->{Content} ) {
if ( defined $item->LargeContent ) {
return $item
if defined $args->{LargeContent}
......
......@@ -1981,8 +1981,8 @@ sub _AddCustomFieldValue {
$i++;
if ( $i < $cf_values ) {
my ( $val, $msg ) = $cf->DeleteValueForObject(
Object => $self,
Content => $value->Content
Object => $self,
Id => $value->id,
);
unless ($val) {
return ( 0, $msg );
......
......@@ -529,13 +529,6 @@ sub _LimitCustomField {
$RT::Logger->warn("$value is not a valid IPAddress");
}
} elsif ( $type eq 'IPAddressRange' ) {
if ( $value =~ /^\s*$RE{net}{CIDR}{IPv4}{-keep}\s*$/o ) {
# convert incomplete 192.168/24 to 192.168.0.0/24 format
$value =
join( '.', map $_ || 0, ( split /\./, $1 )[ 0 .. 3 ] ) . "/$2"
|| $value;
}
my ( $start_ip, $end_ip ) =
RT::ObjectCustomFieldValue->ParseIPRange($value);
if ( $start_ip && $end_ip ) {
......
......@@ -95,27 +95,12 @@ while ( my $CF = $CustomFields->Next ) {
for my $value( @values ) {
if ($value) {
if ( $CF->Type eq 'IPAddress' ) {
use Regexp::Common qw(RE_net_IPv4);
my $ip = RT::ObjectCustomFieldValue->ParseIP( $value );
unless ( $ip ) {
my $msg =
loc( "Input can not be parsed as an IP address" );
$m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
push @res, $CF->Name .': '. $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 as an IP address range" );
$m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
push @res, $CF->Name .': '. $msg;
$valid = 0;
}
my $ref = { Content => $value };
my ($ok, $msg) = $CF->_CanonicalizeValue( $ref );
unless ($ok) {
$m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
push @res, $CF->Name .': '. $msg;
$valid = 0;
}
}
......
......@@ -147,7 +147,7 @@ diag "check that we parse correct IPs only" if $ENV{'TEST_VERBOSE'};
}
);
$agent->content_contains( 'can not be parsed as an IP address',
$agent->content_contains( 'is not a valid IP address',
'ticket fails to create' );
}
......
......@@ -197,7 +197,7 @@ diag "check that we parse correct IPs only" if $ENV{'TEST_VERBOSE'};
}
);
$agent->content_like( qr/can not be parsed as an IP address range/, 'ticket fails to create' );
$agent->content_like( qr/is not a valid IP address range/, 'ticket fails to create' );
}
}
......
......@@ -193,7 +193,7 @@ diag "check that we parse correct IPs only" if $ENV{'TEST_VERBOSE'};
}
);
$agent->content_like( qr/can not be parsed as an IP address range/,
$agent->content_like( qr/is not a valid IP address range/,
'ticket fails to create' );
}
......
......@@ -150,7 +150,7 @@ diag "check that we parse correct IPs only" if $ENV{'TEST_VERBOSE'};
}
);
$agent->content_contains( 'can not be parsed as an IP address',
$agent->content_contains( 'is not a valid IP address',
'ticket fails to create' );
}
}
......
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