diff --git a/lib/NGCP/Panel/Controller/API/CustomerPreferenceDefs.pm b/lib/NGCP/Panel/Controller/API/CustomerPreferenceDefs.pm new file mode 100644 index 0000000000..ecae353fef --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/CustomerPreferenceDefs.pm @@ -0,0 +1,130 @@ +package NGCP::Panel::Controller::API::CustomerPreferenceDefs; +use Sipwise::Base; +use namespace::sweep; +use boolean qw(true); +use Data::HAL qw(); +use Data::HAL::Link qw(); +use HTTP::Headers qw(); +use HTTP::Status qw(:constants); +use MooseX::ClassAttribute qw(class_has); +use NGCP::Panel::Utils::DateTime; +use Path::Tiny qw(path); +use Safe::Isa qw($_isa); +use JSON::Types qw(); +BEGIN { extends 'Catalyst::Controller::ActionRole'; } +require Catalyst::ActionRole::ACL; +require Catalyst::ActionRole::CheckTrailingSlash; +require Catalyst::ActionRole::HTTPMethods; +require Catalyst::ActionRole::RequireSSL; + +with 'NGCP::Panel::Role::API'; + +class_has('resource_name', is => 'ro', default => 'customerpreferencedefs'); +class_has('dispatch_path', is => 'ro', default => '/api/customerpreferencedefs/'); +class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-customerpreferencedefs'); + +__PACKAGE__->config( + action => { + map { $_ => { + ACLDetachTo => '/api/root/invalid_user', + AllowedRole => [qw/admin reseller/], + Args => 0, + Does => [qw(ACL CheckTrailingSlash RequireSSL)], + Method => $_, + Path => __PACKAGE__->dispatch_path, + } } @{ __PACKAGE__->allowed_methods } + }, + action_roles => [qw(HTTPMethods)], +); + +sub auto :Private { + my ($self, $c) = @_; + + $self->set_body($c); + $self->log_request($c); +} + +sub GET :Allow { + my ($self, $c) = @_; + { + my @links; + push @links, + Data::HAL::Link->new( + relation => 'curies', + href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}', + name => 'ngcp', + templated => true, + ), + Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), + Data::HAL::Link->new(relation => 'self', href => sprintf('%s', $self->dispatch_path)); + + my $hal = Data::HAL->new( + links => [@links], + ); + + my $preferences = $c->model('DB')->resultset('voip_preferences')->search({ + internal => 0, + contract_pref => 1, + }); + my $resource = {}; + for my $pref($preferences->all) { + my $fields = { $pref->get_inflated_columns }; + # remove internal fields + for my $del(qw/type attribute expose_to_customer internal peer_pref usr_pref dom_pref contract_pref voip_preference_groups_id id modify_timestamp/) { + delete $fields->{$del}; + } + $fields->{max_occur} = int($fields->{max_occur}); + $fields->{read_only} = JSON::Types::bool($fields->{read_only}); + if($fields->{data_type} eq "enum") { + my @enums = $pref->voip_preferences_enums->search({ + contract_pref => 1, + })->all; + $fields->{enum_values} = []; + foreach my $enum(@enums) { + my $efields = { $enum->get_inflated_columns }; + for my $del(qw/id preference_id usr_pref dom_pref peer_pref contract_pref/) { + delete $efields->{$del}; + } + $efields->{default_val} = JSON::Types::bool($efields->{default_val}); + push @{ $fields->{enum_values} }, $efields; + } + } + $resource->{$pref->attribute} = $fields; + } + $hal->resource($resource); + + my $response = HTTP::Response->new(HTTP_OK, undef, + HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json); + $c->response->headers($response->headers); + $c->response->body($response->content); + return; + } + return; +} + +sub HEAD :Allow { + my ($self, $c) = @_; + $c->forward(qw(GET)); + $c->response->body(q()); + return; +} + +sub OPTIONS :Allow { + my ($self, $c) = @_; + my $allowed_methods = $self->allowed_methods; + $c->response->headers(HTTP::Headers->new( + Allow => $allowed_methods->join(', '), + Accept_Post => 'application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-'.$self->resource_name, + )); + $c->response->content_type('application/json'); + $c->response->body(JSON::to_json({ methods => $allowed_methods })."\n"); + return; +} + +sub end : Private { + my ($self, $c) = @_; + + $self->log_response($c); +} + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/CustomerPreferences.pm b/lib/NGCP/Panel/Controller/API/CustomerPreferences.pm new file mode 100644 index 0000000000..98b919cf91 --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/CustomerPreferences.pm @@ -0,0 +1,125 @@ +package NGCP::Panel::Controller::API::CustomerPreferences; +use Sipwise::Base; +use namespace::sweep; +use boolean qw(true); +use Data::HAL qw(); +use Data::HAL::Link qw(); +use HTTP::Headers qw(); +use HTTP::Status qw(:constants); +use MooseX::ClassAttribute qw(class_has); +use NGCP::Panel::Utils::DateTime; +use Path::Tiny qw(path); +use Safe::Isa qw($_isa); +BEGIN { extends 'Catalyst::Controller::ActionRole'; } +require Catalyst::ActionRole::ACL; +require Catalyst::ActionRole::CheckTrailingSlash; +require Catalyst::ActionRole::HTTPMethods; +require Catalyst::ActionRole::RequireSSL; + +class_has 'api_description' => ( + is => 'ro', + isa => 'Str', + default => + 'Specifies certain properties (preferences) for a Customer. The full list of properties can be obtained via CustomerPreferenceDefs.' +); + +with 'NGCP::Panel::Role::API::Preferences'; + +class_has('resource_name', is => 'ro', default => 'customerpreferences'); +class_has('dispatch_path', is => 'ro', default => '/api/customerpreferences/'); +class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-customerpreferences'); + +__PACKAGE__->config( + action => { + map { $_ => { + ACLDetachTo => '/api/root/invalid_user', + AllowedRole => [qw/admin reseller/], + Args => 0, + Does => [qw(ACL CheckTrailingSlash RequireSSL)], + Method => $_, + Path => __PACKAGE__->dispatch_path, + } } @{ __PACKAGE__->allowed_methods } + }, + action_roles => [qw(HTTPMethods)], +); + +sub auto :Private { + my ($self, $c) = @_; + + $self->set_body($c); + $self->log_request($c); +} + +sub GET :Allow { + my ($self, $c) = @_; + my $page = $c->request->params->{page} // 1; + my $rows = $c->request->params->{rows} // 10; + { + my $customers = $self->item_rs($c, "contracts"); + (my $total_count, $customers) = $self->paginate_order_collection($c, $customers); + my (@embedded, @links); + for my $customer ($customers->all) { + push @embedded, $self->hal_from_item($c, $customer, "contracts"); + push @links, Data::HAL::Link->new( + relation => 'ngcp:'.$self->resource_name, + href => sprintf('%s%d', $self->dispatch_path, $customer->id), + ); + } + push @links, + Data::HAL::Link->new( + relation => 'curies', + href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}', + name => 'ngcp', + templated => true, + ), + Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), + Data::HAL::Link->new(relation => 'self', href => sprintf('%s?page=%s&rows=%s', $self->dispatch_path, $page, $rows)); + if(($total_count / $rows) > $page ) { + push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('%s?page=%d&rows=%d', $self->dispatch_path, $page + 1, $rows)); + } + if($page > 1) { + push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('%s?page=%d&rows=%d', $self->dispatch_path, $page - 1, $rows)); + } + + my $hal = Data::HAL->new( + embedded => [@embedded], + links => [@links], + ); + $hal->resource({ + total_count => $total_count, + }); + my $response = HTTP::Response->new(HTTP_OK, undef, + HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json); + $c->response->headers($response->headers); + $c->response->body($response->content); + return; + } + return; +} + +sub HEAD :Allow { + my ($self, $c) = @_; + $c->forward(qw(GET)); + $c->response->body(q()); + return; +} + +sub OPTIONS :Allow { + my ($self, $c) = @_; + my $allowed_methods = $self->allowed_methods; + $c->response->headers(HTTP::Headers->new( + Allow => $allowed_methods->join(', '), + Accept_Post => 'application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-'.$self->resource_name, + )); + $c->response->content_type('application/json'); + $c->response->body(JSON::to_json({ methods => $allowed_methods })."\n"); + return; +} + +sub end : Private { + my ($self, $c) = @_; + + $self->log_response($c); +} + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/CustomerPreferencesItem.pm b/lib/NGCP/Panel/Controller/API/CustomerPreferencesItem.pm new file mode 100644 index 0000000000..5328335149 --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/CustomerPreferencesItem.pm @@ -0,0 +1,183 @@ +package NGCP::Panel::Controller::API::CustomerPreferencesItem; +use Sipwise::Base; +use namespace::sweep; +use boolean qw(true); +use Data::HAL qw(); +use Data::HAL::Link qw(); +use HTTP::Headers qw(); +use HTTP::Status qw(:constants); +use MooseX::ClassAttribute qw(class_has); +use NGCP::Panel::Utils::ValidateJSON qw(); +use NGCP::Panel::Utils::DateTime; +use Path::Tiny qw(path); +use Safe::Isa qw($_isa); +BEGIN { extends 'Catalyst::Controller::ActionRole'; } +require Catalyst::ActionRole::ACL; +require Catalyst::ActionRole::HTTPMethods; +require Catalyst::ActionRole::RequireSSL; + +with 'NGCP::Panel::Role::API::Preferences'; + +class_has('resource_name', is => 'ro', default => 'customerpreferences'); +class_has('dispatch_path', is => 'ro', default => '/api/customerpreferences/'); +class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-customerpreferences'); + +__PACKAGE__->config( + action => { + map { $_ => { + ACLDetachTo => '/api/root/invalid_user', + AllowedRole => [qw/admin reseller/], + Args => 1, + Does => [qw(ACL RequireSSL)], + Method => $_, + Path => __PACKAGE__->dispatch_path, + } } @{ __PACKAGE__->allowed_methods }, + }, + action_roles => [qw(HTTPMethods)], +); + +sub auto :Private { + my ($self, $c) = @_; + + $self->set_body($c); + $self->log_request($c); + return 1; +} + +sub GET :Allow { + my ($self, $c, $id) = @_; + { + last unless $self->valid_id($c, $id); + my $customer = $self->item_by_id($c, $id, "contracts"); + last unless $self->resource_exists($c, customerpreference => $customer); + + my $hal = $self->hal_from_item($c, $customer, "contracts"); + + my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( + (map { # XXX Data::HAL must be able to generate links with multiple relations + s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|r + =~ s/rel=self/rel="item self"/r; + } $hal->http_headers), + ), $hal->as_json); + $c->response->headers($response->headers); + $c->response->body($response->content); + return; + } + return; +} + +sub HEAD :Allow { + my ($self, $c, $id) = @_; + $c->forward(qw(GET)); + $c->response->body(q()); + return; +} + +sub OPTIONS :Allow { + my ($self, $c, $id) = @_; + my $allowed_methods = $self->allowed_methods; + $c->response->headers(HTTP::Headers->new( + Allow => $allowed_methods->join(', '), + Accept_Patch => 'application/json-patch+json', + )); + $c->response->content_type('application/json'); + $c->response->body(JSON::to_json({ methods => $allowed_methods })."\n"); + return; +} + +sub PATCH :Allow { + my ($self, $c, $id) = @_; + my $guard = $c->model('DB')->txn_scope_guard; + { + my $preference = $self->require_preference($c); + last unless $preference; + + my $json = $self->get_valid_patch_data( + c => $c, + id => $id, + media_type => 'application/json-patch+json', + ops => [qw/add replace remove copy/], + ); + last unless $json; + + my $customer = $self->item_by_id($c, $id, "contracts"); + last unless $self->resource_exists($c, customerpreferences => $customer); + my $old_resource = $self->get_resource($c, $customer, "contracts"); + my $resource = $self->apply_patch($c, $old_resource, $json); + last unless $resource; + + # last param is "no replace" to NOT delete existing prefs + # for proper PATCH behavior + $customer = $self->update_item($c, $customer, $old_resource, $resource, 0, "contracts"); + last unless $customer; + + $guard->commit; + + if ('minimal' eq $preference) { + $c->response->status(HTTP_NO_CONTENT); + $c->response->header(Preference_Applied => 'return=minimal'); + $c->response->body(q()); + } else { + my $hal = $self->hal_from_item($c, $customer, "contracts"); + my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( + $hal->http_headers, + ), $hal->as_json); + $c->response->headers($response->headers); + $c->response->header(Preference_Applied => 'return=representation'); + $c->response->body($response->content); + } + } + return; +} + +sub PUT :Allow { + my ($self, $c, $id) = @_; + my $guard = $c->model('DB')->txn_scope_guard; + { + my $preference = $self->require_preference($c); + last unless $preference; + + my $customer = $self->item_by_id($c, $id, "contracts"); + last unless $self->resource_exists($c, customerpreference => $customer); + my $resource = $self->get_valid_put_data( + c => $c, + id => $id, + media_type => 'application/json', + ); + last unless $resource; + my $old_resource = $self->get_resource($c, $customer, "contracts"); + + use Data::Printer; print ">>>>>>>>>>>>>>>> old resource\n"; p $old_resource; + + # last param is "replace" to delete all existing prefs + # for proper PUT behavior + $customer = $self->update_item($c, $customer, $old_resource, $resource, 1, "contracts"); + last unless $customer; + + $guard->commit; + + if ('minimal' eq $preference) { + $c->response->status(HTTP_NO_CONTENT); + $c->response->header(Preference_Applied => 'return=minimal'); + $c->response->body(q()); + } else { + my $hal = $self->hal_from_item($c, $customer, "contracts"); + my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( + $hal->http_headers, + ), $hal->as_json); + $c->response->headers($response->headers); + $c->response->header(Preference_Applied => 'return=representation'); + $c->response->body($response->content); + } + } + return; +} + +sub end : Private { + my ($self, $c) = @_; + + $self->log_response($c); + return; +} + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/DomainPreferenceDefs.pm b/lib/NGCP/Panel/Controller/API/DomainPreferenceDefs.pm index a0b9e4a86a..a89c30cd9a 100644 --- a/lib/NGCP/Panel/Controller/API/DomainPreferenceDefs.pm +++ b/lib/NGCP/Panel/Controller/API/DomainPreferenceDefs.pm @@ -70,7 +70,7 @@ sub GET :Allow { for my $pref($preferences->all) { my $fields = { $pref->get_inflated_columns }; # remove internal fields - for my $del(qw/type attribute expose_to_customer internal peer_pref usr_pref dom_pref voip_preference_groups_id id modify_timestamp/) { + for my $del(qw/type attribute expose_to_customer internal peer_pref usr_pref dom_pref contract_pref voip_preference_groups_id id modify_timestamp/) { delete $fields->{$del}; } $fields->{max_occur} = int($fields->{max_occur}); @@ -82,7 +82,7 @@ sub GET :Allow { $fields->{enum_values} = []; foreach my $enum(@enums) { my $efields = { $enum->get_inflated_columns }; - for my $del(qw/id preference_id usr_pref dom_pref peer_pref/) { + for my $del(qw/id preference_id usr_pref dom_pref peer_pref contract_pref/) { delete $efields->{$del}; } $efields->{default_val} = JSON::Types::bool($efields->{default_val}); diff --git a/lib/NGCP/Panel/Controller/API/SubscriberPreferenceDefs.pm b/lib/NGCP/Panel/Controller/API/SubscriberPreferenceDefs.pm index 6b06080bef..65b67dbeca 100644 --- a/lib/NGCP/Panel/Controller/API/SubscriberPreferenceDefs.pm +++ b/lib/NGCP/Panel/Controller/API/SubscriberPreferenceDefs.pm @@ -70,7 +70,7 @@ sub GET :Allow { for my $pref($preferences->all) { my $fields = { $pref->get_inflated_columns }; # remove internal fields - for my $del(qw/type attribute expose_to_customer internal peer_pref usr_pref dom_pref voip_preference_groups_id id modify_timestamp/) { + for my $del(qw/type attribute expose_to_customer internal peer_pref usr_pref dom_pref contract_pref voip_preference_groups_id id modify_timestamp/) { delete $fields->{$del}; } $fields->{max_occur} = int($fields->{max_occur}); @@ -82,7 +82,7 @@ sub GET :Allow { $fields->{enum_values} = []; foreach my $enum(@enums) { my $efields = { $enum->get_inflated_columns }; - for my $del(qw/id preference_id usr_pref dom_pref peer_pref/) { + for my $del(qw/id preference_id usr_pref dom_pref peer_pref contract_pref/) { delete $efields->{$del}; } $efields->{default_val} = JSON::Types::bool($efields->{default_val}); diff --git a/lib/NGCP/Panel/Controller/Contract.pm b/lib/NGCP/Panel/Controller/Contract.pm index 6479b16912..0780dec4eb 100644 --- a/lib/NGCP/Panel/Controller/Contract.pm +++ b/lib/NGCP/Panel/Controller/Contract.pm @@ -200,13 +200,18 @@ sub terminate :Chained('base') :PathPart('terminate') :Args(0) { try { my $old_status = $contract->status; $contract->update({ status => 'terminated' }); - # if status changed, populate it down the chain - if($contract->status ne $old_status) { - NGCP::Panel::Utils::Contract::recursively_lock_contract( - c => $c, - contract => $contract, - ); - } + my $schema = $c->model('DB'); + $schema->txn_do(sub { + $contract->voip_contract_preferences->delete; + # if status changed, populate it down the chain + if($contract->status ne $old_status) { + NGCP::Panel::Utils::Contract::recursively_lock_contract( + c => $c, + contract => $contract, + schema => $schema, + ); + } + }); $c->flash(messages => [{type => 'success', text => $c->loc('Contract successfully terminated')}]); } catch ($e) { NGCP::Panel::Utils::Message->error( diff --git a/lib/NGCP/Panel/Controller/Customer.pm b/lib/NGCP/Panel/Controller/Customer.pm index c2217deedb..589ed3dc6b 100644 --- a/lib/NGCP/Panel/Controller/Customer.pm +++ b/lib/NGCP/Panel/Controller/Customer.pm @@ -476,14 +476,19 @@ sub terminate :Chained('base') :PathPart('terminate') :Args(0) { try { my $old_status = $contract->status; - $contract->update({ status => 'terminated' }); - # if status changed, populate it down the chain - if($contract->status ne $old_status) { - NGCP::Panel::Utils::Contract::recursively_lock_contract( - c => $c, - contract => $contract, - ); - } + my $schema = $c->model('DB'); + $schema->txn_do(sub { + $contract->voip_contract_preferences->delete; + $contract->update({ status => 'terminated' }); + # if status changed, populate it down the chain + if($contract->status ne $old_status) { + NGCP::Panel::Utils::Contract::recursively_lock_contract( + c => $c, + contract => $contract, + schema => $schema, + ); + } + }); $c->flash(messages => [{type => 'success', text => $c->loc('Customer successfully terminated') }]); } catch ($e) { NGCP::Panel::Utils::Message->error( @@ -1278,6 +1283,87 @@ sub pbx_device_sync :Chained('pbx_device_base') :PathPart('sync') :Args(0) { ); } +sub preferences :Chained('base') :PathPart('preferences') :Args(0) { + my ($self, $c) = @_; + + $self->load_preference_list($c); + $c->stash(template => 'customer/preferences.tt'); +} + + +sub preferences_base :Chained('base') :PathPart('preferences') :CaptureArgs(1) { + my ($self, $c, $pref_id) = @_; + + $self->load_preference_list($c); + + $c->stash->{preference_meta} = $c->model('DB') + ->resultset('voip_preferences') + ->single({id => $pref_id}); + + $c->stash->{preference} = $c->model('DB') + ->resultset('voip_contract_preferences') + ->search({ + attribute_id => $pref_id, + contract_id => $c->stash->{contract}->id, + }); + my @values = $c->stash->{preference}->get_column("value")->all; + $c->stash->{preference_values} = \@values; + $c->stash(template => 'customer/preferences.tt'); +} + +sub preferences_edit :Chained('preferences_base') :PathPart('edit') :Args(0) { + my ($self, $c) = @_; + + $c->stash(edit_preference => 1); + + my @enums = $c->stash->{preference_meta} + ->voip_preferences_enums + ->search({contract_pref => 1}) + ->all; + + my $pref_rs = $c->stash->{contract}->voip_contract_preferences; + + NGCP::Panel::Utils::Preferences::create_preference_form( c => $c, + pref_rs => $pref_rs, + enums => \@enums, + base_uri => $c->uri_for_action('/customer/preferences', [$c->req->captures->[0]]), + edit_uri => $c->uri_for_action('/customer/preferences_edit', $c->req->captures), + ); +} + +sub load_preference_list :Private { + my ($self, $c) = @_; + + my $contract_pref_values = $c->model('DB') + ->resultset('voip_preferences') + ->search({ + contract_id => $c->stash->{contract}->id, + },{ + prefetch => 'voip_contract_preferences', + }); + + my %pref_values; + foreach my $value($contract_pref_values->all) { + + $pref_values{$value->attribute} = [ + map {$_->value} $value->voip_contract_preferences->all + ]; + } + + my $reseller_id = $c->stash->{contract}->contact->reseller_id; + + my $ncos_levels_rs = $c->model('DB') + ->resultset('ncos_levels') + ->search_rs({ reseller_id => $reseller_id, }); + $c->stash(ncos_levels_rs => $ncos_levels_rs, + ncos_levels => [$ncos_levels_rs->all]); + + NGCP::Panel::Utils::Preferences::load_preference_list( c => $c, + pref_values => \%pref_values, + contract_pref => 1, + ); +} + =head1 AUTHOR Andreas Granig,,, diff --git a/lib/NGCP/Panel/Role/API/Preferences.pm b/lib/NGCP/Panel/Role/API/Preferences.pm index 2161145f87..1978b92751 100644 --- a/lib/NGCP/Panel/Role/API/Preferences.pm +++ b/lib/NGCP/Panel/Role/API/Preferences.pm @@ -25,6 +25,8 @@ sub get_form { sub hal_from_item { my ($self, $c, $item, $type) = @_; + my $print_type = $type; + $print_type = "customers" if $print_type eq "contracts"; my $hal = Data::HAL->new( links => [ Data::HAL::Link->new( @@ -36,7 +38,7 @@ sub hal_from_item { Data::HAL::Link->new(relation => 'collection', href => sprintf("%s", $self->dispatch_path)), Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $item->id)), - Data::HAL::Link->new(relation => "ngcp:$type", href => sprintf("/api/%s/%d", $type, $item->id)), + Data::HAL::Link->new(relation => "ngcp:$print_type", href => sprintf("/api/%s/%d", $print_type, $item->id)), ], relation => 'ngcp:'.$self->resource_name, ); @@ -56,7 +58,8 @@ sub get_resource { $prefs = $item->provisioning_voip_domain->voip_dom_preferences; } elsif($type eq "peerings") { $prefs = $item->voip_peer_preferences; - return; + } elsif($type eq "contracts") { + $prefs = $item->voip_contract_preferences; } $prefs = $prefs->search({ }, { @@ -69,7 +72,6 @@ sub get_resource { my $value; given($pref->attribute->attribute) { - $c->log->debug("+++++++++++++ checking preference ".$pref->attribute->attribute); when(/^rewrite_calle[re]_(in|out)_dpid$/) { next if(exists $resource->{rewrite_rule_set}); my $col = $pref->attribute->attribute; @@ -134,10 +136,15 @@ sub get_resource { next; } - default { next if $pref->attribute->internal != 0 } + default { + if($pref->attribute->internal != 0) { + next; + } + } } + given($pref->attribute->data_type) { when("int") { $value = int($pref->value) if($pref->value->is_int) } when("boolean") { $value = JSON::Types::bool($pref->value) if(defined $pref->value) } @@ -162,7 +169,11 @@ sub get_resource { } elsif($type eq "peerings") { $resource->{peering_id} = int($item->id); $resource->{id} = int($item->id); + } elsif($type eq "contracts") { + $resource->{customer_id} = int($item->id); + $resource->{id} = int($item->id); } + return $resource; } @@ -200,6 +211,23 @@ sub item_rs { } else { return; } + } elsif($type eq "contracts") { + if($c->user->roles eq "admin") { + $item_rs = $c->model('DB')->resultset('contracts')->search({ + status => { '!=' => 'terminated' }, + 'contact.reseller_id' => { '!=' => undef }, + + },{ + join => 'contact', + }); + } elsif($c->user->roles eq "reseller") { + $item_rs = $c->model('DB')->resultset('contracts')->search({ + 'contact.reseller_id' => $c->user->reseller_id, + 'status' => { '!=' => 'terminated' }, + }, { + join => 'contact', + }); + } } return $item_rs; } @@ -213,6 +241,10 @@ sub item_by_id { sub get_preference_rs { my ($self, $c, $type, $elem, $attr) = @_; + + + use Data::Printer; print ">>>>>>>>>>>>>>>> get_preference_rs, type=$type, elem=$elem, attr=$attr\n"; + my $rs; if($type eq "domains") { $rs = NGCP::Panel::Utils::Preferences::get_dom_preference_rs( @@ -232,6 +264,12 @@ sub get_preference_rs { attribute => $attr, peer_host => $elem, ); + } elsif($type eq "contracts") { + $rs = NGCP::Panel::Utils::Preferences::get_contract_preference_rs( + c => $c, + attribute => $attr, + contract => $elem, + ); } return $rs; } @@ -276,6 +314,15 @@ sub update_item { $full_rs = $elem->voip_peer_preferences; $pref_type = 'peer_pref'; $reseller_id = 1; + } elsif($type eq "contracts") { + use Data::Printer; print ">>>>>>>>>>>>>>>> prepare resource\n"; p $resource; p $old_resource; + delete $resource->{customer_id}; + delete $old_resource->{customer_id}; + $accessor = $item->id; + $elem = $item; + $full_rs = $elem->voip_contract_preferences; + $pref_type = 'contract_pref'; + $reseller_id = $item->contact->reseller_id; } else { return; } @@ -283,12 +330,13 @@ sub update_item { if($replace) { # in case of PUT, we remove all old entries try { - $full_rs->delete_all; + $full_rs->delete; } catch($e) { $c->log->error("failed to clear preferences for '$accessor': $e"); $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error."); return; }; + use Data::Printer; print ">>>>>>>>>>>>>>>> done deleteting old for PUT\n"; } else { # in case of PATCH, we remove only those entries marked for removal in the patch try { @@ -296,16 +344,12 @@ sub update_item { given($k) { # no special treatment for *_sound_set deletion, as id is stored in right name - $c->log->debug("+++++++++++++ check $k for deletion"); - when(/^rewrite_rule_set$/) { - $c->log->debug("+++++++++++++ check $k for deletion"); unless(exists $resource->{$k}) { - $c->log->debug("+++++++++++++ $k marked for deletion"); foreach my $p(qw/caller_in_dpid callee_in_dpid caller_out_dpid callee_out_dpid/) { my $rs = $self->get_preference_rs($c, $type, $elem, 'rewrite_' . $p); next unless $rs; # unknown resource, just ignore - $rs->delete_all; + $rs->delete; } } } @@ -313,7 +357,7 @@ sub update_item { unless(exists $resource->{$k}) { my $rs = $self->get_preference_rs($c, $type, $elem, $k . '_id'); next unless $rs; # unknown resource, just ignore - $rs->delete_all; + $rs->delete; } } when(/^(man_)?allowed_ips$/) { @@ -323,16 +367,16 @@ sub update_item { if($rs->first) { $c->model('DB')->resultset('voip_allowed_ip_groups')->search({ group_id => $rs->first->value, - })->delete_all; + })->delete; } - $rs->delete_all; + $rs->delete; } } default { unless(exists $resource->{$k}) { my $rs = $self->get_preference_rs($c, $type, $elem, $k); next unless $rs; # unknown resource, just ignore - $rs->delete_all; + $rs->delete; } } } @@ -345,8 +389,11 @@ sub update_item { } foreach my $pref(keys %{ $resource }) { + use Data::Printer; print ">>>>>>>>>>>>>>>> handling pref $pref\n"; next unless(defined $resource->{$pref}); + use Data::Printer; print ">>>>>>>>>>>>>>>> $pref is defined, get rs for type $type\n"; my $rs = $self->get_preference_rs($c, $type, $elem, $pref); + use Data::Printer; print ">>>>>>>>>>>>>>>> got an rs\n"; unless($rs) { $c->log->debug("removing unknown preference '$pref' from update"); next; @@ -381,7 +428,6 @@ sub update_item { } given($pref) { - $c->log->debug("+++++++++++++ checking preference $pref for update"); when(/^rewrite_rule_set$/) { my $rwr_set = $c->model('DB')->resultset('voip_rewrite_rule_sets')->find({ @@ -452,7 +498,7 @@ sub update_item { $aig_rs = $c->model('DB')->resultset('voip_allowed_ip_groups')->search({ group_id => $rs->first->value }); - $aig_rs->delete_all; + $aig_rs->delete; } else { my $aig_seq = $c->model('DB')->resultset('voip_aig_sequence')->search({},{ for => 'update', @@ -485,7 +531,7 @@ sub update_item { default { if($meta->max_occur != 1) { - $rs->delete_all; + $rs->delete; foreach my $v(@{ $resource->{$pref} }) { return unless $self->check_pref_value($c, $meta, $v, $pref_type); $rs->create({ value => $v }); diff --git a/lib/NGCP/Panel/Utils/Contract.pm b/lib/NGCP/Panel/Utils/Contract.pm index 62127efec0..6e4506e3dc 100644 --- a/lib/NGCP/Panel/Utils/Contract.pm +++ b/lib/NGCP/Panel/Utils/Contract.pm @@ -108,6 +108,7 @@ sub recursively_lock_contract { my $c = $params{c}; my $contract = $params{contract}; + my $schema = $params{schema} // $c->model('DB'); my $status = $contract->status; if($status eq 'terminated') { $contract->autoprov_field_devices->delete_all; @@ -139,7 +140,7 @@ sub recursively_lock_contract { } # then, check all child contracts in case of reseller - my $resellers = $c->model('DB')->resultset('resellers')->search({ + my $resellers = $schema->resultset('resellers')->search({ contract_id => $contract->id, }); for my $reseller($resellers->all) { diff --git a/lib/NGCP/Panel/Utils/Preferences.pm b/lib/NGCP/Panel/Utils/Preferences.pm index 3ddc49e5bb..84ea1b72d9 100644 --- a/lib/NGCP/Panel/Utils/Preferences.pm +++ b/lib/NGCP/Panel/Utils/Preferences.pm @@ -36,11 +36,16 @@ sub load_preference_list { my $peer_pref = $params{peer_pref}; my $dom_pref = $params{dom_pref}; my $usr_pref = $params{usr_pref}; + my $contract_pref = $params{contract_pref}; + my $customer_view = $params{customer_view} // 0; my @pref_groups = $c->model('DB') ->resultset('voip_preference_groups') ->search({ 'voip_preferences.internal' => { '<=' => 0 }, + $contract_pref ? ('voip_preferences.contract_pref' => 1, + -or => ['voip_preferences_enums.contract_pref' => 1, + 'voip_preferences_enums.contract_pref' => undef]) : (), $peer_pref ? ('voip_preferences.peer_pref' => 1, -or => ['voip_preferences_enums.peer_pref' => 1, 'voip_preferences_enums.peer_pref' => undef]) : (), @@ -77,7 +82,6 @@ sub load_preference_list { } } elsif($pref->attribute eq "adm_ncos") { - if ($pref_values->{adm_ncos_id} && (my $tmp = $c->stash->{ncos_levels_rs} ->find($pref_values->{adm_ncos_id}) )) { @@ -585,6 +589,23 @@ sub get_peer_preference_rs { }); } +sub get_contract_preference_rs { + my %params = @_; + + my $c = $params{c}; + my $attribute = $params{attribute}; + my $contract = $params{contract}; + + my $preference = $c->model('DB')->resultset('voip_preferences')->find({ + attribute => $attribute, 'contract_pref' => 1, + }); + return unless($preference); + return $preference->voip_contract_preferences->search_rs({ + contract_id => $contract->id, + }); +} + + sub get_peer_auth_params { my ($c, $prov_subscriber, $prefs) = @_; @@ -614,10 +635,8 @@ sub is_peer_auth_active { defined $prefs->{peer_auth_realm} && defined $prefs->{peer_auth_pass}) { - $c->log->debug("+++++++++++ peer auth register is active"); return 1; } - $c->log->debug("+++++++++++ peer auth register is NOT active"); return; } diff --git a/share/templates/customer/details.tt b/share/templates/customer/details.tt index 0104b4868e..3cf0b07fbc 100644 --- a/share/templates/customer/details.tt +++ b/share/templates/customer/details.tt @@ -17,6 +17,7 @@