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. 9
      resources/lang/en/customer.php
  3. 9
      resources/lang/id/customer.php
  4. 6
      resources/views/customers/edit.blade.php
  5. 41
      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\Partners\Customer $customer
* @param int $dependentRecordsCount
*
* @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;
}
}

9
resources/lang/en/customer.php

@ -32,8 +32,9 @@ return [
'projects_count' => 'Projects count',
// Relations
'projects' => 'Project List',
'payments' => 'Payment History',
'subscriptions' => 'Subscription List',
'invoices' => 'Invoice List',
'projects' => 'Project List',
'payments' => 'Payment History',
'subscriptions' => 'Subscription List',
'subscriptions_count' => 'Subscriptions Count',
'invoices' => 'Invoice List',
];

9
resources/lang/id/customer.php

@ -32,8 +32,9 @@ return [
'projects_count' => 'Jml Project',
// Relations
'projects' => 'List Project',
'payments' => 'History Pembayaran',
'subscriptions' => 'List Langganan',
'invoices' => 'List Invoice',
'projects' => 'List Project',
'payments' => 'History Pembayaran',
'subscriptions' => 'List Langganan',
'subscriptions_count' => 'Jumlah Langganan',
'invoices' => 'List Invoice',
];

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

@ -10,8 +10,9 @@
{{ $customer->name }} <small>{{ trans('customer.edit') }}</small>
</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']) !!}
<div class="row">
<div class="col-md-8 col-md-offset-2">
@ -50,4 +51,5 @@
</div>
</div>
{!! Form::close() !!}
@endif
@endsection

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

@ -1,4 +1,7 @@
@if (Request::get('action') == 'delete' && $customer)
@php
$dependentRecordsCount = 0;
@endphp
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
@ -8,29 +11,37 @@
<p>{{ $customer->name }}</p>
<label class="control-label">{{ trans('contact.email') }}</label>
<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>
<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>
<p>{{ $customer->notes }}</p>
{!! $errors->first('customer_id', '<span class="form-error small">:message</span>') !!}
</div>
<hr style="margin:0">
<div class="panel-body">{{ trans('app.delete_confirm') }}</div>
@if ($dependentRecordsCount)
<div class="panel-body">{{ trans('customer.undeleteable') }}</div>
@else
<div class="panel-body">{{ trans('app.delete_confirm') }}</div>
@endif
<div class="panel-footer">
{!! FormField::delete(
['route'=>['customers.destroy',$customer->id]],
trans('app.delete_confirm_button'),
['class'=>'btn btn-danger'],
[
'customer_id' => $customer->id,
'page' => request('page'),
'q' => request('q'),
]
) !!}
@can('delete', [$customer, $dependentRecordsCount])
{!! FormField::delete(
['route'=>['customers.destroy',$customer->id]],
trans('app.delete_confirm_button'),
['class'=>'btn btn-danger'],
[
'customer_id' => $customer->id,
'page' => request('page'),
'q' => request('q'),
]
) !!}
@endcan
{{ link_to_route('customers.edit', trans('app.cancel'), [$customer->id], ['class' => 'btn btn-default']) }}
</div>
</div>

12
tests/Unit/Policies/CustomerPolicyTest.php

@ -3,7 +3,7 @@
namespace Tests\Unit\Policies;
use App\Entities\Partners\Customer;
use Tests\TestCase as TestCase;
use Tests\TestCase;
/**
* Customer Policy Test.
@ -54,4 +54,14 @@ class CustomerPolicyTest extends TestCase
$this->assertTrue($admin->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