From 8f6431fbeedc6d2df25da73edd3acc9afd81e9c9 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Tue, 8 Oct 2013 15:31:04 +0200 Subject: [PATCH] MT#4025 Implement management of customer sound set If a sound set has a contract_id, only show pbx group (consider it being a customer sound set). If it only has a reseller_id, show everything else except pbx group (consider it being a system sound set). --- lib/NGCP/Panel/Controller/Sound.pm | 75 +++++++++++++++++++----- lib/NGCP/Panel/Form/Sound/AdminSet.pm | 8 ++- lib/NGCP/Panel/Form/Sound/CustomerSet.pm | 66 +++++++++++++++++++++ lib/NGCP/Panel/Form/Sound/ResellerSet.pm | 8 ++- 4 files changed, 140 insertions(+), 17 deletions(-) create mode 100644 lib/NGCP/Panel/Form/Sound/CustomerSet.pm diff --git a/lib/NGCP/Panel/Controller/Sound.pm b/lib/NGCP/Panel/Controller/Sound.pm index 2736a82a5c..247d2594b8 100644 --- a/lib/NGCP/Panel/Controller/Sound.pm +++ b/lib/NGCP/Panel/Controller/Sound.pm @@ -6,13 +6,14 @@ BEGIN { extends 'Catalyst::Controller'; } use NGCP::Panel::Form::Sound::AdminSet; use NGCP::Panel::Form::Sound::ResellerSet; +use NGCP::Panel::Form::Sound::CustomerSet; use NGCP::Panel::Form::Sound::File; use File::Type; use NGCP::Panel::Utils::XMLDispatcher; use NGCP::Panel::Utils::Sounds; use NGCP::Panel::Utils::Navigation; -sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) :AllowedRole(reseller) { +sub auto :Private { my ($self, $c) = @_; $c->log->debug(__PACKAGE__ . '::auto'); NGCP::Panel::Utils::Navigation::check_redirect_chain(c => $c); @@ -23,16 +24,29 @@ sub sets_list :Chained('/') :PathPart('sound') :CaptureArgs(0) { my ( $self, $c ) = @_; my $sets_rs = $c->model('DB')->resultset('voip_sound_sets'); - unless($c->user->is_superuser) { - $sets_rs = $sets_rs->search({ reseller_id => $c->user->reseller_id }); - } - $c->stash->{soundset_dt_columns} = NGCP::Panel::Utils::Datatables::set_columns($c, [ + my $dt_fields = [ { name => 'id', search => 1, title => '#' }, - { name => 'reseller.name', search => 1, title => 'Reseller' }, { name => 'name', search => 1, title => 'Name' }, { name => 'description', search => 1, title => 'Description' }, - ]); + ]; + + if($c->user->roles eq "admin") { + splice @{ $dt_fields }, 1, 0, + { name => 'reseller.name', search => 1, title => 'Reseller' }; + splice @{ $dt_fields }, 2, 0, + { name => 'contract.contact.email', search => 1, title => 'Customer' }; + } elsif($c->user->roles eq "reseller") { + splice @{ $dt_fields }, 1, 0, + { name => 'contract.contact.email', search => 1, title => 'Customer' }; + $sets_rs = $sets_rs->search({ reseller_id => $c->user->reseller_id }); + } elsif($c->user->roles eq "subscriberadmin") { + $sets_rs = $sets_rs->search({ contract_id => $c->user->account_id }); + } else { + $c->detach('/denied_page'); + } + + $c->stash->{soundset_dt_columns} = NGCP::Panel::Utils::Datatables::set_columns($c, $dt_fields); $c->stash(sets_rs => $sets_rs); $c->stash(template => 'sound/list.tt'); @@ -73,11 +87,14 @@ sub edit :Chained('base') :PathPart('edit') { my $form; my $params = { $c->stash->{set_result}->get_inflated_columns }; $params->{reseller}{id} = delete $params->{reseller_id}; + $params->{contract}{id} = delete $params->{contract_id}; $params = $params->merge($c->session->{created_objects}); - if($c->user->is_superuser) { + if($c->user->roles eq "admin") { $form = NGCP::Panel::Form::Sound::AdminSet->new; - } else { + } elsif($c->user->roles eq "reseller") { $form = NGCP::Panel::Form::Sound::ResellerSet->new; + } else { + $form = NGCP::Panel::Form::Sound::CustomerSet->new; } $form->process( posted => $posted, @@ -94,12 +111,15 @@ sub edit :Chained('base') :PathPart('edit') { ); if($posted && $form->validated) { try { - if($c->user->is_superuser) { + if($c->user->roles eq "admin") { $form->values->{reseller_id} = $form->values->{reseller}{id}; + $form->values->{contract_id} = $form->values->{contract}{id} // undef; } delete $form->values->{reseller}; + delete $form->values->{contract}; $c->stash->{set_result}->update($form->values); delete $c->session->{created_objects}->{reseller}; + delete $c->session->{created_objects}->{contract}; $c->flash(messages => [{type => 'success', text => 'Sound set successfully updated'}]); } catch($e) { $c->log->error("failed to update sound set: $e"); @@ -134,10 +154,12 @@ sub create :Chained('sets_list') :PathPart('create') :Args(0) { my $form; my $params = {}; $params = $params->merge($c->session->{created_objects}); - if($c->user->is_superuser) { + if($c->user->roles eq "admin") { $form = NGCP::Panel::Form::Sound::AdminSet->new; - } else { + } elsif($c->user->roles eq "reseller") { $form = NGCP::Panel::Form::Sound::ResellerSet->new; + } else { + $form = NGCP::Panel::Form::Sound::CustomerSet->new; } $form->process( posted => $posted, @@ -149,17 +171,25 @@ sub create :Chained('sets_list') :PathPart('create') :Args(0) { form => $form, fields => { 'reseller.create' => $c->uri_for('/reseller/create'), + 'contract.create' => $c->uri_for_action('/contract/customer_create'), }, back_uri => $c->req->uri, ); if($posted && $form->validated) { try { - if($c->user->is_superuser) { + if($c->user->roles eq "admin") { $form->values->{reseller_id} = $form->values->{reseller}{id}; - delete $form->values->{reseller}; - } else { + $form->values->{contract_id} = $form->values->{contract}{id} // undef; + } elsif($c->user->roles eq "reseller") { $form->values->{reseller_id} = $c->user->reseller_id; + $form->values->{contract_id} = $form->values->{contract}{id} // undef; + } else { + $form->values->{reseller_id} = $c->user->contract->contact->reseller_id; + $form->values->{contract_id} = $c->user->account_id; } + delete $form->values->{reseller}; + delete $form->values->{contract}; + $c->stash->{sets_rs}->create($form->values); delete $c->session->{created_objects}->{reseller}; $c->flash(messages => [{type => 'success', text => 'Sound set successfully created'}]); @@ -180,6 +210,7 @@ sub handles_list :Chained('base') :PathPart('handles') :CaptureArgs(0) { my ( $self, $c ) = @_; my $files_rs = $c->stash->{set_result}->voip_sound_files; + $c->stash(files_rs => $files_rs); $c->stash(handles_base_uri => $c->uri_for_action("/sound/handles_root", [$c->req->captures->[0]])); @@ -202,6 +233,13 @@ sub handles_list :Chained('base') :PathPart('handles') :CaptureArgs(0) { ], ], }); + + if($c->stash->{set_result}->contract_id) { + $handles_rs = $handles_rs->search({ 'groups.name' => { '=' => 'pbx' } }); + } else { + $handles_rs = $handles_rs->search({ 'groups.name' => { '!=' => 'pbx' } }); + } + unless($c->config->{features}->{cloudpbx}) { $handles_rs = $handles_rs->search({ 'groups.name' => { '!=' => 'pbx' } }); } @@ -225,6 +263,7 @@ sub handles_list :Chained('base') :PathPart('handles') :CaptureArgs(0) { push $groups{ $handle->get_column('groupname') }, $handle; } $c->stash(sound_groups => \%groups); + $c->stash(handles_rs => $handles_rs); $c->stash(has_edit => 1); $c->stash(has_delete => 1); @@ -242,6 +281,12 @@ sub handles_base :Chained('handles_list') :PathPart('') :CaptureArgs(1) { $c->flash(messages => [{type => 'error', text => 'Invalid sound handle id detected'}]); NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{handles_base_uri}); } + my @tmph = $c->stash->{handles_rs}->all; + use Data::Printer; p @tmph; + unless($c->stash->{handles_rs}->find({ 'handles.id' => $handle_id })) { + $c->flash(messages => [{type => 'error', text => 'Sound handle id does not exist'}]); + NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{handles_base_uri}); + } my $res = $c->stash->{files_rs}->find_or_create(handle_id => $handle_id); unless(defined $res ) { diff --git a/lib/NGCP/Panel/Form/Sound/AdminSet.pm b/lib/NGCP/Panel/Form/Sound/AdminSet.pm index d3c0eb3ef1..37ed30b3fc 100644 --- a/lib/NGCP/Panel/Form/Sound/AdminSet.pm +++ b/lib/NGCP/Panel/Form/Sound/AdminSet.pm @@ -9,10 +9,16 @@ has_field 'reseller' => ( not_nullable => 1, ); +has_field 'contract' => ( + type => '+NGCP::Panel::Field::CustomerContract', + label => 'Customer', + not_nullable => 0, +); + has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], - render_list => [qw/reseller name description/], + render_list => [qw/reseller contract name description/], ); 1; diff --git a/lib/NGCP/Panel/Form/Sound/CustomerSet.pm b/lib/NGCP/Panel/Form/Sound/CustomerSet.pm new file mode 100644 index 0000000000..47eb467759 --- /dev/null +++ b/lib/NGCP/Panel/Form/Sound/CustomerSet.pm @@ -0,0 +1,66 @@ +package NGCP::Panel::Form::Sound::CustomerSet; + +use HTML::FormHandler::Moose; +extends 'HTML::FormHandler'; +use Moose::Util::TypeConstraints; + +use HTML::FormHandler::Widget::Block::Bootstrap; + +has '+widget_wrapper' => ( default => 'Bootstrap' ); +has_field 'submitid' => ( type => 'Hidden' ); +sub build_render_list {[qw/submitid fields actions/]} +sub build_form_element_class {[qw(form-horizontal)]} + +has_field 'name' => ( + type => 'Text', + label => 'Name', + required => 1, +); + +has_field 'description' => ( + type => 'Text', +); + +has_field 'save' => ( + type => 'Submit', + value => 'Save', + element_class => [qw/btn btn-primary/], + label => '', +); + +has_block 'fields' => ( + tag => 'div', + class => [qw/modal-body/], + render_list => [qw/name description/], +); + +has_block 'actions' => ( + tag => 'div', + class => [qw/modal-footer/], + render_list => [qw/save/], +); + +1; + +=head1 NAME + +NGCP::Panel::Form::SoundSet + +=head1 DESCRIPTION + +Form to modify a provisioning.voip_sound_sets row. + +=head1 METHODS + +=head1 AUTHOR + +Gerhard Jungwirth + +=head1 LICENSE + +This library is free software. You can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Form/Sound/ResellerSet.pm b/lib/NGCP/Panel/Form/Sound/ResellerSet.pm index 0a21059f39..39e7545c10 100644 --- a/lib/NGCP/Panel/Form/Sound/ResellerSet.pm +++ b/lib/NGCP/Panel/Form/Sound/ResellerSet.pm @@ -11,6 +11,12 @@ has_field 'submitid' => ( type => 'Hidden' ); sub build_render_list {[qw/submitid fields actions/]} sub build_form_element_class {[qw(form-horizontal)]} +has_field 'contract' => ( + type => '+NGCP::Panel::Field::CustomerContract', + label => 'Customer', + not_nullable => 0, +); + has_field 'name' => ( type => 'Text', label => 'Name', @@ -31,7 +37,7 @@ has_field 'save' => ( has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], - render_list => [qw/name description/], + render_list => [qw/contract name description/], ); has_block 'actions' => (