diff --git a/lib/App/Netdisco/Web/Plugin/Search/Port.pm b/lib/App/Netdisco/Web/Plugin/Search/Port.pm index 63c73a7b..cb82892d 100644 --- a/lib/App/Netdisco/Web/Plugin/Search/Port.pm +++ b/lib/App/Netdisco/Web/Plugin/Search/Port.pm @@ -7,6 +7,9 @@ use Dancer::Plugin::Auth::Extensible; use App::Netdisco::Web::Plugin; use App::Netdisco::Util::Web 'sql_match'; +use Regexp::Common 'net'; +use NetAddr::MAC (); + register_search_tab({ tag => 'port', label => 'Port', @@ -41,34 +44,38 @@ get '/ajax/content/search/port' => require_login sub { send_error( 'Missing query', 400 ) unless $q; my $rs; - if ( $q =~ m/^\d+$/ ) { - $rs - = schema('netdisco')->resultset('DevicePort') - ->columns( [qw/ ip port name up up_admin speed /] )->search({ - "port_vlans.vlan" => $q, - ( param('uplink') ? () : (-or => [ - {-not_bool => "me.is_uplink"}, - {"me.is_uplink" => undef}, - ]) ), - ( param('ethernet') ? ("me.type" => 'ethernetCsmacd') : () ), - },{ '+columns' => [qw/ device.dns device.name port_vlans.vlan /], - join => [qw/ port_vlans device /] - } - )->with_times; + if ($q =~ m/^[0-9]+$/ and $q < 4096) { + $rs = schema('netdisco')->resultset('DevicePort') + ->columns( [qw/ ip port name up up_admin speed /] )->search({ + "port_vlans.vlan" => $q, + ( param('uplink') ? () : (-or => [ + {-not_bool => "me.is_uplink"}, + {"me.is_uplink" => undef}, + ]) ), + ( param('ethernet') ? ("me.type" => 'ethernetCsmacd') : () ), + },{ '+columns' => [qw/ device.dns device.name port_vlans.vlan /], + join => [qw/ port_vlans device /] + } + )->with_times; } else { my ( $likeval, $likeclause ) = sql_match($q); + my $mac = NetAddr::MAC->new($q); - $rs - = schema('netdisco')->resultset('DevicePort') + undef $mac if + ($mac and $mac->as_ieee + and (($mac->as_ieee eq '00:00:00:00:00:00') + or ($mac->as_ieee !~ m/$RE{net}{MAC}/))); + + $rs = schema('netdisco')->resultset('DevicePort') ->columns( [qw/ ip port name up up_admin speed /] ) ->search({ -and => [ -or => [ { "me.name" => ( param('partial') ? $likeclause : $q ) }, - ( length $q == 17 - ? { "me.mac" => $q } - : \[ 'me.mac::text ILIKE ?', $likeval ] + ( ((!defined $mac) or $mac->errstr) + ? \[ 'me.mac::text ILIKE ?', $likeval ] + : { 'me.mac' => $mac->as_ieee } ), ( param('uplink') ? ( { "me.remote_id" => $likeclause }, diff --git a/lib/App/Netdisco/Web/Search.pm b/lib/App/Netdisco/Web/Search.pm index 2f59e2c6..cbf5718b 100644 --- a/lib/App/Netdisco/Web/Search.pm +++ b/lib/App/Netdisco/Web/Search.pm @@ -62,7 +62,7 @@ get '/search' => require_login sub { ->search({ -or => [ {name => $likeclause}, - ((!defined $mac or $mac->errstr) + (((!defined $mac) or $mac->errstr) ? \['mac::text ILIKE ?', $likeval] : {mac => $mac->as_ieee}), ],