[#4] Allow comment on device port in the log, for any user

This commit is contained in:
Oliver Gorwits
2015-01-01 17:48:25 +00:00
parent f9122ce72e
commit 06f62f97b7
8 changed files with 110 additions and 7 deletions

View File

@@ -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<device_port_log> 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<device_port_power> table if one refers to this
@@ -345,4 +355,16 @@ Returns the C<mac> column instantiated into a L<NetAddr::MAC> 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;

View File

@@ -157,6 +157,7 @@ sub delete {
DevicePortVlan
DevicePortWireless
DevicePortSsid
DevicePortLog
/) {
$schema->resultset($set)->search(
{ ip => { '-in' => $ports->as_query }},

View File

@@ -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 => '' },

View File

@@ -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');