MT#12707 - add "malicious calls" feature

Change-Id: I53138d7ba591f8e138712eb7e6252007fc20d671
changes/05/2405/5
Kirill Solomko 11 years ago
parent 5a182d037a
commit 1db8dac5b6

@ -0,0 +1,148 @@
package NGCP::Panel::Controller::API::MaliciousCalls;
use Sipwise::Base;
use namespace::sweep;
use boolean qw(true);
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use MooseX::ClassAttribute qw(class_has);
use NGCP::Panel::Utils::DateTime;
use Path::Tiny qw(path);
use Safe::Isa qw($_isa);
BEGIN { extends 'Catalyst::Controller::ActionRole'; }
require Catalyst::ActionRole::ACL;
require Catalyst::ActionRole::CheckTrailingSlash;
require Catalyst::ActionRole::HTTPMethods;
require Catalyst::ActionRole::RequireSSL;
class_has 'api_description' => (
is => 'ro',
isa => 'Str',
default =>
'Defines a registered malicious calls list.',
);
class_has 'query_params' => (
is => 'ro',
isa => 'ArrayRef',
default => sub {[
{
param => 'reseller_id',
description => 'Filter for malicious calls belonging to a specific reseller',
query => {
first => sub {
my $q = shift;
{ 'reseller.id' => $q };
},
second => sub {
return { join => { 'subscriber' => {
'contract' => {
'contact' => 'reseller' } } } },
},
},
},
]},
);
with 'NGCP::Panel::Role::API::MaliciousCalls';
class_has('resource_name', is => 'ro', default => 'maliciouscalls');
class_has('dispatch_path', is => 'ro', default => '/api/maliciouscalls/');
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-maliciouscalls');
__PACKAGE__->config(
action => {
map { $_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => [qw/admin reseller/],
Args => 0,
Does => [qw(ACL CheckTrailingSlash RequireSSL)],
Method => $_,
Path => __PACKAGE__->dispatch_path,
} } @{ __PACKAGE__->allowed_methods }
},
action_roles => [qw(HTTPMethods)],
);
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$self->log_request($c);
}
sub GET :Allow {
my ($self, $c) = @_;
my $page = $c->request->params->{page} // 1;
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->all) {
push @embedded, $self->hal_from_item($c, $item, $form);
push @links, Data::HAL::Link->new(
relation => 'ngcp:'.$self->resource_name,
href => sprintf('/%s%d', $c->request->path, $item->id),
);
}
push @links,
Data::HAL::Link->new(
relation => 'curies',
href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}',
name => 'ngcp',
templated => true,
),
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
Data::HAL::Link->new(relation => 'self', href => sprintf('/%s?page=%s&rows=%s', $c->request->path, $page, $rows));
if(($total_count / $rows) > $page ) {
push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page + 1, $rows));
}
if($page > 1) {
push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page - 1, $rows));
}
my $hal = Data::HAL->new(
embedded => [@embedded],
links => [@links],
);
$hal->resource({
total_count => $total_count,
});
my $response = HTTP::Response->new(HTTP_OK, undef,
HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json);
$c->response->headers($response->headers);
$c->response->body($response->content);
return;
}
return;
}
sub HEAD :Allow {
my ($self, $c) = @_;
$c->forward(qw(GET));
$c->response->body(q());
return;
}
sub OPTIONS :Allow {
my ($self, $c) = @_;
my $allowed_methods = $self->allowed_methods_filtered($c);
$c->response->headers(HTTP::Headers->new(
Allow => $allowed_methods->join(', '),
Accept_Post => 'application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-'.$self->resource_name,
));
$c->response->content_type('application/json');
$c->response->body(JSON::to_json({ methods => $allowed_methods })."\n");
return;
}
sub end : Private {
my ($self, $c) = @_;
$self->log_response($c);
}
# vim: set tabstop=4 expandtab:

