From a9b68ae367522b555874762c17a953485c21b36e Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Mon, 5 Aug 2013 16:29:29 +0200 Subject: [PATCH] Fix contract/contact editing from within reseller. --- lib/NGCP/Panel/Controller/Contact.pm | 53 +++++++++++++++++++++------ lib/NGCP/Panel/Controller/Reseller.pm | 4 ++ share/templates/reseller/details.tt | 13 ++++++- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Contact.pm b/lib/NGCP/Panel/Controller/Contact.pm index 2e11ae406c..33729ba761 100644 --- a/lib/NGCP/Panel/Controller/Contact.pm +++ b/lib/NGCP/Panel/Controller/Contact.pm @@ -99,37 +99,62 @@ sub base :Chained('list_contact') :PathPart('') :CaptureArgs(1) { my ($self, $c, $contact_id) = @_; unless($contact_id && $contact_id->is_int) { - $c->flash(messages => [{type => 'error', text => 'Invalid contact id detected!'}]); + $c->flash(messages => [{type => 'error', text => 'Invalid contact id detected'}]); NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/contact')); } my $res = $c->stash->{contacts}; $c->stash(contact => $res->find($contact_id)); + unless($c->stash->{contact}) { + $c->flash(messages => [{type => 'error', text => 'Contact not found'}]); + NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/contact')); + } } sub edit :Chained('base') :PathPart('edit') :Args(0) { - my ($self, $c) = @_; + my ($self, $c, $no_reseller) = @_; my $posted = ($c->request->method eq 'POST'); - my $form = NGCP::Panel::Form::Contact->new; + my $form; + my $params = { $c->stash->{contact}->get_inflated_columns }; + $params = $params->merge($c->session->{created_objects}); + if($c->user->is_superuser && $no_reseller) { + $form = NGCP::Panel::Form::Contact::Reseller->new; + $params->{reseller}{id} = $c->user->reseller_id; + } elsif($c->user->is_superuser) { + $form = NGCP::Panel::Form::Contact::Admin->new; + } else { + $form = NGCP::Panel::Form::Contact::Reseller->new; + } + $form->process( posted => $posted, params => $c->request->params, - item => $c->stash->{contact}, - action => $c->uri_for($c->stash->{contact}->id, 'edit'), + item => $params, + ); + NGCP::Panel::Utils::Navigation::check_form_buttons( + c => $c, + form => $form, + fields => { + 'reseller.create' => $c->uri_for('/reseller/create'), + }, + back_uri => $c->req->uri, ); - # TODO: use values instead of params, chain reseller creation, add check_form_buttons if($posted && $form->validated) { try { - delete $form->params->{submitid}; - delete $form->params->{save}; - $c->stash->{contact}->update($form->params); + if($c->user->is_superuser && $no_reseller) { + delete $form->values->{reseller}; + } elsif($c->user->is_superuser) { + $form->values->{reseller_id} = $form->values->{reseller}{id}; + } + delete $form->values->{reseller}; + $c->stash->{contact}->update($form->values); $c->flash(messages => [{type => 'success', text => 'Contact successfully changed'}]); - NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/contact')); + delete $c->session->{created_objects}->{reseller}; } catch($e) { $c->log->error("failed to update contact: $e"); $c->flash(messages => [{type => 'error', text => 'Failed to update contact'}]); - NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/contact')); } + NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/contact')); } $c->stash( @@ -138,6 +163,12 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { ); } +sub edit_without_reseller :Chained('base') :PathPart('edit/noreseller') :Args(0) { + my ($self, $c) = @_; + + $self->edit($c, 1); +} + sub delete :Chained('base') :PathPart('delete') :Args(0) { my ($self, $c) = @_; diff --git a/lib/NGCP/Panel/Controller/Reseller.pm b/lib/NGCP/Panel/Controller/Reseller.pm index c466565113..df9a190a9f 100644 --- a/lib/NGCP/Panel/Controller/Reseller.pm +++ b/lib/NGCP/Panel/Controller/Reseller.pm @@ -141,6 +141,10 @@ sub base :Chained('list_reseller') :PathPart('') :CaptureArgs(1) { ]); $c->stash(reseller => $c->stash->{resellers}->search_rs({ id => $reseller_id })); + unless($c->stash->{reseller}->first) { + $c->flash(messages => [{type => 'error', text => 'Reseller not found'}]); + NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/reseller')); + } } sub reseller_contacts :Chained('base') :PathPart('contacts/ajax') :Args(0) { diff --git a/share/templates/reseller/details.tt b/share/templates/reseller/details.tt index 2360e7b2c8..6a75a3d37b 100644 --- a/share/templates/reseller/details.tt +++ b/share/templates/reseller/details.tt @@ -44,7 +44,6 @@ UNLESS c.user.read_only; helper.dt_buttons = [ { name = 'Edit', uri = "/reseller/'+full.id+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' }, - { name = 'Terminate', uri = "/reseller/'+full.id+'/terminate", class = 'btn-small btn-secondary', icon = 'icon-remove' }, ]; END; @@ -73,6 +72,12 @@ helper.ajax_uri = c.uri_for_action('/reseller/reseller_contracts', c.req.captures ); helper.base_uri = c.uri_for_action('/contract/root'); + UNLESS c.user.read_only; + helper.dt_buttons = [ + { name = 'Edit', uri = "/contract/'+full.id+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' }, + ]; + END; + PROCESS 'helpers/datatables.tt'; -%] @@ -98,6 +103,12 @@ helper.ajax_uri = c.uri_for_action('/reseller/reseller_contacts', c.req.captures ); helper.base_uri = c.uri_for_action('/contact/root'); + UNLESS c.user.read_only; + helper.dt_buttons = [ + { name = 'Edit', uri = "/contact/'+full.id+'/edit/noreseller", class = 'btn-small btn-primary', icon = 'icon-edit' }, + ]; + END; + PROCESS 'helpers/datatables.tt'; -%]