Browse Source

Prevent admin to delete customer that has project

pull/7/head
Nafies Luthfi 8 years ago
parent
commit
813ece1eee
  1. 5
      app/Policies/Partners/CustomerPolicy.php
  2. 1
      resources/lang/en/customer.php
  3. 1
      resources/lang/id/customer.php
  4. 6
      resources/views/customers/edit.blade.php
  5. 19
      resources/views/customers/forms.blade.php
  6. 12
      tests/Unit/Policies/CustomerPolicyTest.php

5
app/Policies/Partners/CustomerPolicy.php

@ -59,11 +59,12 @@ class CustomerPolicy
* *
* @param \App\Entities\Users\User $user * @param \App\Entities\Users\User $user
* @param \App\Entities\Partners\Customer $customer * @param \App\Entities\Partners\Customer $customer
* @param int $dependentRecordsCount
* *
* @return mixed * @return mixed
*/ */
public function delete(User $user, Customer $customer)
public function delete(User $user, Customer $customer, int $dependentRecordsCount = 0)
{ {
return $this->view($user, $customer);
return $user->hasRole('admin') && $dependentRecordsCount == 0;
} }
} }

1
resources/lang/en/customer.php

@ -35,5 +35,6 @@ return [
'projects' => 'Project List', 'projects' => 'Project List',
'payments' => 'Payment History', 'payments' => 'Payment History',
'subscriptions' => 'Subscription List', 'subscriptions' => 'Subscription List',
'subscriptions_count' => 'Subscriptions Count',
'invoices' => 'Invoice List', 'invoices' => 'Invoice List',
]; ];

1
resources/lang/id/customer.php

@ -35,5 +35,6 @@ return [
'projects' => 'List Project', 'projects' => 'List Project',
'payments' => 'History Pembayaran', 'payments' => 'History Pembayaran',
'subscriptions' => 'List Langganan', 'subscriptions' => 'List Langganan',
'subscriptions_count' => 'Jumlah Langganan',
'invoices' => 'List Invoice', 'invoices' => 'List Invoice',
]; ];

6
resources/views/customers/edit.blade.php

@ -10,8 +10,9 @@
{{ $customer->name }} <small>{{ trans('customer.edit') }}</small> {{ $customer->name }} <small>{{ trans('customer.edit') }}</small>
</h1> </h1>
@includeWhen(Request::has('action'), 'customers.forms')
@if (Request::has('action'))
@include('customers.forms')
@else
{!! Form::model($customer, ['route' => ['customers.update', $customer->id],'method' => 'patch']) !!} {!! Form::model($customer, ['route' => ['customers.update', $customer->id],'method' => 'patch']) !!}
<div class="row"> <div class="row">
<div class="col-md-8 col-md-offset-2"> <div class="col-md-8 col-md-offset-2">
@ -50,4 +51,5 @@
</div> </div>
</div> </div>
{!! Form::close() !!} {!! Form::close() !!}
@endif
@endsection @endsection

19
resources/views/customers/forms.blade.php

@ -1,4 +1,7 @@
@if (Request::get('action') == 'delete' && $customer) @if (Request::get('action') == 'delete' && $customer)
@php
$dependentRecordsCount = 0;
@endphp
<div class="row"> <div class="row">
<div class="col-md-4 col-md-offset-4"> <div class="col-md-4 col-md-offset-4">
<div class="panel panel-default"> <div class="panel panel-default">
@ -8,19 +11,26 @@
<p>{{ $customer->name }}</p> <p>{{ $customer->name }}</p>
<label class="control-label">{{ trans('contact.email') }}</label> <label class="control-label">{{ trans('contact.email') }}</label>
<p>{{ $customer->email }}</p> <p>{{ $customer->email }}</p>
<label class="control-label">{{ trans('contact.phone') }}</label>
<p>{{ $customer->phone }}</p>
<label class="control-label">{{ trans('address.address') }}</label>
<p>{{ $customer->address }}</p>
<label class="control-label">{{ trans('app.status') }}</label> <label class="control-label">{{ trans('app.status') }}</label>
<p>{{ $customer->is_active }}</p> <p>{{ $customer->is_active }}</p>
<label class="control-label">{{ trans('customer.projects_count') }}</label>
<p>{{ $projectsCount = $customer->projects()->count() }}</p>
@php $dependentRecordsCount += $projectsCount; @endphp
<label class="control-label">{{ trans('customer.subscriptions_count') }}</label>
<p>{{ $subscriptionsCount = $customer->subscriptions()->count() }}</p>
@php $dependentRecordsCount += $subscriptionsCount; @endphp
<label class="control-label">{{ trans('app.notes') }}</label> <label class="control-label">{{ trans('app.notes') }}</label>
<p>{{ $customer->notes }}</p> <p>{{ $customer->notes }}</p>
{!! $errors->first('customer_id', '<span class="form-error small">:message</span>') !!} {!! $errors->first('customer_id', '<span class="form-error small">:message</span>') !!}
</div> </div>
<hr style="margin:0"> <hr style="margin:0">
@if ($dependentRecordsCount)
<div class="panel-body">{{ trans('customer.undeleteable') }}</div>
@else
<div class="panel-body">{{ trans('app.delete_confirm') }}</div> <div class="panel-body">{{ trans('app.delete_confirm') }}</div>
@endif
<div class="panel-footer"> <div class="panel-footer">
@can('delete', [$customer, $dependentRecordsCount])
{!! FormField::delete( {!! FormField::delete(
['route'=>['customers.destroy',$customer->id]], ['route'=>['customers.destroy',$customer->id]],
trans('app.delete_confirm_button'), trans('app.delete_confirm_button'),
@ -31,6 +41,7 @@
'q' => request('q'), 'q' => request('q'),
] ]
) !!} ) !!}
@endcan
{{ link_to_route('customers.edit', trans('app.cancel'), [$customer->id], ['class' => 'btn btn-default']) }} {{ link_to_route('customers.edit', trans('app.cancel'), [$customer->id], ['class' => 'btn btn-default']) }}
</div> </div>
</div> </div>

12
tests/Unit/Policies/CustomerPolicyTest.php

@ -3,7 +3,7 @@
namespace Tests\Unit\Policies; namespace Tests\Unit\Policies;
use App\Entities\Partners\Customer; use App\Entities\Partners\Customer;
use Tests\TestCase as TestCase;
use Tests\TestCase;
/** /**
* Customer Policy Test. * Customer Policy Test.
@ -54,4 +54,14 @@ class CustomerPolicyTest extends TestCase
$this->assertTrue($admin->can('delete', $customer)); $this->assertTrue($admin->can('delete', $customer));
$this->assertFalse($worker->can('delete', $customer)); $this->assertFalse($worker->can('delete', $customer));
} }
/** @test */
public function admin_cannot_delete_customer_if_it_has_dependent_records()
{
$admin = $this->createUser('admin');
$customer = factory(Customer::class)->create();
$this->assertTrue($admin->can('delete', [$customer, 0]));
$this->assertFalse($admin->can('delete', [$customer, 1]));
}
} }
Loading…
Cancel
Save