MT#32992 fix /api/resellerbrandnglogos

* add described query_params 'subscriber_id', 'reseller_id' so they are
  avaialable on the doc
* remove search by param from Role::ResellerBrandingLogos::_item_rs()
* fix Role::ResellerBrandingLogos::_item_rs() queries
* Controller::API::ResellerBrandingLogos*
    - now always returns the binary data containing the logo and with
      the Content-Type header
    - change NOT_FOUND message to indicate that the ResellerBrandingLogo
      is either not found or does not have image/image_type
* do not log response queries
* fix allowed_role to be: admin, reseller, subsscriberadmin

Change-Id: Iaadb47fb2d72886a8d9244a523d5914500a4dd20
mr11.2
Kirill Solomko 3 years ago
parent 2904de66b9
commit f942b07a1e

@ -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.");
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 $branding = $item->first->branding;
my $item = $self->item_rs($c)->first;
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;

@ -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;

@ -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;

Loading…
Cancel
Save