parent
139dabf562
commit
d71f12aff9
@ -0,0 +1,141 @@
|
||||
package NGCP::Panel::Controller::API::SpeedDials;
|
||||
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 NGCP::Panel::Utils::Subscriber;
|
||||
use NGCP::Panel::Utils::Preferences;
|
||||
use Path::Tiny qw(path);
|
||||
use Safe::Isa qw($_isa);
|
||||
use UUID;
|
||||
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 =>
|
||||
'Show a collection of speeddials, belonging to a specific subscriber.',
|
||||
);
|
||||
|
||||
class_has 'query_params' => (
|
||||
is => 'ro',
|
||||
isa => 'ArrayRef',
|
||||
default => sub {[
|
||||
]},
|
||||
);
|
||||
|
||||
with 'NGCP::Panel::Role::API::SpeedDials';
|
||||
|
||||
class_has('resource_name', is => 'ro', default => 'speeddials');
|
||||
class_has('dispatch_path', is => 'ro', default => '/api/speeddials/');
|
||||
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-speeddials');
|
||||
|
||||
__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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub GET :Allow {
|
||||
my ($self, $c) = @_;
|
||||
my $page = $c->request->params->{page} // 1;
|
||||
my $rows = $c->request->params->{rows} // 10;
|
||||
{
|
||||
my $subscribers = $self->item_rs($c);
|
||||
my $total_count = int($subscribers->count);
|
||||
$subscribers = $subscribers->search(undef, {
|
||||
page => $page,
|
||||
rows => $rows,
|
||||
});
|
||||
my (@embedded, @links);
|
||||
for my $subscriber ($subscribers->search({}, {order_by => {-asc => 'me.id'}})->all) {
|
||||
push @embedded, $self->hal_from_item($c, $subscriber);
|
||||
push @links, Data::HAL::Link->new(
|
||||
relation => 'ngcp:'.$self->resource_name,
|
||||
href => sprintf('%s%d', $self->dispatch_path, $subscriber->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', $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;
|
||||
$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,191 @@
|
||||
package NGCP::Panel::Controller::API::SpeedDialsItem;
|
||||
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::SpeedDials';
|
||||
|
||||
class_has('resource_name', is => 'ro', default => 'speeddials');
|
||||
class_has('dispatch_path', is => 'ro', default => '/api/speeddials/');
|
||||
class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-speeddials');
|
||||
|
||||
__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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub GET :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
{
|
||||
last unless $self->valid_id($c, $id);
|
||||
my $subscriber = $self->item_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, subscriber => $subscriber);
|
||||
|
||||
my $hal = $self->hal_from_item($c, $subscriber);
|
||||
|
||||
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 PUT :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $schema = $c->model('DB');
|
||||
my $guard = $schema->txn_scope_guard;
|
||||
{
|
||||
my $preference = $self->require_preference($c);
|
||||
last unless $preference;
|
||||
|
||||
my $subscriber = $self->item_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, subscriber => $subscriber);
|
||||
my $resource = $self->get_valid_put_data(
|
||||
c => $c,
|
||||
id => $id,
|
||||
media_type => 'application/json',
|
||||
);
|
||||
last unless $resource;
|
||||
my $update = 1;
|
||||
my $r = $self->prepare_resource($c, $schema, $resource, $update);
|
||||
last unless $r;
|
||||
$resource = $r->{resource};
|
||||
|
||||
my $form = $self->get_form($c);
|
||||
$subscriber = $self->update_item($c, $subscriber, $r, $resource, $form);
|
||||
last unless $subscriber;
|
||||
|
||||
$guard->commit;
|
||||
|
||||
if ('minimal' eq $preference) {
|
||||
$c->response->status(HTTP_NO_CONTENT);
|
||||
$c->response->header(Preference_Applied => 'return=minimal');
|
||||
$c->response->body(q());
|
||||
} else {
|
||||
$resource = $self->transform_resource($c, $subscriber, $form);
|
||||
my $hal = $self->hal_from_item($c, $subscriber, $resource, $form);
|
||||
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 PATCH :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $schema = $c->model('DB');
|
||||
my $guard = $schema->txn_scope_guard;
|
||||
{
|
||||
my $preference = $self->require_preference($c);
|
||||
last unless $preference;
|
||||
|
||||
my $subscriber = $self->item_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, subscriber => $subscriber);
|
||||
my $json = $self->get_valid_patch_data(
|
||||
c => $c,
|
||||
id => $id,
|
||||
media_type => 'application/json-patch+json',
|
||||
ops => ["add", "replace", "copy", "remove"],
|
||||
);
|
||||
last unless $json;
|
||||
|
||||
my $form = $self->get_form($c);
|
||||
my $old_resource = $self->transform_resource($c, $subscriber, $form);
|
||||
my $resource = $self->apply_patch($c, $old_resource, $json);
|
||||
last unless $resource;
|
||||
|
||||
my $update = 1;
|
||||
my $r = $self->prepare_resource($c, $schema, $resource, $update);
|
||||
last unless $r;
|
||||
$resource = $r->{resource};
|
||||
|
||||
$subscriber = $self->update_item($c, $subscriber, $r, $resource, $form);
|
||||
last unless $subscriber;
|
||||
|
||||
$guard->commit;
|
||||
|
||||
if ('minimal' eq $preference) {
|
||||
$c->response->status(HTTP_NO_CONTENT);
|
||||
$c->response->header(Preference_Applied => 'return=minimal');
|
||||
$c->response->body(q());
|
||||
} else {
|
||||
$resource = $self->transform_resource($c, $subscriber, $form);
|
||||
my $hal = $self->hal_from_item($c, $subscriber, $resource, $form);
|
||||
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,165 @@
|
||||
package NGCP::Panel::Role::API::SpeedDials;
|
||||
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 Test::More;
|
||||
use NGCP::Panel::Form::Subscriber::SubscriberAPI;
|
||||
use NGCP::Panel::Utils::XMLDispatcher;
|
||||
use NGCP::Panel::Utils::Prosody;
|
||||
use NGCP::Panel::Utils::Subscriber;
|
||||
|
||||
sub get_form {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
return '';# NGCP::Panel::Form::Subscriber::SubscriberAPI->new;
|
||||
}
|
||||
|
||||
sub hal_from_item {
|
||||
my ($self, $c, $item) = @_;
|
||||
|
||||
my $p_subs = $item->provisioning_voip_subscriber;
|
||||
my $resource = { subscriber_id => $item->id, speeddials => $self->speeddials_from_subscriber($p_subs) };
|
||||
|
||||
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)),
|
||||
Data::HAL::Link->new(relation => 'ngcp:speeddials', href => sprintf("/api/speeddials/%d", $item->id)),
|
||||
Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $item->id)),
|
||||
],
|
||||
relation => 'ngcp:'.$self->resource_name,
|
||||
);
|
||||
|
||||
$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' } });
|
||||
if($c->user->roles eq "admin") {
|
||||
} elsif($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) = @_;
|
||||
|
||||
my $item_rs = $self->item_rs($c);
|
||||
return $item_rs->find($id);
|
||||
}
|
||||
|
||||
sub update_item {
|
||||
my ($self, $c, $item, $full_resource, $resource, $form) = @_;
|
||||
|
||||
my $subscriber = $item;
|
||||
my $customer = $full_resource->{customer};
|
||||
my $admin = $full_resource->{admin};
|
||||
my $alias_numbers = $full_resource->{alias_numbers};
|
||||
my $preferences = $full_resource->{preferences};
|
||||
|
||||
if($subscriber->status ne $resource->{status}) {
|
||||
if($resource->{status} eq 'locked') {
|
||||
$resource->{lock} = 4;
|
||||
} elsif($subscriber->status eq 'locked' && $resource->{status} eq 'active') {
|
||||
$resource->{lock} ||= 0;
|
||||
} elsif($resource->{status} eq 'terminated') {
|
||||
try {
|
||||
NGCP::Panel::Utils::Subscriber::terminate(c => $c, subscriber => $subscriber);
|
||||
} catch($e) {
|
||||
$c->log->error("failed to terminate subscriber id ".$subscriber->id);
|
||||
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to terminate subscriber");
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(defined $resource->{lock}) {
|
||||
try {
|
||||
NGCP::Panel::Utils::Subscriber::lock_provisoning_voip_subscriber(
|
||||
c => $c,
|
||||
prov_subscriber => $subscriber->provisioning_voip_subscriber,
|
||||
level => $resource->{lock},
|
||||
);
|
||||
} catch($e) {
|
||||
$c->log->error("failed to lock subscriber id ".$subscriber->id." with level ".$resource->{lock});
|
||||
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to update subscriber lock");
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
NGCP::Panel::Utils::Subscriber::update_subscriber_numbers(
|
||||
schema => $c->model('DB'),
|
||||
primary_number => $resource->{e164},
|
||||
alias_numbers => $alias_numbers,
|
||||
reseller_id => $customer->contact->reseller_id,
|
||||
subscriber_id => $subscriber->id,
|
||||
);
|
||||
|
||||
my $billing_res = {
|
||||
external_id => $resource->{external_id},
|
||||
status => $resource->{status},
|
||||
};
|
||||
my $provisioning_res = {
|
||||
password => $resource->{password},
|
||||
webusername => $resource->{webusername},
|
||||
webpassword => $resource->{webpassword},
|
||||
admin => $resource->{administrative},
|
||||
is_pbx_group => $resource->{is_pbx_group},
|
||||
pbx_group_id => $resource->{pbx_group_id},
|
||||
modify_timestamp => NGCP::Panel::Utils::DateTime::current_local,
|
||||
|
||||
};
|
||||
|
||||
$subscriber->update($billing_res);
|
||||
$subscriber->provisioning_voip_subscriber->update($provisioning_res);
|
||||
$subscriber->discard_changes;
|
||||
NGCP::Panel::Utils::Subscriber::update_preferences(
|
||||
c => $c,
|
||||
prov_subscriber => $subscriber->provisioning_voip_subscriber,
|
||||
preferences => $preferences,
|
||||
);
|
||||
|
||||
# TODO: status handling (termination, ...)
|
||||
|
||||
return $subscriber;
|
||||
}
|
||||
|
||||
sub speeddials_from_subscriber {
|
||||
my ($self, $prov_subscriber) = @_;
|
||||
|
||||
my @speeddials;
|
||||
for my $s ($prov_subscriber->voip_speed_dials->all) {
|
||||
push @speeddials, {slot => $s->slot, destination => $s->destination};
|
||||
}
|
||||
return \@speeddials;
|
||||
}
|
||||
|
||||
1;
|
||||
# vim: set tabstop=4 expandtab:
|
||||
Loading…
Reference in new issue