A free open source IT asset/license management system
Added mutex rules
snipedd870a0d66e9
diff --git a/app/Models/Component.php b/app/Models/Component.phpindex 5cd7dc6e1c..9df8dc7b6b 100644--- a/app/Models/Component.php+++ b/app/Models/Component.php@@ -14,6 +14,7 @@ use App\Models\Traits\Requestable;use App\Models\Traits\Searchable;use App\Presenters\ComponentPresenter;use App\Presenters\Presentable;+use App\Rules\RequestableArchivedMutex;use Illuminate\Database\Eloquent\Casts\Attribute;use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Relations\BelongsTo;@@ -50,6 +51,7 @@ class Component extends SnipeModelprotected $casts = ['purchase_date' => 'datetime','requestable' => 'boolean',+ 'archived' => 'boolean',];protected $table = 'components';@@ -70,9 +72,31 @@ class Component extends SnipeModel'manufacturer_id' => 'integer|exists:manufacturers,id|nullable','default_supplier_id' => 'nullable|integer|exists:suppliers,id','default_purchase_cost' => 'numeric|nullable|gte:0|max:99999999999999999.99',- 'requestable' => 'nullable|boolean',+ 'requestable' => ['nullable', 'boolean'],+ 'archived' => ['nullable', 'boolean'],];+ /**+ * PHP 8.2 blocks `new X` in property defaults, so the rule object+ * gets spliced in here rather than in $rules above.+ */+ public function getRules(): array+ {+ $rules = $this->rules ?? [];+ $mutex = new RequestableArchivedMutex;++ $noArchivingWithCheckouts = function (string $attribute, mixed $value, \Closure $fail): void {+ if (filter_var($value, FILTER_VALIDATE_BOOLEAN) && $this->exists && $this->numCheckedOut() > 0) {+ $fail(trans('admin/components/message.archived_blocked_by_checkouts'));+ }+ };++ $rules['requestable'] = array_merge((array) ($rules['requestable'] ?? []), [$mutex]);+ $rules['archived'] = array_merge((array) ($rules['archived'] ?? []), [$mutex, $noArchivingWithCheckouts]);++ return $rules;+ }+/*** Whether the model should inject it's identifier to the unique* validation rules before attempting validation. If this property@@ -107,6 +131,7 @@ class Component extends SnipeModel'default_supplier_id','default_purchase_cost','requestable',+ 'archived',];use Searchable;diff --git a/app/Rules/RequestableArchivedMutex.php b/app/Rules/RequestableArchivedMutex.phpnew file mode 100644index 0000000000..badeac46e8--- /dev/null+++ b/app/Rules/RequestableArchivedMutex.php@@ -0,0 +1,41 @@+<?php++namespace App\Rules;++use Closure;+use Illuminate\Contracts\Validation\DataAwareRule;+use Illuminate\Contracts\Validation\ValidationRule;++/**+ * Checkoutable items (components, accessories, consumables, licenses, …)+ * can be flagged `requestable` or `archived`, but never both at once:+ * requestable implies "available for people to ask for" while archived+ * implies "retired from active inventory." Place this rule on BOTH+ * fields in a model's $rules so whichever side gets toggled first+ * surfaces the collision.+ */+class RequestableArchivedMutex implements DataAwareRule, ValidationRule+{+ /** @var array<string, mixed> */+ protected array $data = [];++ public function setData(array $data): static+ {+ $this->data = $data;++ return $this;+ }++ public function validate(string $attribute, mixed $value, Closure $fail): void+ {+ if (! filter_var($value, FILTER_VALIDATE_BOOLEAN)) {+ return;+ }++ $other = $attribute === 'requestable' ? 'archived' : 'requestable';++ if (filter_var($this->data[$other] ?? null, FILTER_VALIDATE_BOOLEAN)) {+ $fail(trans('general.requestable_archived_conflict'));+ }+ }+}