@ -0,0 +1,108 @@
package NGCP::Panel::Controller::API::MaliciousCallsItem;
use Sipwise::Base;
use namespace::sweep;
use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use MooseX::ClassAttribute qw(class_has);
use NGCP::Panel::Utils::DateTime;
use NGCP::Panel::Utils::ValidateJSON qw();
use Path::Tiny qw(path);
use Safe::Isa qw($_isa);
BEGIN { extends 'Catalyst::Controller::ActionRole'; }
require Catalyst::ActionRole::ACL;
require Catalyst::ActionRole::HTTPMethods;
require Catalyst::ActionRole::RequireSSL;
with 'NGCP::Panel::Role::API::MaliciousCalls';
class_has('resource_name', is => 'ro', default => 'maliciouscalls');
class_has('dispatch_path', is => 'ro', default => '/api/maliciouscalls/');
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-maliciouscalls');
__PACKAGE__->config(
action => {
map { $_ => {
ACLDetachTo => '/api/root/invalid_user',
AllowedRole => [qw/admin reseller/],
Args => 1,
Does => [qw(ACL RequireSSL)],
Method => $_,
Path => __PACKAGE__->dispatch_path,
} } @{ __PACKAGE__->allowed_methods }
},
action_roles => [qw(HTTPMethods)],
);
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$self->log_request($c);
}
sub GET :Allow {
my ($self, $c, $id) = @_;
{
last unless $self->valid_id($c, $id);
my $item = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, maliciouscall => $item);
my $hal = $self->hal_from_item($c, $item);
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
(map { # XXX Data::HAL must be able to generate links with multiple relations
s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|;
s/rel=self/rel="item self"/;
$_
} $hal->http_headers),
), $hal->as_json);
$c->response->headers($response->headers);
$c->response->body($response->content);
return;
}
return;
}
sub HEAD :Allow {
my ($self, $c, $id) = @_;
$c->forward(qw(GET));
$c->response->body(q());
return;
}
sub OPTIONS :Allow {
my ($self, $c, $id) = @_;
my $allowed_methods = $self->allowed_methods_filtered($c);
$c->response->headers(HTTP::Headers->new(
Allow => $allowed_methods->join(', '),
Accept_Patch => 'application/json-patch+json',
));
$c->response->content_type('application/json');
$c->response->body(JSON::to_json({ methods => $allowed_methods })."\n");
return;
}
sub DELETE :Allow {
my ($self, $c, $id) = @_;
my $guard = $c->model('DB')->txn_scope_guard;
{
my $item = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, maliciouscall => $item);
$item->delete;
$guard->commit;
$c->response->status(HTTP_NO_CONTENT);
$c->response->body(q());
}
return;
}
sub end : Private {
my ($self, $c) = @_;
$self->log_response($c);
}
# vim: set tabstop=4 expandtab:

