Skip to content

A free open source IT asset/license management system

Check company boundary on checkout before DB write

snipe0aded3607897
diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.php
index 2dec53a496..9cdbf9b71d 100644
--- a/app/Http/Controllers/Assets/AssetCheckoutController.php
+++ b/app/Http/Controllers/Assets/AssetCheckoutController.php
@@ -109,6 +109,26 @@ class AssetCheckoutController extends Controller
$target = $this->determineCheckoutTarget();
+ // Company-boundary gate has to fire before any DB write. The
+ // updateAssetLocation() helper mass-updates child assets'
+ // location_id when the target is a location, and the license-seat
+ // loop below persists $seat->assigned_to = $target->id. Both are
+ // real writes with nothing to roll them back if canCheckoutTo
+ // subsequently rejects a cross-company target.
+ if (! $asset->canCheckoutTo($target)) {
+ $targetType = match (class_basename($target)) {
+ 'User' => trans('general.user'),
+ 'Location' => trans('general.location'),
+ default => trans('general.asset'),
+ };
+
+ return redirect()->route('hardware.checkout.create', $asset)->with('error', trans('general.error_checkout_company_mismatch', [
+ 'item' => trans('general.asset').' "'.$asset->display_name.'"',
+ 'item_company' => $asset->company?->name ?? trans('general.unassigned'),
+ 'target' => $targetType.' "'.($target->name ?? $target->username ?? $target->id).'"',
+ ]));
+ }
+
$asset = $this->updateAssetLocation($asset, $target);
$checkout_at = date('Y-m-d H:i:s');
@@ -143,20 +163,6 @@ class AssetCheckoutController extends Controller
// Add any custom fields that should be included in the checkout
$asset->customFieldsForCheckinCheckout('display_checkout');
- if (! $asset->canCheckoutTo($target)) {
- $targetType = match (class_basename($target)) {
- 'User' => trans('general.user'),
- 'Location' => trans('general.location'),
- default => trans('general.asset'),
- };
-
- return redirect()->route('hardware.checkout.create', $asset)->with('error', trans('general.error_checkout_company_mismatch', [
- 'item' => trans('general.asset').' "'.$asset->display_name.'"',
- 'item_company' => $asset->company?->name ?? trans('general.unassigned'),
- 'target' => $targetType.' "'.($target->name ?? $target->username ?? $target->id).'"',
- ]));
- }
-
session()->put([
'redirect_option' => $request->input('redirect_option'),
'checkout_to_type' => $request->input('checkout_to_type'),
diff --git a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php
index 7b598efe58..0921d76c08 100644
--- a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php
+++ b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php
@@ -557,4 +557,38 @@ class AssetCheckoutTest extends TestCase
'location target' => ['location'],
];
}
+
+ public function test_license_seats_are_not_reassigned_when_cross_company_checkout_is_rejected()
+ {
+ // The company-boundary gate has to fire before the license-seat
+ // loop persists $seat->assigned_to = $target->id. Prior to the
+ // ordering fix the seat writes ran first, so a cross-company
+ // checkout attempt would leave the seat reassigned to a user in
+ // a different company even though the checkout itself was
+ // refused.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$assetCompany, $userCompany] = Company::factory()->count(2)->create();
+
+ $asset = Asset::factory()->for($assetCompany)->create();
+ $seat = LicenseSeat::factory()->assignedToAsset($asset)->create();
+ $originalSeatAssignedTo = $seat->assigned_to;
+
+ $user = User::factory()->forCompany($userCompany)->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('hardware.checkout.store', $asset), [
+ 'checkout_to_type' => 'user',
+ 'assigned_user' => $user->id,
+ ])
+ ->assertRedirect(route('hardware.checkout.store', $asset));
+
+ Event::assertNotDispatched(CheckoutableCheckedOut::class);
+
+ $this->assertSame(
+ $originalSeatAssignedTo,
+ $seat->fresh()->assigned_to,
+ 'License seat must not be reassigned when the parent checkout is refused for a cross-company target.',
+ );
+ }
}