From 7a5c1eb12ae73c1d6ecbc2de72f0e17640ed9e25 Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Tue, 7 Mar 2017 15:20:54 +0100 Subject: [PATCH] TT#4335 callforwards for subadmin API Change-Id: I3e11c1405f971261aee6e6aff558a8bc2aad2185 --- lib/NGCP/Panel/Controller/API/CallForwards.pm | 2 +- .../Panel/Controller/API/CallForwardsItem.pm | 4 +- lib/NGCP/Panel/Role/API/CFDestinationSets.pm | 2 +- lib/NGCP/Panel/Role/API/CallForwards.pm | 47 +++++++++++++++++-- lib/NGCP/Panel/Utils/Subscriber.pm | 18 ++++++- sandbox/callforwards.pl | 2 +- 6 files changed, 66 insertions(+), 9 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/CallForwards.pm b/lib/NGCP/Panel/Controller/API/CallForwards.pm index ddc56ab26a..5662a7d768 100644 --- a/lib/NGCP/Panel/Controller/API/CallForwards.pm +++ b/lib/NGCP/Panel/Controller/API/CallForwards.pm @@ -65,7 +65,7 @@ __PACKAGE__->config( action => { map { $_ => { ACLDetachTo => '/api/root/invalid_user', - AllowedRole => [qw/admin reseller/], + AllowedRole => [qw/admin reseller subscriberadmin/], Args => 0, Does => [qw(ACL CheckTrailingSlash RequireSSL)], Method => $_, diff --git a/lib/NGCP/Panel/Controller/API/CallForwardsItem.pm b/lib/NGCP/Panel/Controller/API/CallForwardsItem.pm index 7c2894d0bc..7b8a546422 100644 --- a/lib/NGCP/Panel/Controller/API/CallForwardsItem.pm +++ b/lib/NGCP/Panel/Controller/API/CallForwardsItem.pm @@ -42,7 +42,7 @@ __PACKAGE__->config( action => { (map { $_ => { ACLDetachTo => '/api/root/invalid_user', - AllowedRole => [qw/admin reseller/], + AllowedRole => [qw/admin reseller subscriberadmin/], Args => 1, Does => [qw(ACL RequireSSL)], Method => $_, @@ -50,7 +50,7 @@ __PACKAGE__->config( } } @{ __PACKAGE__->allowed_methods }), @{ __PACKAGE__->get_journal_action_config(__PACKAGE__->resource_name,{ ACLDetachTo => '/api/root/invalid_user', - AllowedRole => [qw/admin reseller/], + AllowedRole => [qw/admin reseller subscriberadmin/], Does => [qw(ACL RequireSSL)], }) } }, diff --git a/lib/NGCP/Panel/Role/API/CFDestinationSets.pm b/lib/NGCP/Panel/Role/API/CFDestinationSets.pm index a14d2eca69..206133c6b2 100644 --- a/lib/NGCP/Panel/Role/API/CFDestinationSets.pm +++ b/lib/NGCP/Panel/Role/API/CFDestinationSets.pm @@ -29,7 +29,7 @@ sub hal_from_item { my ($d, $duri) = NGCP::Panel::Utils::Subscriber::destination_to_field($dest->destination); my $deflated; if($d eq "uri") { - $deflated = NGCP::Panel::Utils::Subscriber::uri_deflate($duri, $item->subscriber->voip_subscriber); + $deflated = NGCP::Panel::Utils::Subscriber::uri_deflate($c, $duri, $item->subscriber->voip_subscriber); $d = $dest->destination; } my $destelem = {$dest->get_inflated_columns, diff --git a/lib/NGCP/Panel/Role/API/CallForwards.pm b/lib/NGCP/Panel/Role/API/CallForwards.pm index 2dd789050b..078d1e34df 100644 --- a/lib/NGCP/Panel/Role/API/CallForwards.pm +++ b/lib/NGCP/Panel/Role/API/CallForwards.pm @@ -46,8 +46,11 @@ sub hal_from_item { ], relation => 'ngcp:'.$self->resource_name, ); + + my $allowed_prefs = $self->_get_allowed_prefs($c, $prov_subs); @resource{qw/cfu cfb cft cfna cfs/} = ({}) x 5; for my $item_cf ($item->provisioning_voip_subscriber->voip_cf_mappings->all) { + next unless ($allowed_prefs->{$item_cf->type}); $resource{$item_cf->type} = $self->_contents_from_cfm($c, $item_cf, $item); } if(keys %{$resource{cft}}){ @@ -66,6 +69,12 @@ sub hal_from_item { run => 0, ); + for my $cf_type (qw/cfu cft cfb cfna cfs/) { + unless ($allowed_prefs->{$cf_type}) { + delete $resource{$cf_type}; + } + } + $hal->resource(\%resource); return $hal; } @@ -79,12 +88,18 @@ sub _item_rs { { 'me.status' => { '!=' => 'terminated' } }, { 'prefetch' => { 'provisioning_voip_subscriber' => 'voip_cf_mappings' },}, ); - if($c->user->roles eq "reseller") { + if ($c->user->roles eq "reseller") { $item_rs = $item_rs->search({ 'contact.reseller_id' => $c->user->reseller_id, }, { join => { 'contract' => 'contact' }, }); + } elsif ($c->user->roles eq 'subscriberadmin') { + $item_rs = $item_rs->search({ + 'contract.id' => $c->user->account_id, + }, { + join => 'contract', + }); } return $item_rs; @@ -103,6 +118,7 @@ sub update_item { my $prov_subs = $item->provisioning_voip_subscriber; die "need provisioning_voip_subscriber" unless $prov_subs; my $prov_subscriber_id = $prov_subs->id; + my $allowed_prefs = $self->_get_allowed_prefs($c, $prov_subs); return unless $self->validate_form( c => $c, @@ -113,6 +129,7 @@ sub update_item { for my $type (qw/cfu cfb cft cfna cfs/) { next unless "ARRAY" eq ref $resource->{$type}{destinations}; + next unless ($allowed_prefs->{$type}); for my $d (@{ $resource->{$type}{destinations} }) { if (exists $d->{timeout} && ! is_int($d->{timeout})) { $c->log->error("Invalid timeout in '$type'"); @@ -144,6 +161,7 @@ sub update_item { } for my $type (qw/cfu cfb cft cfna cfs/) { + next unless ($allowed_prefs->{$type}); my $mapping = $c->model('DB')->resultset('voip_cf_mappings')->search_rs({ subscriber_id => $prov_subscriber_id, type => $type, @@ -237,6 +255,8 @@ sub update_item { domain => $domain, uri => $d->{destination}, cf_type => $type, + c => $c, + subscriber => $item, ); $dset->voip_cf_destinations->update_or_create($d); } @@ -269,7 +289,7 @@ sub update_item { } } - if ($resource->{cft}{ringtimeout} && $resource->{cft}{ringtimeout} > 0) { + if ($allowed_prefs->{cft} && $resource->{cft}{ringtimeout} && $resource->{cft}{ringtimeout} > 0) { my $ringtimeout_preference = NGCP::Panel::Utils::Preferences::get_usr_preference_rs( c => $c, attribute => 'ringtimeout', prov_subscriber => $prov_subs); @@ -302,7 +322,7 @@ sub _contents_from_cfm { my ($d, $duri) = NGCP::Panel::Utils::Subscriber::destination_to_field($dest->destination); my $deflated; if($d eq "uri") { - $deflated = NGCP::Panel::Utils::Subscriber::uri_deflate($duri,$sub) if $d eq "uri"; + $deflated = NGCP::Panel::Utils::Subscriber::uri_deflate($c, $duri, $sub) if $d eq "uri"; $d = $dest->destination; } push @destinations, {$dest->get_inflated_columns, @@ -318,5 +338,26 @@ sub _contents_from_cfm { return {times => \@times, destinations => \@destinations, sources => \@sources}; } +sub _get_allowed_prefs { + my ($self, $c, $prov_subs) = @_; + + my %allowed_prefs = map {$_ => 1} (qw/cfu cfb cft cfna cfs/); + + if ($c->user->roles eq "subscriber" || $c->user->roles eq "subscriberadmin") { + if ($prov_subs && $prov_subs->voip_subscriber_profile) { + my $profile = $prov_subs->voip_subscriber_profile; + my @allowed_attr_ids = $profile->profile_attributes + ->get_column('attribute_id')->all; + my @allowed_attrs = $c->model('DB')->resultset('voip_preferences')->search_rs({ + 'id' => { '-in' => \@allowed_attr_ids }, + 'attribute' => { '-in' => [qw/cfu cfb cft cfna cfs/]}, + })->get_column('attribute')->all; + %allowed_prefs = map {$_ => 1} @allowed_attrs; + } + } + + return \%allowed_prefs; +} + 1; # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Utils/Subscriber.pm b/lib/NGCP/Panel/Utils/Subscriber.pm index 9163b7f298..3628e96804 100644 --- a/lib/NGCP/Panel/Utils/Subscriber.pm +++ b/lib/NGCP/Panel/Utils/Subscriber.pm @@ -1161,6 +1161,9 @@ sub field_to_destination { my $d = $params{destination}; my $uri = $params{uri}; my $cf_type = $params{cf_type}; + my $c = $params{c}; # if not passed, rwr is not applied (web panel, it is done there separately) + my $sub = $params{subscriber}; + my $vm_prefix = "vmu"; if(defined $cf_type && $cf_type eq "cfb") { $vm_prefix = "vmb"; @@ -1189,6 +1192,13 @@ sub field_to_destination { $v =~ s/^sips?://; my ($vuser, $vdomain) = split(/\@/, $v); $vdomain = $domain unless($vdomain); + + if($c && $c->user->roles eq "subscriberadmin" || $c->user->roles eq "subscriber") { + $vuser = NGCP::Panel::Utils::Subscriber::apply_rewrite( + c => $c, subscriber => $sub, number => $vuser, direction => 'callee_in', + ); + } + $d = 'sip:' . $vuser . '@' . $vdomain; } return $d; @@ -1225,10 +1235,16 @@ sub destination_to_field { } sub uri_deflate { - my ($v, $sub) = @_; + my ($c, $v, $sub) = @_; + my $direction = 'caller_out'; $v =~ s/^sips?://; my $t; my ($user, $domain) = split(/\@/, $v); + if($c && ($c->user->roles eq "subscriberadmin" || $c->user->roles eq "subscriber")) { + $user = NGCP::Panel::Utils::Subscriber::apply_rewrite( + c => $c, subscriber => $sub, number => $user, direction => $direction, + ); + } if($domain eq $sub->domain->domain) { $v = $user; } else { diff --git a/sandbox/callforwards.pl b/sandbox/callforwards.pl index 6f9bdb9f1a..8bf9014b1a 100644 --- a/sandbox/callforwards.pl +++ b/sandbox/callforwards.pl @@ -143,7 +143,7 @@ sub _contents_from_cfm { my ($d, $duri) = NGCP::Panel::Utils::Subscriber::destination_to_field($dest->destination); my $deflated; if($d eq "uri") { - $deflated = NGCP::Panel::Utils::Subscriber::uri_deflate($duri,$sub) if $d eq "uri"; + $deflated = NGCP::Panel::Utils::Subscriber::uri_deflate(undef, $duri,$sub) if $d eq "uri"; $d = $dest->destination; } push @destinations, {$dest->get_inflated_columns,