From e15e4042322262c88019505b05c89bcc801cf871 Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Tue, 5 Feb 2019 16:16:57 +0100 Subject: [PATCH] TT#51109 Enable api soundgroups and soundhandles for subscriberadmin Change-Id: Ie9f20f7c6f06ee87860762b57a5ae41f55c00238 --- lib/NGCP/Panel/Controller/API/SoundGroups.pm | 4 +- .../Panel/Controller/API/SoundGroupsItem.pm | 4 +- lib/NGCP/Panel/Controller/API/SoundHandles.pm | 71 ++----------------- .../Panel/Controller/API/SoundHandlesItem.pm | 45 +----------- lib/NGCP/Panel/Role/API/SoundHandles.pm | 51 +++---------- 5 files changed, 22 insertions(+), 153 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/SoundGroups.pm b/lib/NGCP/Panel/Controller/API/SoundGroups.pm index 3bc1b7950a..8bde218eae 100644 --- a/lib/NGCP/Panel/Controller/API/SoundGroups.pm +++ b/lib/NGCP/Panel/Controller/API/SoundGroups.pm @@ -7,7 +7,9 @@ use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::SoundGroups/; use HTTP::Status qw(:constants); use NGCP::Panel::Utils::Security; -__PACKAGE__->set_config(); +__PACKAGE__->set_config({ + allowed_roles => [qw/admin reseller subscriberadmin/], +}); sub allowed_methods { return [qw/GET OPTIONS HEAD/]; diff --git a/lib/NGCP/Panel/Controller/API/SoundGroupsItem.pm b/lib/NGCP/Panel/Controller/API/SoundGroupsItem.pm index 75830514ae..4313d86a43 100644 --- a/lib/NGCP/Panel/Controller/API/SoundGroupsItem.pm +++ b/lib/NGCP/Panel/Controller/API/SoundGroupsItem.pm @@ -5,7 +5,9 @@ use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::SoundGroup use HTTP::Status qw(:constants); -__PACKAGE__->set_config(); +__PACKAGE__->set_config({ + allowed_roles => [qw/admin reseller subscriberadmin/], +}); sub allowed_methods{ return [qw/GET OPTIONS HEAD/]; diff --git a/lib/NGCP/Panel/Controller/API/SoundHandles.pm b/lib/NGCP/Panel/Controller/API/SoundHandles.pm index 336f33d656..711cc487a6 100644 --- a/lib/NGCP/Panel/Controller/API/SoundHandles.pm +++ b/lib/NGCP/Panel/Controller/API/SoundHandles.pm @@ -1,14 +1,14 @@ package NGCP::Panel::Controller::API::SoundHandles; use NGCP::Panel::Utils::Generic qw(:all); - use Sipwise::Base; - -use boolean qw(true); -use Data::HAL qw(); -use Data::HAL::Link qw(); use HTTP::Headers qw(); use HTTP::Status qw(:constants); +use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::SoundHandles/; + +__PACKAGE__->set_config({ + allowed_roles => [qw/admin reseller subscriberadmin/], +}); sub allowed_methods{ return [qw/GET OPTIONS HEAD/]; @@ -35,67 +35,6 @@ sub query_params { }, ]; } - -use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::SoundHandles/; - -sub resource_name{ - return 'soundhandles'; -} - -sub dispatch_path{ - return '/api/soundhandles/'; -} - -sub relation{ - return 'http://purl.org/sipwise/ngcp-api/#rel-soundhandles'; -} - -__PACKAGE__->set_config({ - allowed_roles => [qw/admin reseller/], -}); - -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, my $items_rows) = $self->paginate_order_collection($c, $items); - my (@embedded, @links); - my $form = $self->get_form($c); - for my $item (@$items_rows) { - 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/'), - $self->collection_nav_links($c, $page, $rows, $total_count, $c->request->path, $c->request->query_params); - - 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; -} - 1; # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/SoundHandlesItem.pm b/lib/NGCP/Panel/Controller/API/SoundHandlesItem.pm index 53b9f23ea8..f2ce68df61 100644 --- a/lib/NGCP/Panel/Controller/API/SoundHandlesItem.pm +++ b/lib/NGCP/Panel/Controller/API/SoundHandlesItem.pm @@ -6,53 +6,14 @@ use Sipwise::Base; use HTTP::Headers qw(); use HTTP::Status qw(:constants); -use NGCP::Panel::Utils::ValidateJSON qw(); -require Catalyst::ActionRole::ACL; -require NGCP::Panel::Role::HTTPMethods; -require Catalyst::ActionRole::RequireSSL; - -sub allowed_methods{ - return [qw/GET OPTIONS HEAD/]; -} - use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::SoundHandles/; -sub resource_name{ - return 'soundhandles'; -} - -sub dispatch_path{ - return '/api/soundhandles/'; -} - -sub relation{ - return 'http://purl.org/sipwise/ngcp-api/#rel-soundhandles'; -} - __PACKAGE__->set_config({ - allowed_roles => [qw/admin reseller/], + allowed_roles => [qw/admin reseller subscriberadmin/], }); -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, soundhandle => $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"|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 allowed_methods{ + return [qw/GET OPTIONS HEAD/]; } 1; diff --git a/lib/NGCP/Panel/Role/API/SoundHandles.pm b/lib/NGCP/Panel/Role/API/SoundHandles.pm index 1f7bec83bc..fd3a21701f 100644 --- a/lib/NGCP/Panel/Role/API/SoundHandles.pm +++ b/lib/NGCP/Panel/Role/API/SoundHandles.pm @@ -1,16 +1,19 @@ package NGCP::Panel::Role::API::SoundHandles; use NGCP::Panel::Utils::Generic qw(:all); - use Sipwise::Base; use parent 'NGCP::Panel::Role::API'; - -use boolean qw(true); -use Data::HAL qw(); -use Data::HAL::Link qw(); use HTTP::Status qw(:constants); +sub item_name{ + return 'soundhandle'; +} + +sub resource_name{ + return 'soundhandles'; +} + sub _item_rs { my ($self, $c) = @_; @@ -27,44 +30,6 @@ sub get_form { return NGCP::Panel::Form::get("NGCP::Panel::Form::Sound::HandleAPI", $c); } -sub item_by_id { - my ($self, $c, $id) = @_; - my $item_rs = $self->item_rs($c); - return $item_rs->find($id); -} - -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, - ); - - $resource{id} = int($item->id); - $hal->resource({%resource}); - return $hal; -} 1; # vim: set tabstop=4 expandtab: