MT#7377 API: better check for JSON::Types::bool.

Sometimes the value is a ref to some other var, so a simple
if($foo){} is not sufficient, as it would be true, althouhg we're
just checking if it's defined and has a value.
mr3.3.1
Andreas Granig 12 years ago
parent 363249fac1
commit 0baef998a6

@ -2,6 +2,7 @@ package NGCP::Panel::Role::API;
use Moose::Role;
use Sipwise::Base;
use boolean;
use Storable qw();
use JSON qw();
use JSON::Pointer;
@ -129,6 +130,7 @@ sub validate_form {
# otherwise it breaks db fields with the \0 and \1 notation
unless($run) {
$resource->{$k} = JSON::Types::bool($resource->{$k})
#$resource->{$k} = $resource->{$k} ? true : false
if(defined $resource->{$k} &&
$form->field($k)->$_isa('HTML::FormHandler::Field::Boolean'));
}
@ -480,5 +482,29 @@ around 'item_rs' => sub {
return $item_rs;
};
sub is_true {
my ($self, $v) = @_;
my $val;
if(ref $v eq "") {
$val = $v;
} else {
$val = $$v;
}
return 1 if(defined $val && $val == 1);
return;
}
sub is_false {
my ($self, $v) = @_;
my $val;
if(ref $v eq "") {
$val = $v;
} else {
$val = $$v;
}
return 1 unless(defined $val && $val == 1);
return;
}
1;
# vim: set tabstop=4 expandtab:

@ -33,11 +33,15 @@ sub resource_from_item {
delete $prov_resource->{domain_id};
delete $prov_resource->{account_id};
my %resource = %{ $bill_resource->merge($prov_resource) };
$resource{administrative} = delete $resource{admin};
unless($customer->get_column('product_class') eq 'pbxaccount') {
delete $resource{is_pbx_group};
delete $resource{is_pbx_pilot};
delete $resource{pbx_extension};
delete $resource{pbx_group_id};
}
unless($resource{is_pbx_group}) {
unless($self->is_true($resource{is_pbx_group})) {
delete $resource{pbx_hunt_policy};
delete $resource{pbx_hunt_timeout};
}
@ -48,7 +52,6 @@ sub resource_from_item {
$resource{email} = undef;
}
$resource{administrative} = delete $resource{admin};
$form //= $self->get_form($c);
last unless $self->validate_form(
@ -88,6 +91,8 @@ sub resource_from_item {
$resource{id} = int($item->id);
$resource{domain} = $item->domain->domain;
use Data::Printer; p %resource;
return \%resource;
}
@ -269,12 +274,13 @@ sub prepare_resource {
join => 'provisioning_voip_subscriber',
})->first;
if($pilot && $resource->{is_pbx_pilot} && $pilot->id != $subscriber_id) {
use Data::Printer; p $resource;
if($pilot && $self->is_true($resource->{is_pbx_pilot}) && $pilot->id != $subscriber_id) {
$c->log->error("failed to create subscriber, contract_id " . $customer->id . " already has pbx pilot subscriber");
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Customer already has a pbx pilot subscriber.");
return;
}
elsif(!$pilot && !$resource->{is_pbx_pilot}) {
elsif(!$pilot && !$self->is_true($resource->{is_pbx_pilot})) {
$c->log->error("failed to create subscriber, contract_id " . $customer->id . " has no pbx pilot subscriber and is_pbx_pilot is set");
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Customer has no pbx pilot subscriber yet and is_pbx_pilot is not set.");
return;
@ -307,7 +313,7 @@ sub prepare_resource {
$resource->{e164}->{ac} = $pilot->primary_number->ac // '';
$resource->{e164}->{sn} = $pilot->primary_number->sn . $resource->{pbx_extension};
unless($resource->{is_pbx_group}) {
unless($self->is_true($resource->{is_pbx_group})) {
unless($resource->{pbx_group_id}) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "A pbx_group_id is required if customer is PBX and pilot subscriber exists.");
return;
@ -411,7 +417,7 @@ sub update_item {
my $preferences = $full_resource->{preferences};
if($subscriber->provisioning_voip_subscriber->is_pbx_pilot && !$resource->{is_pbx_pilot}) {
if($subscriber->provisioning_voip_subscriber->is_pbx_pilot && !$self->is_true($resource->{is_pbx_pilot})) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Cannot revoke is_pbx_pilot status from a subscriber.");
return;
}
@ -543,7 +549,7 @@ sub update_item {
profile_set_id => $profile_set ? $profile_set->id : undef,
profile_id => $profile ? $profile->id : undef,
};
if($resource->{is_pbx_group}) {
if($self->is_true($resource->{is_pbx_group})) {
$provisioning_res->{pbx_hunt_policy} = $resource->{pbx_hunt_policy};
$provisioning_res->{pbx_hunt_timeout} = $resource->{pbx_hunt_timeout};
$provisioning_res->{pbx_group_id} = undef;

Loading…
Cancel
Save