From 938f6ba05c8028fd6a7559cd7a1c8f8966fe5f0b Mon Sep 17 00:00:00 2001 From: Kirill Solomko Date: Thu, 7 Jul 2022 21:31:00 +0200 Subject: [PATCH] TT#176050 add expand support on array of ids * it is possible to expand arrays of ids now * add expand support for pbx_group_ids * add expand support for pxb_groupmember_ids * remove _password and _webpassword internally prepared fields from expands by subscriber_id, pbx_group_ids, pbx_groupmember_ids Change-Id: I7651aae4c58d98943e82d1eda6b24d260ff2480a --- lib/NGCP/Panel/Form/Expand.pm | 24 +++++++++++++++++++++++- lib/NGCP/Panel/Role/API.pm | 35 ++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/lib/NGCP/Panel/Form/Expand.pm b/lib/NGCP/Panel/Form/Expand.pm index e879c4635f..95bce649b2 100644 --- a/lib/NGCP/Panel/Form/Expand.pm +++ b/lib/NGCP/Panel/Form/Expand.pm @@ -92,7 +92,7 @@ has_field 'subscriber_id' => ( element_attr => { expand => { class => 'NGCP::Panel::Role::API::Subscribers', - remove_fields => [qw(password webpassword)], + remove_fields => [qw(password webpassword _password _webpassword)], allowed_roles => [qw(admin reseller ccareadmin ccare subscriberadmin)], }, }, @@ -168,4 +168,26 @@ has_field 'voucher_id' => ( }, ); +has_field 'pbx_group_ids' => ( + type => 'PosInteger', + element_attr => { + expand => { + class => 'NGCP::Panel::Role::API::Subscribers', + remove_fields => [qw(password webpassword _password _webpassword)], + allowed_roles => [qw(admin reseller ccareadmin ccare subscriberadmin subscriber)], + }, + }, +); + +has_field 'pbx_groupmember_ids' => ( + type => 'PosInteger', + element_attr => { + expand => { + class => 'NGCP::Panel::Role::API::Subscribers', + remove_fields => [qw(password webpassword _password _webpassword)], + allowed_roles => [qw(admin reseller ccareadmin ccare subscriberadmin subscriber)], + }, + }, +); + 1; diff --git a/lib/NGCP/Panel/Role/API.pm b/lib/NGCP/Panel/Role/API.pm index 93202d604f..502e5ac1ef 100644 --- a/lib/NGCP/Panel/Role/API.pm +++ b/lib/NGCP/Panel/Role/API.pm @@ -1355,11 +1355,34 @@ sub expand_field { return unless $expand->{allowed_roles}; return unless any { $c->user->roles eq $_ } @{$expand->{allowed_roles}}; - my $id = $resource->{$field} // return $found; # null value but the field exists - my $to = $expand->{to} // $field . '_expand'; + my $id = $resource->{$field} // return $found; # null value but the field exists + my $to = $expand->{to} // $field . '_expand'; my $class = $expand->{class} // return; my $form = $class->get_form($c) // return; + if (ref $id eq 'ARRAY') { + for (my $i=0; $i<=$#$id; $i++) { + my $a_id = $id->[$i]; + $resource->{$to}[$i] = + $self->get_expanded_field_data($c, $expand, $resource, $field, $class, $form, $a_id); + if ($subfield) { + $found = $self->expand_field($c, $resource->{$to}[$i], $form, $subfield); + } + } + } else { + $resource->{$to} = + $self->get_expanded_field_data($c, $expand, $resource, $field, $class, $form, $id); + if ($subfield) { + $found = $self->expand_field($c, $resource->[$to], $form, $subfield); + } + } + + return defined $found; +} + +sub get_expanded_field_data { + my ($self, $c, $expand, $resource, $field, $class, $form, $id) = @_; + my $item = $class->item_by_id($c, $id) // return; my $item_res = $class->resource_from_item($c, $item, $form); @@ -1369,13 +1392,7 @@ sub expand_field { delete @{$data}{@{$remove_fields}}; } - $resource->{$to} = $data; - - if ($subfield) { - $found = $self->expand_field($c, $resource->{$to}, $form, $subfield); - } - - return defined $found; + return $data; } sub get_mandatory_params {