From ae4c3660d7f740db387ccda9f7704a56562b4ded Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Mon, 24 Jun 2013 15:33:22 +0200 Subject: [PATCH] use different Forms for BillingProfile when admin/reseller is logged in reseller cannot create billing_profiles for other resellers/admins... --- lib/NGCP/Panel/Controller/Billing.pm | 40 ++++++++++++++----- lib/NGCP/Panel/Form/BillingProfile_admin.pm | 21 ++++++++++ ...gProfile.pm => BillingProfile_reseller.pm} | 18 +++++++-- 3 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 lib/NGCP/Panel/Form/BillingProfile_admin.pm rename lib/NGCP/Panel/Form/{BillingProfile.pm => BillingProfile_reseller.pm} (86%) diff --git a/lib/NGCP/Panel/Controller/Billing.pm b/lib/NGCP/Panel/Controller/Billing.pm index 3ea17d5bcc..f4dda7c7dd 100644 --- a/lib/NGCP/Panel/Controller/Billing.pm +++ b/lib/NGCP/Panel/Controller/Billing.pm @@ -6,7 +6,8 @@ use I18N::Langinfo qw(langinfo DAY_1 DAY_2 DAY_3 DAY_4 DAY_5 DAY_6 DAY_7); BEGIN { extends 'Catalyst::Controller'; } -use NGCP::Panel::Form::BillingProfile; +use NGCP::Panel::Form::BillingProfile_admin; +use NGCP::Panel::Form::BillingProfile_reseller; use NGCP::Panel::Form::BillingFee; use NGCP::Panel::Form::BillingZone; use NGCP::Panel::Form::BillingPeaktimeWeekdays; @@ -27,12 +28,29 @@ sub profile_list :Chained('/') :PathPart('billing') :CaptureArgs(0) { my ( $self, $c ) = @_; NGCP::Panel::Utils::check_redirect_chain(c => $c); + + my $dispatch_to = '_profile_resultset_' . $c->user->auth_realm; + my $profiles_rs = $self->$dispatch_to($c); + $c->stash(profiles_rs => $profiles_rs); $c->stash(has_edit => 1); $c->stash(has_delete => 0); $c->stash(template => 'billing/list.tt'); } +sub _profile_resultset_admin { + my ($self, $c) = @_; + my $rs = $c->model('billing')->resultset('billing_profiles'); + return $rs; +} + +sub _profile_resultset_reseller { + my ($self, $c) = @_; + my $rs = $c->model('billing')->resultset('admins') + ->find($c->user->id)->reseller->billing_profiles; + return $rs; +} + sub root :Chained('profile_list') :PathPart('') :Args(0) { my ($self, $c) = @_; } @@ -40,7 +58,7 @@ sub root :Chained('profile_list') :PathPart('') :Args(0) { sub ajax :Chained('profile_list') :PathPart('ajax') :Args(0) { my ($self, $c) = @_; - my $resultset = $c->model('billing')->resultset('billing_profiles'); + my $resultset = $c->stash->{profiles_rs}; $c->forward( "/ajax_process_resultset", [$resultset, ["id", "name"], @@ -58,7 +76,7 @@ sub base :Chained('profile_list') :PathPart('') :CaptureArgs(1) { return; } - my $res = $c->model('billing')->resultset('billing_profiles')->find($profile_id); + my $res = $c->stash->{profiles_rs}->find($profile_id); unless(defined($res)) { $c->flash(messages => [{type => 'error', text => 'Billing Profile does not exist!'}]); $c->response->redirect($c->uri_for()); @@ -72,15 +90,15 @@ sub edit :Chained('base') :PathPart('edit') { my ($self, $c) = @_; my $posted = ($c->request->method eq 'POST'); - my $form = NGCP::Panel::Form::BillingProfile->new; + my $dispatch_to = 'NGCP::Panel::Form::BillingProfile_' . $c->user->auth_realm; + my $form = $dispatch_to->new; $form->process( - posted => 1, - params => $posted ? $c->request->params : $c->stash->{profile}, + posted => $posted, + params => $c->request->params, action => $c->uri_for($c->stash->{profile}->{id}, 'edit'), + item => $c->stash->{profile_result}, ); if($posted && $form->validated) { - $c->model('billing')->resultset('billing_profiles') - ->find($form->field('id')->value)->update($form->fif); $c->flash(messages => [{type => 'success', text => 'Billing Profile successfully changed!'}]); $c->response->redirect($c->uri_for()); @@ -94,15 +112,15 @@ sub edit :Chained('base') :PathPart('edit') { sub create :Chained('profile_list') :PathPart('create') :Args(0) { my ($self, $c) = @_; - my $form = NGCP::Panel::Form::BillingProfile->new; + my $dispatch_to = 'NGCP::Panel::Form::BillingProfile_' . $c->user->auth_realm; + my $form = $dispatch_to->new; $form->process( posted => ($c->request->method eq 'POST'), params => $c->request->params, action => $c->uri_for('create'), + item => $c->stash->{profiles_rs}->new_result({}), ); if($form->validated) { - $c->model('billing')->resultset('billing_profiles')->create( - $form->fif() ); $c->flash(messages => [{type => 'success', text => 'Billing profile successfully created!'}]); $c->response->redirect($c->uri_for()); return; diff --git a/lib/NGCP/Panel/Form/BillingProfile_admin.pm b/lib/NGCP/Panel/Form/BillingProfile_admin.pm new file mode 100644 index 0000000000..5d1fc3a9a9 --- /dev/null +++ b/lib/NGCP/Panel/Form/BillingProfile_admin.pm @@ -0,0 +1,21 @@ +package NGCP::Panel::Form::BillingProfile_admin; + +use HTML::FormHandler::Moose; +extends 'NGCP::Panel::Form::BillingProfile_reseller'; + +has_field 'reseller' => ( + type => '+NGCP::Panel::Field::Reseller', +); + +has_block 'fields' => ( + tag => 'div', + class => [qw/modal-body/], + render_list => [qw/handle name interval_charge interval_free_time interval_free_cash + fraud_interval_limit fraud_interval_lock fraud_interval_notify + fraud_daily_limit fraud_daily_lock fraud_daily_notify + currency vat_rate vat_included reseller id/], +); + + +1; +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Form/BillingProfile.pm b/lib/NGCP/Panel/Form/BillingProfile_reseller.pm similarity index 86% rename from lib/NGCP/Panel/Form/BillingProfile.pm rename to lib/NGCP/Panel/Form/BillingProfile_reseller.pm index 051f2d6511..9be38030ac 100644 --- a/lib/NGCP/Panel/Form/BillingProfile.pm +++ b/lib/NGCP/Panel/Form/BillingProfile_reseller.pm @@ -1,7 +1,7 @@ -package NGCP::Panel::Form::BillingProfile; +package NGCP::Panel::Form::BillingProfile_reseller; use HTML::FormHandler::Moose; -extends 'HTML::FormHandler'; +extends 'HTML::FormHandler::Model::DBIC'; use Moose::Util::TypeConstraints; use HTML::FormHandler::Widget::Block::Bootstrap; @@ -20,6 +20,11 @@ has_field 'name' => ( maxlength => 31, ); +has_field 'handle' => ( + type => 'Text', + required => 1, +); + has_field 'interval_charge' => ( type => 'Money', ); @@ -102,7 +107,7 @@ has_field 'save' => ( has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], - render_list => [qw/name interval_charge interval_free_time interval_free_cash + render_list => [qw/handle name interval_charge interval_free_time interval_free_cash fraud_interval_limit fraud_interval_lock fraud_interval_notify fraud_daily_limit fraud_daily_lock fraud_daily_notify currency vat_rate vat_included id/], @@ -114,5 +119,12 @@ has_block 'actions' => ( render_list => [qw/save/], ); +before 'update_model' => sub { + my $self = shift; + foreach my $val(values $self->value) { + $val = '' unless defined($val); + } +}; + 1; # vim: set tabstop=4 expandtab: