#721 redux - Port Search handles VLAN and MAC addr properly

This commit is contained in:
Oliver Gorwits
2020-06-29 19:00:05 +01:00
parent 170ea64b7e
commit 88ce0d7f9a
2 changed files with 27 additions and 20 deletions

View File

@@ -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,9 +44,8 @@ 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')
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 => [
@@ -58,17 +60,22 @@ get '/ajax/content/search/port' => require_login sub {
}
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 },

View File

@@ -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}),
],