@ -0,0 +1,123 @@
package NGCP::Panel::Controller::MaliciousCall;
use Sipwise::Base;
BEGIN { extends 'Catalyst::Controller'; }
use NGCP::Panel::Form::MaliciousCall::Reseller;
use NGCP::Panel::Form::MaliciousCall::Admin;
use NGCP::Panel::Utils::Message;
sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) :AllowedRole(reseller) {
my ($self, $c) = @_;
$c->log->debug(__PACKAGE__ . '::auto');
NGCP::Panel::Utils::Navigation::check_redirect_chain(c => $c);
return 1;
}
sub mcid_list :Chained('/') :PathPart('maliciouscall') :CaptureArgs(0) {
my ( $self, $c ) = @_;
if($c->user->roles eq "admin") {
my $mcid_rs = $c->model('DB')->resultset('malicious_calls');
$c->stash->{mcid_dt_columns} = NGCP::Panel::Utils::Datatables::set_columns($c, [
{ name => 'id', search => 1, title => $c->loc('#') },
{ name => 'subscriber.contract.contact.reseller.name', search => 1, title => $c->loc('Reseller') },
{ name => 'call_id', search => 1, title => $c->loc('Call-Id') },
{ name => 'caller', search => 1, title => $c->loc('Caller') },
{ name => 'callee', search => 1, title => $c->loc('Callee') },
{ name => 'start_time', search => 1, title => $c->loc('Called at') },
{ name => 'duration', search => 1, title => $c->loc('Duration') },
{ name => 'reported_at', search => 1, title => $c->loc('Reported at') },
]);
$c->stash->{mcid_rs} = $mcid_rs;
} elsif($c->user->roles eq "reseller") {
my $mcid_rs = $c->model('DB')->resultset('malicious_calls')
->search({
'reseller.id' => $c->user->reseller_id,
},{
join => { 'subscriber' => { 'contract' => { 'contact' => 'reseller' } } },
});
#my $mcid_rs = $mcid_rs->search({
# reseller_id => $c->user->reseller_id,
#});
$c->stash->{mcid_dt_columns} = NGCP::Panel::Utils::Datatables::set_columns($c, [
{ name => 'id', search => 1, title => $c->loc('#') },
{ name => 'call_id', search => 1, title => $c->loc('Call-Id') },
{ name => 'caller', search => 1, title => $c->loc('Caller') },
{ name => 'callee', search => 1, title => $c->loc('Callee') },
{ name => 'start_time', search => 1, title => $c->loc('Called at') },
{ name => 'duration', search => 1, title => $c->loc('Duration') },
{ name => 'reported_at', search => 1, title => $c->loc('Reported at') },
]);
$c->stash->{mcid_rs} = $mcid_rs;
}
$c->stash(template => 'maliciouscall/list.tt');
}
sub root :Chained('mcid_list') :PathPart('') :Args(0) {
my ($self, $c) = @_;
}
sub ajax :Chained('mcid_list') :PathPart('ajax') :Args(0) {
my ($self, $c) = @_;
my $rs = $c->stash->{mcid_rs};
NGCP::Panel::Utils::Datatables::process($c, $rs, $c->stash->{mcid_dt_columns});
$c->detach( $c->view("JSON") );
}
sub base :Chained('mcid_list') :PathPart('') :CaptureArgs(1) {
my ($self, $c, $mcid_id) = @_;
unless($mcid_id && $mcid_id->is_integer) {
NGCP::Panel::Utils::Message->error(
c => $c,
log => 'Invalid malicious call id detected',
desc => $c->log('Invalid malicious call id detected'),
);
$c->response->redirect($c->uri_for());
$c->detach;
return;
}
my $res = $c->stash->{mcid_rs}->find($mcid_id);
unless(defined($res)) {
NGCP::Panel::Utils::Message->error(
c => $c,
log => 'Malicious call does not exist',
desc => $c->log('Malicious call does not exist'),
);
$c->response->redirect($c->uri_for());
$c->detach;
return;
}
$c->stash->{mcid_res} = $res;
}
sub delete :Chained('base') :PathPart('delete') {
my ($self, $c) = @_;
$c->detach('/denied_page')
if(($c->user->roles eq "admin" || $c->user->roles eq "reseller") && $c->user->read_only);
try {
$c->stash->{mcid_res}->delete;
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $c->loc('Malicious call successfully deleted'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
data => { id => $$c->stash->{mcid_res}->id },
desc => $c->loc('Failed to delete Malicious call'),
);
}
$c->response->redirect($c->uri_for());
}
__PACKAGE__->meta->make_immutable;
1;
# vim: set tabstop=4 expandtab:

@ -0,0 +1,20 @@
package NGCP::Panel::Form::MaliciousCall::Admin;
use HTML::FormHandler::Moose;
extends 'NGCP::Panel::Form::MaliciousCall::Reseller';
use Moose::Util::TypeConstraints;
has_field 'reseller' => (
type => '+NGCP::Panel::Field::Reseller',
validate_when_empty => 1,
);
has_block 'fields' => (
tag => 'div',
class => [qw/modal-body/],
render_list => [qw/reseller call_id caller callee start_time duration reported_at/],
);
1;
# vim: set tabstop=4 expandtab:

