Skip to content

A free open source IT asset/license management system

Added/refactorerd isDeletable()

snipe19877244cdee
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 <[email protected]>
+ * @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 <[email protected]>
+ * @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