diff --git a/lib/NGCP/Panel/Controller/API/ResellerBrandingLogos.pm b/lib/NGCP/Panel/Controller/API/ResellerBrandingLogos.pm index 5753ca27e6..b192ceda34 100644 --- a/lib/NGCP/Panel/Controller/API/ResellerBrandingLogos.pm +++ b/lib/NGCP/Panel/Controller/API/ResellerBrandingLogos.pm @@ -7,42 +7,66 @@ use HTTP::Status qw(:constants); use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::ResellerBrandingLogos/; -__PACKAGE__->set_config(); +__PACKAGE__->set_config({ + log_response => 0, + allowed_roles => [qw/admin reseller subscriberadmin/], +}); -sub config_allowed_roles { - return [qw/admin reseller subscriberadmin subscriber/]; -} - -sub allowed_methods{ +sub allowed_methods { return [qw/GET OPTIONS HEAD/]; } sub api_description { - return 'Used to download the reseller branding logo image. Returns a binary attachment with the correct content type (e.g. image/jpeg) of the image.'; + return 'Download the reseller branding logo image. Returns a binary data with the correct content-type (e.g. image/jpeg) of the image.'; }; +sub query_params { + return [ + { + param => 'subscriber_id', + description => 'Filter for logos that belong to the reseller of the subscriber', + new_rs => sub { + my ($c, $q, $rs) = @_; + return $rs->search_rs({ + 'voip_subscribers.id' => $c->req->param('subscriber_id') + },{ + join => { 'reseller' => { 'contacts' => { 'contracts' => 'voip_subscribers' } } } + }); + }, + }, + { + param => 'reseller_id', + description => 'Filter for logos that belong to the reseller', + query => { + first => sub { + my $q = shift; + { 'me.reseller_id' => $q }; + }, + second => sub {}, + }, + }, + ]; +} + sub GET :Allow { my ($self, $c) = @_; - my $item = $self->item_rs($c); - unless($c->req->param('subscriber_id')) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "subscriber_id parameter is mandatory."); - return; + if ($c->user->roles eq 'admin') { + if (!$c->req->param('subscriber_id') && !$c->req->param('reseller_id')) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "'reseller_id' or 'subscriber_id' parameter is mandatory."); + return; + } } - unless($item->first) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid subscriber_id. Subscriber not found."); - return; - } + my $item = $self->item_rs($c)->first; - my $branding = $item->first->branding; - - if(!$branding || !$branding->logo) { - $self->error($c, HTTP_NOT_FOUND, "No branding logo available for this reseller"); + unless ($item && $item->logo && $item->logo_image_type) { + $self->error($c, HTTP_NOT_FOUND, "ResellerBrandingLogo is not found or does not have image/image_type"); return; } - $c->response->content_type($branding->logo_image_type); - $c->response->body($branding->logo); + + $c->response->content_type($item->logo_image_type); + $c->response->body($item->logo); } 1; diff --git a/lib/NGCP/Panel/Controller/API/ResellerBrandingLogosItem.pm b/lib/NGCP/Panel/Controller/API/ResellerBrandingLogosItem.pm index 460ff1b4fa..69a63301a9 100644 --- a/lib/NGCP/Panel/Controller/API/ResellerBrandingLogosItem.pm +++ b/lib/NGCP/Panel/Controller/API/ResellerBrandingLogosItem.pm @@ -8,33 +8,26 @@ use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::ResellerBr use HTTP::Status qw(:constants); __PACKAGE__->set_config({ - GET => { - ReturnContentType => 'binary', - }, log_response => 0, - allowed_roles => [qw/admin reseller subscriberadmin subscriber/], + allowed_roles => [qw/admin reseller subscriberadmin/], }); -sub allowed_methods{ +sub allowed_methods { return [qw/GET OPTIONS HEAD/]; } -sub get_item_binary_data{ - my($self, $c, $id, $item) = @_; +sub GET :Allow { + my($self, $c, $id) = @_; - my $branding = $item->branding; + my $item = $self->item_by_id($c, $id); - if ($branding && $branding->logo) { - my $data = $branding->logo; - my $mime_type = $branding->logo_image_type; - my $fext = $mime_type; - $fext =~ s/^.*?([a-zA-Z0-9]+)$/$1/; - return (\$data, $mime_type, 'reseller_'.$item->id.'_branding_logo.'.$fext); - } - else{ - $self->error($c, HTTP_NOT_FOUND, "No branding logo available for this reseller"); + unless ($item && $item->logo && $item->logo_image_type) { + $self->error($c, HTTP_NOT_FOUND, "ResellerBrandingLogo is not found or does not have image/image_type"); return; } + + $c->response->content_type($item->logo_image_type); + $c->response->body($item->logo); } 1; diff --git a/lib/NGCP/Panel/Role/API/ResellerBrandingLogos.pm b/lib/NGCP/Panel/Role/API/ResellerBrandingLogos.pm index d2bd980012..92f733c6dd 100644 --- a/lib/NGCP/Panel/Role/API/ResellerBrandingLogos.pm +++ b/lib/NGCP/Panel/Role/API/ResellerBrandingLogos.pm @@ -17,29 +17,30 @@ sub relation{ sub _item_rs { my ($self, $c) = @_; - my $item_rs; - if ($c->req->param('subscriber_id')) { - $item_rs = $c->model('DB')->resultset('resellers')->search( - { - 'voip_subscribers.id' => $c->req->param('subscriber_id') - }, - { - join => { 'contacts' => { 'contracts' => 'voip_subscribers' } } - } - ); - } else { - $item_rs = $c->model('DB')->resultset('resellers')->search({ - status => { '!=' => 'terminated' } - }); - } + + my $item_rs = $c->model('DB')->resultset('reseller_brandings')->search({ + 'reseller.status' => { '!=' => 'terminated' } + },{ + join => 'reseller' + }); + if ($c->user->roles eq "admin") { } elsif ($c->user->roles eq "reseller") { - $item_rs = $item_rs->search({ reseller_id => $c->user->reseller_id }); + $item_rs = $item_rs->search({ + 'me.reseller_id' => $c->user->reseller_id + }); } elsif ($c->user->roles eq "subscriberadmin" || $c->user->roles eq "subscriber") { - my $reseller_id = $c->user->contract->contact->reseller_id; - return unless $reseller_id; $item_rs = $item_rs->search({ - reseller_id => $reseller_id, + 'provisioning_voip_subscriber.id' => $c->user->id, + },{ + join => { 'reseller' => { + 'contacts' => { + 'contracts' => { + 'voip_subscribers' => 'provisioning_voip_subscriber' + } + } + } + } }); } return $item_rs;