From 06f62f97b73ca67d2d6ff2c2f9a409012e52a9e6 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Thu, 1 Jan 2015 17:48:25 +0000 Subject: [PATCH] [#4] Allow comment on device port in the log, for any user --- Netdisco/Changes | 4 +++ .../lib/App/Netdisco/DB/Result/DevicePort.pm | 22 +++++++++++++ .../App/Netdisco/DB/ResultSet/DevicePort.pm | 1 + Netdisco/lib/App/Netdisco/Web/Device.pm | 1 + .../App/Netdisco/Web/Plugin/Report/PortLog.pm | 26 ++++++++++++++++ Netdisco/share/views/ajax/device/ports.tt | 11 +++++-- Netdisco/share/views/ajax/report/portlog.tt | 21 ++++++++++--- Netdisco/share/views/js/report.js | 31 +++++++++++++++++++ 8 files changed, 110 insertions(+), 7 deletions(-) diff --git a/Netdisco/Changes b/Netdisco/Changes index cc01e03c..d0286d10 100644 --- a/Netdisco/Changes +++ b/Netdisco/Changes @@ -1,5 +1,9 @@ 2.029015 + [NEW FEATURES] + + * [#4] Allow comment on device port in the log, for any user + [ENHANCEMENTS] * Request net-snmp-devel on Fedora/Red-Hat builds diff --git a/Netdisco/lib/App/Netdisco/DB/Result/DevicePort.pm b/Netdisco/lib/App/Netdisco/DB/Result/DevicePort.pm index 93b94367..8093e42e 100644 --- a/Netdisco/lib/App/Netdisco/DB/Result/DevicePort.pm +++ b/Netdisco/lib/App/Netdisco/DB/Result/DevicePort.pm @@ -160,6 +160,16 @@ __PACKAGE__->has_many( active_nodes_with_age => 'App::Netdisco::DB::Result::Virt cascade_copy => 0, cascade_update => 0, cascade_delete => 0 }, ); +=head2 logs + +Returns the set of C entries associated with this Port. + +=cut + +__PACKAGE__->has_many( logs => 'App::Netdisco::DB::Result::DevicePortLog', + { 'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port' }, +); + =head2 power Returns a row from the C table if one refers to this @@ -345,4 +355,16 @@ Returns the C column instantiated into a L object. sub net_mac { return NetAddr::MAC->new(mac => (shift)->mac) } +=head2 last_comment + +Returns the most recent comment from the logs for this device port. + +=cut + +sub last_comment { + my $row = (shift)->logs->search(undef, + { order_by => { -desc => 'creation' }, rows => 1 })->first; + return ($row ? $row->log : ''); +} + 1; diff --git a/Netdisco/lib/App/Netdisco/DB/ResultSet/DevicePort.pm b/Netdisco/lib/App/Netdisco/DB/ResultSet/DevicePort.pm index 4b471617..91ee2682 100644 --- a/Netdisco/lib/App/Netdisco/DB/ResultSet/DevicePort.pm +++ b/Netdisco/lib/App/Netdisco/DB/ResultSet/DevicePort.pm @@ -157,6 +157,7 @@ sub delete { DevicePortVlan DevicePortWireless DevicePortSsid + DevicePortLog /) { $schema->resultset($set)->search( { ip => { '-in' => $ports->as_query }}, diff --git a/Netdisco/lib/App/Netdisco/Web/Device.pm b/Netdisco/lib/App/Netdisco/Web/Device.pm index 23ea7caa..2235d7d9 100644 --- a/Netdisco/lib/App/Netdisco/Web/Device.pm +++ b/Netdisco/lib/App/Netdisco/Web/Device.pm @@ -14,6 +14,7 @@ hook 'before' => sub { my @default_port_columns_right = ( { name => 'c_descr', label => 'Description', default => '' }, + { name => 'c_comment', label => 'Last Comment', default => '' }, { name => 'c_type', label => 'Type', default => '' }, { name => 'c_duplex', label => 'Duplex', default => '' }, { name => 'c_lastchange', label => 'Last Change', default => '' }, diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/PortLog.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/PortLog.pm index 73ca2700..5c8c9081 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/PortLog.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/PortLog.pm @@ -14,6 +14,32 @@ register_report({ hidden => true, }); +sub _sanity_ok { + return 0 unless + param('ip') =~ m/^[[:print:]]+$/ + and param('port') =~ m/^[[:print:]]+$/ + and param('log') =~ m/^[[:print:]]+$/; + + return 1; +} + +ajax '/ajax/control/report/portlog/add' => require_login sub { + send_error('Bad Request', 400) unless _sanity_ok(); + + schema('netdisco')->txn_do(sub { + my $user = schema('netdisco')->resultset('DevicePortLog') + ->create({ + ip => param('ip'), + port => param('port'), + reason => 'other', + log => param('log'), + username => session('logged_in_user'), + userip => request->remote_address, + action => 'comment', + }); + }); +}; + ajax '/ajax/content/report/portlog' => require_role port_control => sub { my $device = param('q'); my $port = param('f'); diff --git a/Netdisco/share/views/ajax/device/ports.tt b/Netdisco/share/views/ajax/device/ports.tt index 4e23349c..f91bf468 100644 --- a/Netdisco/share/views/ajax/device/ports.tt +++ b/Netdisco/share/views/ajax/device/ports.tt @@ -82,6 +82,10 @@ rel="tooltip" data-placement="top" data-offset="3" data-animation="" data-title="Enable Port"> + [% END %] + [% ELSE %] + [% END %] @@ -89,9 +93,6 @@ rel="tooltip" data-placement="top" data-offset="3" data-animation="" data-title="View Port Log"> - [% ELSE %] - - [% END %] [% IF row.is_master %] @@ -121,6 +122,10 @@ [% row.descr | html_entity %] [% END %] + [% IF params.c_comment %] + [% row.last_comment | html_entity %] + [% END %] + [% IF params.c_type %] [% row.type | html_entity %] [% END %] diff --git a/Netdisco/share/views/ajax/report/portlog.tt b/Netdisco/share/views/ajax/report/portlog.tt index 777e32d1..b229065e 100644 --- a/Netdisco/share/views/ajax/report/portlog.tt +++ b/Netdisco/share/views/ajax/report/portlog.tt @@ -1,6 +1,3 @@ -[% IF results.count == 0 %] -
This port's log is empty.
-[% ELSE %] @@ -10,9 +7,24 @@ + + + + + + + + + + + + + [% WHILE (row = results.next) %] @@ -21,15 +33,16 @@ + [% END %]
Action Reason LogAction
-[% session.logged_in_user | html_entity %]-commentOther + +
[% row.creation_stamp | html_entity %][% row.action | html_entity %] [% settings.port_control_reasons.item(row.reason) || row.reason | html_entity %] [% row.log || '-' | html_entity %]
-[% END %]