From 1fbccb8e17a295db4dc4e135aeb76fdbaa92a1a6 Mon Sep 17 00:00:00 2001 From: Rene Krenn Date: Thu, 18 Jul 2024 11:47:02 +0200 Subject: [PATCH] MT#60520 prov templates: fix lookup of items item (contract, contact, subscriber) lookup will pick the first matching element (an arbitrary one if multiple are matching). ie. when lookup identifiers are too loose (f.ex. subscribers without domain in a multidomain setup) it might pick a wrong item - which is ok (supposed to bail out at a later step) unless that wrong item is a *terminated* one, causing to proceed checks on a terminated record (which can never happen from API or UI side since those hide terminated items), which produces a misleading 500 error in the end. we prevent this now by ensuring that looked up items are not terminated ones. even if the looked up items is not the desired one, becasue of an inaccurate template. Change-Id: I6691937c2e62b05915c7eac9980224abe2185b2c (cherry picked from commit 0fd36ace55ef543819c8545474c11534489dffc3) (cherry picked from commit 0f497541348e068dc5ada0c10d0ece00c457d50b) --- lib/NGCP/Panel/Utils/ProvisioningTemplates.pm | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/lib/NGCP/Panel/Utils/ProvisioningTemplates.pm b/lib/NGCP/Panel/Utils/ProvisioningTemplates.pm index 08d65e9583..3ea25d1122 100644 --- a/lib/NGCP/Panel/Utils/ProvisioningTemplates.pm +++ b/lib/NGCP/Panel/Utils/ProvisioningTemplates.pm @@ -785,13 +785,14 @@ sub _init_contract_context { } $context->{contract_contact} = \%contract_contact; if (scalar @identifiers) { - my $e = $schema->resultset('contacts')->search_rs({ - map { $_ => $contract_contact{$_}; } @identifiers - })->first; - if ($e and 'terminated' ne $e->status) { - $contract_contact{id} = $e->id; - } else { - delete $contract_contact{id}; + delete $contract_contact{id}; + foreach my $e ($schema->resultset('contacts')->search_rs({ + map { $_ => $contract_contact{$_}; } @identifiers + })->all) { + if ('terminated' ne $e->status) { + $contract_contact{id} = $e->id; + last; + } } } else { delete $contract_contact{id}; @@ -863,13 +864,14 @@ sub _init_contract_context { $context->{contract} = \%contract; if (scalar @identifiers) { - my $e = $schema->resultset('contracts')->search_rs({ - map { $_ => $contract{$_}; } @identifiers - })->first; - if ($e and 'terminated' ne $e->status) { - $contract{id} = $e->id; - } else { - delete $contract{id}; + delete $contract{id}; + foreach my $e ($schema->resultset('contracts')->search_rs({ + map { $_ => $contract{$_}; } @identifiers + })->all) { + if ('terminated' ne $e->status) { + $contract{id} = $e->id; + last; + } } } else { delete $contract{id}; @@ -930,15 +932,17 @@ sub _init_subscriber_context { my $item; if (scalar @identifiers) { - $item = $schema->resultset('voip_subscribers')->search_rs({ - map { $_ => $subscriber{$_}; } @identifiers - },{ - join => 'domain', - })->first; - if ($item and 'terminated' ne $item->status) { - $subscriber{id} = $item->id; - } else { - delete $subscriber{id}; + delete $subscriber{id}; + foreach my $e ($schema->resultset('voip_subscribers')->search_rs({ + map { $_ => $subscriber{$_}; } @identifiers + },{ + #join => 'domain', + })->all) { + if ('terminated' ne $e->status) { + $subscriber{id} = $e->id; + $item = $e; + last; + } } } else { delete $subscriber{id};