From f8d18a8eb09b7943ad9663043daadfa4f60ba9de Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Wed, 29 Apr 2020 10:59:00 -0400 Subject: [PATCH] Revert asset-checkout-different validation. This was causing issues when trying to check an item out to a user or a location because of the way laravel handles validation. Instead, rely on the exception check we had in the controller. I moved this exception up to the model checkout method so that it would work for anywhere that that method was called, even if it avoided the controller. --- app/Http/Controllers/Assets/AssetCheckoutController.php | 4 +--- app/Http/Requests/AssetCheckoutRequest.php | 2 +- app/Models/Asset.php | 4 ++++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.php index a6d049874a..5671395982 100644 --- a/app/Http/Controllers/Assets/AssetCheckoutController.php +++ b/app/Http/Controllers/Assets/AssetCheckoutController.php @@ -62,9 +62,7 @@ class AssetCheckoutController extends Controller $admin = Auth::user(); $target = $this->determineCheckoutTarget($asset); - if ($asset->is($target)) { - throw new CheckoutNotAllowed('You cannot check an asset out to itself.'); - } + $asset = $this->updateAssetLocation($asset, $target); $checkout_at = date("Y-m-d H:i:s"); diff --git a/app/Http/Requests/AssetCheckoutRequest.php b/app/Http/Requests/AssetCheckoutRequest.php index afe46440ed..4ba8ce09b7 100644 --- a/app/Http/Requests/AssetCheckoutRequest.php +++ b/app/Http/Requests/AssetCheckoutRequest.php @@ -23,7 +23,7 @@ class AssetCheckoutRequest extends Request { $rules = [ "assigned_user" => 'required_without_all:assigned_asset,assigned_location', - "assigned_asset" => 'required_without_all:assigned_user,assigned_location|different:'.$this->id, + "assigned_asset" => 'required_without_all:assigned_user,assigned_location', "assigned_location" => 'required_without_all:assigned_user,assigned_asset', "checkout_to_type" => 'required|in:asset,location,user' ]; diff --git a/app/Models/Asset.php b/app/Models/Asset.php index f0bdf4321c..2a92c81a5a 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Events\AssetCheckedOut; use App\Events\CheckoutableCheckedOut; +use App\Exceptions\CheckoutNotAllowed; use App\Http\Traits\UniqueSerialTrait; use App\Http\Traits\UniqueUndeletedTrait; use App\Models\Traits\Acceptable; @@ -271,6 +272,9 @@ class Asset extends Depreciable if (!$target) { return false; } + if ($this->is($target)) { + throw new CheckoutNotAllowed('You cannot check an asset out to itself.'); + } if ($expected_checkin) { $this->expected_checkin = $expected_checkin;