From 19877244cdee66e8dee7fb9e76fc65dec1b2dab7 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 22 Nov 2023 18:03:26 +0000 Subject: [PATCH] Added/refactorerd isDeletable() Signed-off-by: snipe --- app/Models/AssetModel.php | 16 ++++++++++++++++ app/Models/Category.php | 3 ++- app/Models/Manufacturer.php | 3 ++- app/Models/User.php | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.php index f94c6f8eac..d0e47e1cf1 100755 --- a/app/Models/AssetModel.php +++ b/app/Models/AssetModel.php @@ -6,6 +6,7 @@ use App\Models\Traits\Searchable; use App\Presenters\Presentable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Storage; use Watson\Validating\ValidatingTrait; @@ -188,6 +189,21 @@ class AssetModel extends SnipeModel return false; } + + /** + * Checks if the model is deletable + * + * @author A. Gianotto + * @since [v6.3.4] + * @return bool + */ + public function isDeletable() + { + return Gate::allows('delete', $this) + && ($this->assets_count == 0) + && ($this->deleted_at == ''); + } + /** * Get uploads for this model * diff --git a/app/Models/Category.php b/app/Models/Category.php index d880ba899d..17e41da1fc 100755 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -100,7 +100,8 @@ class Category extends SnipeModel { return Gate::allows('delete', $this) - && ($this->itemCount() == 0); + && ($this->itemCount() == 0) + && ($this->deleted_at == ''); } /** diff --git a/app/Models/Manufacturer.php b/app/Models/Manufacturer.php index da4f26b02f..5408d50c36 100755 --- a/app/Models/Manufacturer.php +++ b/app/Models/Manufacturer.php @@ -77,7 +77,8 @@ class Manufacturer extends SnipeModel && ($this->assets()->count() === 0) && ($this->licenses()->count() === 0) && ($this->consumables()->count() === 0) - && ($this->accessories()->count() === 0); + && ($this->accessories()->count() === 0) + && ($this->deleted_at == ''); } public function assets() diff --git a/app/Models/User.php b/app/Models/User.php index 8011f94ff4..85a17b16d5 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -17,6 +17,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Facades\Gate; use Laravel\Passport\HasApiTokens; use Watson\Validating\ValidatingTrait; @@ -201,6 +202,23 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo return $this->checkPermissionSection('superuser'); } + /** + * Checks if the user is deletable + * + * @author A. Gianotto + * @since [v6.3.4] + * @return bool + */ + public function isDeletable() + { + return Gate::allows('delete', $this) + && ($this->assets()->count() === 0) + && ($this->licenses()->count() === 0) + && ($this->consumables()->count() === 0) + && ($this->accessories()->count() === 0) + && ($this->deleted_at == ''); + } + /** * Establishes the user -> company relationship