@ -0,0 +1,66 @@
package NGCP::Panel::Form::MaliciousCall::Reseller;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
use Moose::Util::TypeConstraints;
use HTML::FormHandler::Widget::Block::Bootstrap;
has '+widget_wrapper' => ( default => 'Bootstrap' );
has_field 'submitid' => ( type => 'Hidden' );
sub build_render_list {[qw/submitid fields actions/]}
sub build_form_element_class {[qw(form-horizontal)]}
has_field 'id' => (
type => 'Hidden',
);
has_field 'callee_uuid' => (
type => 'Hidden',
);
has_field 'call_id' => (
type => 'Text',
label => 'Call-Id',
required => 1,
);
has_field 'caller' => (
type => 'Text',
label => 'Caller',
required => 1,
);
has_field 'callee' => (
type => 'Text',
label => 'Callee',
required => 1,
);
has_field 'start_time' => (
type => 'Text',
label => 'Called at',
required => 1,
);
has_field 'duration' => (
type => 'PosInteger',
label => 'Duration',
required => 1,
);
has_field 'reported_at' => (
type => 'Text',
label => 'Reported at',
required => 1,
);
has_block 'fields' => (
tag => 'div',
class => [qw/modal-body/],
render_list => [qw/id callee_uuid call_id caller callee start_time duration reported_at/],
);
1;
# vim: set tabstop=4 expandtab:

@ -0,0 +1,83 @@
package NGCP::Panel::Role::API::MaliciousCalls;
use Moose::Role;
use Sipwise::Base;
with 'NGCP::Panel::Role::API' => {
-alias =>{ item_rs => '_item_rs', },
-excludes => [ 'item_rs' ],
};
use boolean qw(true);
use TryCatch;
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);
use NGCP::Panel::Form::MaliciousCall::Admin;
use NGCP::Panel::Form::MaliciousCall::Reseller;
use Data::Dumper;
sub item_rs {
my ($self, $c) = @_;
my $item_rs = $c->model('DB')->resultset('malicious_calls');
if($c->user->roles eq "admin") {
} elsif($c->user->roles eq "reseller") {
$item_rs = $item_rs->search({
'reseller.id' => $c->user->reseller_id,
},{
join => { 'subscriber' => { 'contract' => { 'contact' => 'reseller' } } },
});
}
return $item_rs;
}
sub get_form {
my ($self, $c) = @_;
if($c->user->roles eq "admin") {
return NGCP::Panel::Form::MaliciousCall::Admin->new;
} elsif($c->user->roles eq "reseller") {
return NGCP::Panel::Form::MaliciousCall::Reseller->new;
}
}
sub hal_from_item {
my ($self, $c, $item, $form) = @_;
my %resource = $item->get_inflated_columns;
my $hal = Data::HAL->new(
links => [
Data::HAL::Link->new(
relation => 'curies',
href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}',
name => 'ngcp',
templated => true,
),
Data::HAL::Link->new(relation => 'collection', href => sprintf("/api/%s/", $self->resource_name)),
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $item->id)),
],
relation => 'ngcp:'.$self->resource_name,
);
$form //= $self->get_form($c);
$self->validate_form(
c => $c,
resource => \%resource,
form => $form,
run => 0,
exceptions => [qw/call_id/],
);
$resource{id} = int($item->id);
$hal->resource({%resource});
return $hal;
}
sub item_by_id {
my ($self, $c, $id) = @_;
my $item_rs = $self->item_rs($c);
return $item_rs->find($id);
}
1;
# vim: set tabstop=4 expandtab:

@ -35,6 +35,8 @@ log4perl.appender.Default.layout.ConversionPattern=%d{ISO8601} [%p] [%F +%L] %m{
callingcard 1
voucher 1
malicious_call 1
</features>
<faxserver>

@ -0,0 +1,23 @@
[% site_config.title = c.loc('Malicious Calls') -%]
[%
helper.name = c.loc('Malicious Call');
helper.identifier = 'malicious_call';
helper.messages = messages;
helper.dt_columns = mcid_dt_columns;
helper.length_change = 1;
helper.close_target = close_target;
helper.create_flag = create_flag;
helper.edit_flag = edit_flag;
helper.form_object = form;
helper.ajax_uri = c.uri_for( c.controller.action_for('ajax') );
UNLESS c.user.read_only;
helper.dt_buttons = [
{ name = c.loc('Delete'), uri = "/maliciouscall/'+full.id+'/delete", class = 'btn-small btn-secondary', icon = 'icon-trash' },
];
END;
PROCESS 'helpers/datatables.tt';
-%]
[% # vim: set tabstop=4 syntax=html expandtab: -%]
Loading…
Cancel
Save