make the branch authonly

This commit is contained in:
Oliver Gorwits
2019-03-17 20:08:06 +00:00
parent 33f94eac62
commit 46bc2c6feb
4 changed files with 9 additions and 145 deletions

View File

@@ -13,24 +13,19 @@ use base 'DBIx::Class::Core';
__PACKAGE__->table("node_ip"); __PACKAGE__->table("node_ip");
__PACKAGE__->add_columns( __PACKAGE__->add_columns(
"mac", "mac",
{ data_type => "macaddr", is_nullable => 0, { data_type => "macaddr", is_nullable => 0 },
extra => { descr => 'MAC address' } },
"ip", "ip",
{ data_type => "inet", is_nullable => 0, { data_type => "inet", is_nullable => 0 },
extra => { descr => 'IP address' } },
"dns", "dns",
{ data_type => "text", is_nullable => 1, { data_type => "text", is_nullable => 1 },
extra => { descr => 'FQDN of the node' } },
"active", "active",
{ data_type => "boolean", is_nullable => 1, { data_type => "boolean", is_nullable => 1 },
extra => { descr => 'Whether the entry is still "fresh"' } },
"time_first", "time_first",
{ {
data_type => "timestamp", data_type => "timestamp",
default_value => \"current_timestamp", default_value => \"current_timestamp",
is_nullable => 1, is_nullable => 1,
original => { default_value => \"now()" }, original => { default_value => \"now()" },
extra => { hide_from_api => 1 },
}, },
"time_last", "time_last",
{ {
@@ -38,7 +33,6 @@ __PACKAGE__->add_columns(
default_value => \"current_timestamp", default_value => \"current_timestamp",
is_nullable => 1, is_nullable => 1,
original => { default_value => \"now()" }, original => { default_value => \"now()" },
extra => { hide_from_api => 1 },
}, },
); );
__PACKAGE__->set_primary_key("mac", "ip"); __PACKAGE__->set_primary_key("mac", "ip");

View File

@@ -1,102 +0,0 @@
package App::Netdisco::Util::API;
use strict;
use warnings;
use Dancer ':syntax';
use Dancer::Plugin::DBIC 'schema';
use List::MoreUtils 'singleton';
use base 'Exporter';
our @EXPORT = ();
our @EXPORT_OK = qw/
resultsource_to_openapi_params
parse_search_params
format_data
format_error
/;
our %EXPORT_TAGS = (all => \@EXPORT_OK);
sub resultsource_to_openapi_params {
my $sourcename = shift or return ();
my @params = ();
my $rs = schema('netdisco')->source($sourcename) or return ();
my $columns = $rs->columns_info;
foreach my $col ($rs->primary_columns,
(singleton ($rs->primary_columns, keys %{ $columns }))) {
my $data = $columns->{$col};
next if $data->{extra}->{hide_from_api};
push @params, (
$col => {
description => $data->{extra}->{descr},
type => ($data->{data_type} =~ m/int/ ? 'integer'
: $data->{data_type} eq 'boolean' ? 'boolean' : 'string'),
}
);
}
return @params;
}
sub parse_search_params {
my $params = shift;
my $search = {};
my $partial = $params->{partial} || false;
foreach my $param (keys %{$params}) {
if ($param ne 'return_url' and $param ne 'partial') {
if ($partial eq 'true') {
$search->{"text(".$param.")"} = { -ilike => '%'.$params->{$param}.'%'};
}
else {
$search->{$param} = $params->{$param};
}
}
}
return $search;
}
sub format_data {
my $items = shift;
my $results = {};
if (ref($items) =~ m/ResultSet/) {
my @hashes;
foreach my $item ($items->all) {
my $c = {};
my $columns = $item->{_column_data};
foreach my $col (keys %{$columns}) {
$c->{$col} = $columns->{$col};
}
push @hashes, $c;
}
$results->{data} = \@hashes;
}
elsif (ref($items) =~ m/Result/) {
$results->{data} = $items->{_column_data};
}
else {
$results->{data} = $items;
}
header('Content-Type' => 'application/json');
return to_json $results;
};
sub format_error {
my $status = shift;
my $message = shift;
status $status;
header('Content-Type' => 'application/json');
return to_json { error => $message };
}
true;

View File

@@ -232,14 +232,6 @@ hook 'after' => sub {
} }
}; };
# forward API calls to AJAX route handlers
any '/api/:type/:identifier/:method' => require_login sub {
vars->{'is_api'} = 1;
my $target =
sprintf '/ajax/content/%s/%s', params->{'type'}, params->{'method'};
forward $target, { tab => params->{'method'}, q => params->{'identifier'} };
};
any qr{.*} => sub { any qr{.*} => sub {
var('notfound' => true); var('notfound' => true);
status 'not_found'; status 'not_found';

View File

@@ -4,25 +4,13 @@ use Dancer ':syntax';
use Dancer::Plugin::Ajax; use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC; use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible; use Dancer::Plugin::Auth::Extensible;
use Dancer::Plugin::Swagger;
use App::Netdisco::Web::Plugin; use App::Netdisco::Web::Plugin;
register_device_tab({ tag => 'details', label => 'Details' }); register_device_tab({ tag => 'details', label => 'Details' });
# device details table # device details table
swagger_path { ajax '/ajax/content/device/details' => require_login sub {
description => 'Get properties and power details for a device.',
path => '/api/device/{identifier}/details',
tags => ['Devices'],
parameters => [
{ name => 'identifier', in => 'path', required => 1, type => 'string' },
],
responses => { default => { examples => {
'application/json' => { device => {}, power => {} },
} } },
},
get '/ajax/content/device/details' => require_login sub {
my $q = param('q'); my $q = param('q');
my $device = schema('netdisco')->resultset('Device') my $device = schema('netdisco')->resultset('Device')
->search_for_device($q) or send_error('Bad device', 400); ->search_for_device($q) or send_error('Bad device', 400);
@@ -36,18 +24,10 @@ get '/ajax/content/device/details' => require_login sub {
= schema('netdisco')->resultset('DevicePower') = schema('netdisco')->resultset('DevicePower')
->search( { 'me.ip' => $device->ip } )->with_poestats->hri->all; ->search( { 'me.ip' => $device->ip } )->with_poestats->hri->all;
if (vars->{'is_api'}) { content_type('text/html');
content_type('application/json'); template 'ajax/device/details.tt', {
# TODO merge power into device details d => $results[0], p => \@power
# TODO remove sensitive data (community) }, { layout => undef };
to_json { device => $results[0], power => \@power };
}
else {
content_type('text/html');
template 'ajax/device/details.tt', {
d => $results[0], p => \@power
}, { layout => undef };
}
}; };
1; 1;