parent
2f7d097f4c
commit
8f5c6d0907
@ -0,0 +1,169 @@
|
||||
package NGCP::Panel::Controller::API::FaxserverSettings;
|
||||
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 =>
|
||||
'Specifies faxserver settings for a specific subscriber.',
|
||||
);
|
||||
|
||||
class_has 'query_params' => (
|
||||
is => 'ro',
|
||||
isa => 'ArrayRef',
|
||||
default => sub {[
|
||||
{
|
||||
param => 'name_or_password',
|
||||
description => 'Filter for items (subscribers) where name or password field match given pattern',
|
||||
query => {
|
||||
first => sub {
|
||||
my $q = shift;
|
||||
return { '-or' => [
|
||||
{ 'voip_fax_preference.name' => { like => $q } },
|
||||
{ 'voip_fax_preference.password' => { like => $q } },
|
||||
] };
|
||||
},
|
||||
second => sub {
|
||||
return { prefetch => { 'provisioning_voip_subscriber' => 'voip_fax_preference' } };
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
param => 'active',
|
||||
description => 'Filter for items (subscribers) with active faxserver settings',
|
||||
query => {
|
||||
first => sub {
|
||||
my $q = shift;
|
||||
if ($q) {
|
||||
{ 'voip_fax_preference.active' => 1 };
|
||||
} else {
|
||||
{};
|
||||
}
|
||||
},
|
||||
second => sub {
|
||||
{ prefetch => { 'provisioning_voip_subscriber' => 'voip_fax_preference' } };
|
||||
},
|
||||
},
|
||||
},
|
||||
]},
|
||||
);
|
||||
|
||||
with 'NGCP::Panel::Role::API::FaxserverSettings';
|
||||
|
||||
class_has('resource_name', is => 'ro', default => 'faxserversettings');
|
||||
class_has('dispatch_path', is => 'ro', default => '/api/faxserversettings/');
|
||||
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-faxserversettings');
|
||||
|
||||
__PACKAGE__->config(
|
||||
action => {
|
||||
map { $_ => {
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => 'admin',
|
||||
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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub GET :Allow {
|
||||
my ($self, $c) = @_;
|
||||
my $page = $c->request->params->{page} // 1;
|
||||
my $rows = $c->request->params->{rows} // 10;
|
||||
{
|
||||
my $cfs = $self->item_rs($c);
|
||||
(my $total_count, $cfs) = $self->paginate_order_collection($c, $cfs);
|
||||
my (@embedded, @links);
|
||||
for my $cf ($cfs->all) {
|
||||
try {
|
||||
push @embedded, $self->hal_from_item($c, $cf);
|
||||
push @links, Data::HAL::Link->new(
|
||||
relation => 'ngcp:'.$self->resource_name,
|
||||
href => sprintf('%s%s', $self->dispatch_path, $cf->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', $self->dispatch_path, $page, $rows));
|
||||
if(($total_count / $rows) > $page ) {
|
||||
push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('%s?page=%d&rows=%d', $self->dispatch_path, $page + 1, $rows));
|
||||
}
|
||||
if($page > 1) {
|
||||
push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('%s?page=%d&rows=%d', $self->dispatch_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;
|
||||
$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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
||||
@ -0,0 +1,179 @@
|
||||
package NGCP::Panel::Controller::API::FaxserverSettingsItem;
|
||||
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::ValidateJSON qw();
|
||||
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::HTTPMethods;
|
||||
require Catalyst::ActionRole::RequireSSL;
|
||||
|
||||
with 'NGCP::Panel::Role::API::FaxserverSettings';
|
||||
|
||||
class_has('resource_name', is => 'ro', default => 'faxserversettings');
|
||||
class_has('dispatch_path', is => 'ro', default => '/api/faxserversettings/');
|
||||
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-faxserversettings');
|
||||
|
||||
__PACKAGE__->config(
|
||||
action => {
|
||||
map { $_ => {
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => 'admin',
|
||||
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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub GET :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
{
|
||||
last unless $self->valid_id($c, $id);
|
||||
my $cfm = $self->item_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, cfm => $cfm);
|
||||
|
||||
my $hal = $self->hal_from_item($c, $cfm);
|
||||
|
||||
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"|r
|
||||
=~ s/rel=self/rel="item self"/r;
|
||||
} $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;
|
||||
$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 PATCH :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $guard = $c->model('DB')->txn_scope_guard;
|
||||
{
|
||||
my $preference = $self->require_preference($c);
|
||||
last unless $preference;
|
||||
|
||||
my $json = $self->get_valid_patch_data(
|
||||
c => $c,
|
||||
id => $id,
|
||||
media_type => 'application/json-patch+json',
|
||||
ops => [qw/add replace remove copy/],
|
||||
);
|
||||
last unless $json;
|
||||
|
||||
my $item = $self->item_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, faxserversettings => $item);
|
||||
my $old_resource = $self->hal_from_item($c, $item)->resource;
|
||||
my $resource = $self->apply_patch($c, $old_resource, $json);
|
||||
last unless $resource;
|
||||
|
||||
my $form = $self->get_form($c);
|
||||
$item = $self->update_item($c, $item, $old_resource, $resource, $form);
|
||||
last unless $item;
|
||||
|
||||
$guard->commit;
|
||||
|
||||
if ('minimal' eq $preference) {
|
||||
$c->response->status(HTTP_NO_CONTENT);
|
||||
$c->response->header(Preference_Applied => 'return=minimal');
|
||||
$c->response->body(q());
|
||||
} else {
|
||||
my $hal = $self->hal_from_item($c, $item);
|
||||
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
|
||||
$hal->http_headers,
|
||||
), $hal->as_json);
|
||||
$c->response->headers($response->headers);
|
||||
$c->response->header(Preference_Applied => 'return=representation');
|
||||
$c->response->body($response->content);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
sub PUT :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $guard = $c->model('DB')->txn_scope_guard;
|
||||
{
|
||||
my $preference = $self->require_preference($c);
|
||||
last unless $preference;
|
||||
|
||||
my $item = $self->item_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, faxserversettings => $item);
|
||||
my $resource = $self->get_valid_put_data(
|
||||
c => $c,
|
||||
id => $id,
|
||||
media_type => 'application/json',
|
||||
);
|
||||
last unless $resource;
|
||||
my $old_resource = undef;
|
||||
|
||||
my $form = $self->get_form($c);
|
||||
$item = $self->update_item($c, $item, $old_resource, $resource, $form);
|
||||
last unless $item;
|
||||
|
||||
$guard->commit;
|
||||
|
||||
if ('minimal' eq $preference) {
|
||||
$c->response->status(HTTP_NO_CONTENT);
|
||||
$c->response->header(Preference_Applied => 'return=minimal');
|
||||
$c->response->body(q());
|
||||
} else {
|
||||
my $hal = $self->hal_from_item($c, $item);
|
||||
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
|
||||
$hal->http_headers,
|
||||
), $hal->as_json);
|
||||
$c->response->headers($response->headers);
|
||||
$c->response->header(Preference_Applied => 'return=representation');
|
||||
$c->response->body($response->content);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
sub end : Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->log_response($c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
||||
@ -0,0 +1,106 @@
|
||||
package NGCP::Panel::Form::Faxserver::API;
|
||||
|
||||
use HTML::FormHandler::Moose;
|
||||
extends 'HTML::FormHandler';
|
||||
use Moose::Util::TypeConstraints;
|
||||
|
||||
|
||||
has_field 'name' => (
|
||||
type => 'Text',
|
||||
label => 'Name in Fax Header',
|
||||
required => 0,
|
||||
);
|
||||
|
||||
|
||||
has_field 'password' => (
|
||||
type => 'Text',
|
||||
label => 'Password',
|
||||
required => 0,
|
||||
);
|
||||
|
||||
sub validate_password {
|
||||
my ($self, $field) = @_;
|
||||
my $c = $self->form->ctx;
|
||||
return unless $c;
|
||||
|
||||
NGCP::Panel::Utils::Form::validate_password(c => $c, field => $field);
|
||||
return;
|
||||
}
|
||||
|
||||
has_field 'active' => (
|
||||
type => 'Boolean',
|
||||
label => 'Active',
|
||||
required => 0,
|
||||
);
|
||||
|
||||
|
||||
has_field 'send_copy' => (
|
||||
type => 'Boolean',
|
||||
label => 'Send Copies',
|
||||
required => 0,
|
||||
);
|
||||
|
||||
|
||||
|
||||
has_field 'send_status' => (
|
||||
type => 'Boolean',
|
||||
label => 'Send Reports',
|
||||
required => 0,
|
||||
);
|
||||
|
||||
|
||||
has_field 'destinations' => (
|
||||
type => 'Repeatable',
|
||||
setup_for_js => 1,
|
||||
do_wrapper => 1,
|
||||
do_label => 0,
|
||||
tags => {
|
||||
controls_div => 1,
|
||||
},
|
||||
);
|
||||
|
||||
has_field 'destinations.destination' => (
|
||||
type => 'Text',
|
||||
label => 'Destination',
|
||||
required => 1,
|
||||
);
|
||||
|
||||
has_field 'destinations.filetype' => (
|
||||
type => 'Select',
|
||||
options => [
|
||||
{ label => 'TIFF', value => 'TIFF' },
|
||||
{ label => 'PS', value => 'PS' },
|
||||
{ label => 'PDF', value => 'PDF' },
|
||||
{ label => 'PDF14', value => 'PDF14' },
|
||||
],
|
||||
label => 'File Type',
|
||||
required => 1,
|
||||
);
|
||||
|
||||
has_field 'destinations.cc' => (
|
||||
type => 'Boolean',
|
||||
label => 'Incoming Email as CC',
|
||||
default => 0,
|
||||
);
|
||||
|
||||
has_field 'destinations.incoming' => (
|
||||
type => 'Boolean',
|
||||
label => 'Deliver Incoming Faxes',
|
||||
default => 1,
|
||||
);
|
||||
|
||||
has_field 'destinations.outgoing' => (
|
||||
type => 'Boolean',
|
||||
label => 'Deliver Outgoing Faxes',
|
||||
default => 1,
|
||||
);
|
||||
|
||||
has_field 'destinations.status' => (
|
||||
type => 'Boolean',
|
||||
label => 'Receive Reports',
|
||||
default => 1,
|
||||
);
|
||||
|
||||
1;
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
||||
@ -0,0 +1,158 @@
|
||||
package NGCP::Panel::Role::API::FaxserverSettings;
|
||||
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 JSON::Types;
|
||||
use NGCP::Panel::Form::Faxserver::API;
|
||||
use NGCP::Panel::Utils::Subscriber;
|
||||
|
||||
sub get_form {
|
||||
my ($self, $c, $type) = @_;
|
||||
|
||||
return NGCP::Panel::Form::Faxserver::API->new(ctx => $c);
|
||||
}
|
||||
|
||||
sub hal_from_item {
|
||||
my ($self, $c, $item) = @_;
|
||||
my $form;
|
||||
my $rwr_form = $self->get_form($c);
|
||||
my $type = 'faxserversettings';
|
||||
|
||||
my $prov_subs = $item->provisioning_voip_subscriber;
|
||||
|
||||
die "no provisioning_voip_subscriber" unless $prov_subs;
|
||||
|
||||
my $fax_preference = $prov_subs->voip_fax_preference;
|
||||
unless ($fax_preference) {
|
||||
try {
|
||||
$fax_preference = $prov_subs->create_related('voip_fax_preference', {});
|
||||
$fax_preference->discard_changes; # reload
|
||||
} catch($e) {
|
||||
$c->log->error("Error creating empty fax_preference on get");
|
||||
};
|
||||
}
|
||||
|
||||
my %resource = (
|
||||
$fax_preference ? $fax_preference->get_inflated_columns : (),
|
||||
subscriber_id => $item->id,
|
||||
);
|
||||
delete $resource{id};
|
||||
my @destinations;
|
||||
for my $dest ($prov_subs->voip_fax_destinations->all) {
|
||||
push @destinations, {$dest->get_inflated_columns};
|
||||
}
|
||||
$resource{destinations} = \@destinations;
|
||||
|
||||
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("%s", $self->dispatch_path)),
|
||||
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
|
||||
Data::HAL::Link->new(relation => 'self', href => sprintf("%s%s", $self->dispatch_path, $item->id)),
|
||||
Data::HAL::Link->new(relation => "ngcp:$type", href => sprintf("/api/%s/%s", $type, $item->id)),
|
||||
Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $item->id)),
|
||||
],
|
||||
relation => 'ngcp:'.$self->resource_name,
|
||||
);
|
||||
|
||||
|
||||
$form //= $self->get_form($c);
|
||||
return unless $self->validate_form(
|
||||
c => $c,
|
||||
form => $form,
|
||||
resource => \%resource,
|
||||
run => 0,
|
||||
);
|
||||
|
||||
$hal->resource(\%resource);
|
||||
return $hal;
|
||||
}
|
||||
|
||||
sub item_rs {
|
||||
my ($self, $c) = @_;
|
||||
my $item_rs;
|
||||
|
||||
$item_rs = $c->model('DB')->resultset('voip_subscribers')
|
||||
->search(
|
||||
{ status => { '!=' => 'terminated' } },
|
||||
{ prefetch => 'provisioning_voip_subscriber',},
|
||||
);
|
||||
if($c->user->roles eq "reseller") {
|
||||
$item_rs = $item_rs->search({
|
||||
'contact.reseller_id' => $c->user->reseller_id,
|
||||
}, {
|
||||
join => { 'contract' => 'contact' },
|
||||
});
|
||||
}
|
||||
|
||||
return $item_rs;
|
||||
}
|
||||
|
||||
sub item_by_id {
|
||||
my ($self, $c, $id) = @_;
|
||||
|
||||
return $self->item_rs($c)->search_rs({'me.id' => $id})->first;
|
||||
}
|
||||
|
||||
sub update_item {
|
||||
my ($self, $c, $item, $old_resource, $resource, $form) = @_;
|
||||
|
||||
delete $resource->{id};
|
||||
my $billing_subscriber_id = $item->id;
|
||||
my $prov_subs = $item->provisioning_voip_subscriber;
|
||||
die "need provisioning_voip_subscriber" unless $prov_subs;
|
||||
my $prov_subscriber_id = $prov_subs->id;
|
||||
my $destinations_rs = $prov_subs->voip_fax_destinations;
|
||||
|
||||
return unless $self->validate_form(
|
||||
c => $c,
|
||||
form => $form,
|
||||
resource => $resource,
|
||||
run => 1,
|
||||
);
|
||||
|
||||
if (! exists $resource->{destinations} ) {
|
||||
$resource->{destinations} = [];
|
||||
}
|
||||
if (ref $resource->{destinations} ne "ARRAY") {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid field 'destinations'. Must be an array.");
|
||||
return;
|
||||
}
|
||||
|
||||
my %update_fields = %{ $resource };
|
||||
delete $update_fields{destinations};
|
||||
|
||||
try {
|
||||
$prov_subs->delete_related('voip_fax_preference');
|
||||
$destinations_rs->delete;
|
||||
$prov_subs->create_related('voip_fax_preference', \%update_fields);
|
||||
$prov_subs->discard_changes; #reload
|
||||
|
||||
for my $dest (@{ $resource->{destinations} }) {
|
||||
$destinations_rs->create($dest);
|
||||
}
|
||||
} catch($e) {
|
||||
$c->log->error("Error Updating faxserversettings: $e");
|
||||
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "faxserversettings could not be updated.");
|
||||
return;
|
||||
};
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
1;
|
||||
# vim: set tabstop=4 expandtab:
|
||||
Loading…
Reference in new issue