From 64519f8c77b4556d18a28458e0a99e13e50d64cd Mon Sep 17 00:00:00 2001 From: Bozhidar Slaveykov Date: Thu, 4 Apr 2024 16:31:37 +0300 Subject: [PATCH] update --- web/app/Models/Customer.php | 5 -- web/app/Policies/CustomerPolicy.php | 71 ++++++++++++++++++++++++ web/app/Providers/AppServiceProvider.php | 4 ++ 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 web/app/Policies/CustomerPolicy.php diff --git a/web/app/Models/Customer.php b/web/app/Models/Customer.php index b0bc222..25925cb 100644 --- a/web/app/Models/Customer.php +++ b/web/app/Models/Customer.php @@ -33,11 +33,6 @@ class Customer extends Model }); static::deleting(function ($model) { - $findWebistes = Website::where('customer_id', $model->id)->count(); - if ($findWebistes > 0) { - throw new ('Customer has websites, please delete them first.'); - return false; - } event(new ModelCustomerDeleting($model)); }); diff --git a/web/app/Policies/CustomerPolicy.php b/web/app/Policies/CustomerPolicy.php new file mode 100644 index 0000000..dab67a1 --- /dev/null +++ b/web/app/Policies/CustomerPolicy.php @@ -0,0 +1,71 @@ +id)->count(); + if ($findWebsites > 0) { + return Response::deny('Customer has websites, please delete them first.'); + } + return Response::allow(); + } + + /** + * Determine whether the user can restore the model. + */ + public function restore(User $user, Customer $customer): bool + { + // + } + + /** + * Determine whether the user can permanently delete the model. + */ + public function forceDelete(User $user, Customer $customer): bool + { + // + } +} diff --git a/web/app/Providers/AppServiceProvider.php b/web/app/Providers/AppServiceProvider.php index fe8f51f..c769371 100644 --- a/web/app/Providers/AppServiceProvider.php +++ b/web/app/Providers/AppServiceProvider.php @@ -10,8 +10,10 @@ use App\Listeners\ModelCustomerCreatedListener; use App\Listeners\ModelCustomerDeletingListener; use App\Listeners\ModelWebsiteCreatedListener; use App\Listeners\ModelWebsiteDeletingListener; +use App\Policies\CustomerPolicy; use Filament\Facades\Filament; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -30,6 +32,8 @@ class AppServiceProvider extends ServiceProvider public function boot(): void { + Gate::define('delete-customer', [CustomerPolicy::class, 'delete']); + Filament::serving(function () { // Using Vite Filament::registerViteTheme('resources/css/app.css');