Skip to content

A free open source IT asset/license management system

Fixed FD-57089: PredefinedKitCheckoutService: tighten checkout target validation

snipef590af4c87b8
diff --git a/app/Services/PredefinedKitCheckoutService.php b/app/Services/PredefinedKitCheckoutService.php
index 2c2b6bdd96..8c191e3e65 100644
--- a/app/Services/PredefinedKitCheckoutService.php
+++ b/app/Services/PredefinedKitCheckoutService.php
@@ -37,9 +37,9 @@ class PredefinedKitCheckoutService
$errors = [];
$assets_to_add = $this->getAssetsToAdd($kit, $user, $errors);
- $license_seats_to_add = $this->getLicenseSeatsToAdd($kit, $errors);
- $consumables_to_add = $this->getConsumablesToAdd($kit, $errors);
- $accessories_to_add = $this->getAccessoriesToAdd($kit, $errors);
+ $license_seats_to_add = $this->getLicenseSeatsToAdd($kit, $user, $errors);
+ $consumables_to_add = $this->getConsumablesToAdd($kit, $user, $errors);
+ $accessories_to_add = $this->getAccessoriesToAdd($kit, $user, $errors);
if (count($errors) > 0) {
return ['errors' => $errors];
@@ -80,11 +80,24 @@ class PredefinedKitCheckoutService
foreach ($models as $model) {
$assets = $model->assets;
$quantity = $model->pivot->quantity;
+ $firstBlockedByCompany = null;
foreach ($assets as $asset) {
if (
$asset->availableForCheckout()
&& ! $asset->is($user)
) {
+ // FMCS tenant isolation. Skip candidate assets whose
+ // company doesn't allow this checkout target. Every
+ // other checkout sink (single, bulk, API, accessory,
+ // license, consumable) enforces this via canCheckoutTo.
+ // The kit path historically didn't, which let a
+ // multi-company non-superuser assign a company-A asset
+ // to a company-B-only user by routing through kits.
+ if (! $asset->canCheckoutTo($user)) {
+ $firstBlockedByCompany ??= $asset;
+
+ continue;
+ }
$this->authorize('checkout', $asset);
$quantity -= 1;
$assets_to_add[] = $asset;
@@ -94,20 +107,44 @@ class PredefinedKitCheckoutService
}
}
if ($quantity > 0) {
- $errors[] = trans('admin/kits/general.none_models', ['model' => $model->name, 'qty' => $model->pivot->quantity]);
+ // Prefer the cross-company message when the shortfall was
+ // caused by canCheckoutTo filtering rather than genuine
+ // unavailability, so the operator understands why a kit
+ // that visibly contains the model still can't be handed
+ // to this target. Reference the specific blocked asset's
+ // company since asset models themselves are not per-company.
+ if ($firstBlockedByCompany !== null) {
+ $errors[] = trans('general.error_checkout_company_mismatch', [
+ 'item' => trans('general.asset').' "'.$firstBlockedByCompany->display_name.'"',
+ 'item_company' => $firstBlockedByCompany->company?->name ?? trans('general.unassigned'),
+ 'target' => trans('general.user').' "'.($user->name ?? $user->username ?? $user->id).'"',
+ ]);
+ } else {
+ $errors[] = trans('admin/kits/general.none_models', ['model' => $model->name, 'qty' => $model->pivot->quantity]);
+ }
}
}
return $assets_to_add;
}
- protected function getLicenseSeatsToAdd($kit, &$errors)
+ protected function getLicenseSeatsToAdd($kit, $user, &$errors)
{
$seats_to_add = [];
$licenses = $kit->licenses()
->with('freeSeats')
->get();
foreach ($licenses as $license) {
+ // FMCS tenant isolation, see comment in getAssetsToAdd.
+ if (! $license->canCheckoutTo($user)) {
+ $errors[] = trans('general.error_checkout_company_mismatch', [
+ 'item' => trans('general.license').' "'.$license->name.'"',
+ 'item_company' => $license->company?->name ?? trans('general.unassigned'),
+ 'target' => trans('general.user').' "'.($user->name ?? $user->username ?? $user->id).'"',
+ ]);
+
+ continue;
+ }
$this->authorize('checkout', $license);
$quantity = $license->pivot->quantity;
if ($quantity > count($license->freeSeats)) {
@@ -121,30 +158,54 @@ class PredefinedKitCheckoutService
return $seats_to_add;
}
- protected function getConsumablesToAdd($kit, &$errors)
+ protected function getConsumablesToAdd($kit, $user, &$errors)
{
$consumables = $kit->consumables()->with('users')->get();
+ $eligible = [];
foreach ($consumables as $consumable) {
+ // FMCS tenant isolation, see comment in getAssetsToAdd.
+ if (! $consumable->canCheckoutTo($user)) {
+ $errors[] = trans('general.error_checkout_company_mismatch', [
+ 'item' => trans('general.consumable').' "'.$consumable->name.'"',
+ 'item_company' => $consumable->company?->name ?? trans('general.unassigned'),
+ 'target' => trans('general.user').' "'.($user->name ?? $user->username ?? $user->id).'"',
+ ]);
+
+ continue;
+ }
$this->authorize('checkout', $consumable);
if ($consumable->numRemaining() < $consumable->pivot->quantity) {
$errors[] = trans('admin/kits/general.none_consumables', ['consumable' => $consumable->name, 'qty' => $consumable->pivot->quantity]);
}
+ $eligible[] = $consumable;
}
- return $consumables;
+ return $eligible;
}
- protected function getAccessoriesToAdd($kit, &$errors)
+ protected function getAccessoriesToAdd($kit, $user, &$errors)
{
$accessories = $kit->accessories()->with('users')->get();
+ $eligible = [];
foreach ($accessories as $accessory) {
+ // FMCS tenant isolation, see comment in getAssetsToAdd.
+ if (! $accessory->canCheckoutTo($user)) {
+ $errors[] = trans('general.error_checkout_company_mismatch', [
+ 'item' => trans('general.accessory').' "'.$accessory->name.'"',
+ 'item_company' => $accessory->company?->name ?? trans('general.unassigned'),
+ 'target' => trans('general.user').' "'.($user->name ?? $user->username ?? $user->id).'"',
+ ]);
+
+ continue;
+ }
$this->authorize('checkout', $accessory);
if ($accessory->numRemaining() < $accessory->pivot->quantity) {
$errors[] = trans('admin/kits/general.none_accessory', ['accessory' => $accessory->name, 'qty' => $accessory->pivot->quantity]);
}
+ $eligible[] = $accessory;
}
- return $accessories;
+ return $eligible;
}
protected function saveToDb($user, $admin, $checkout_at, $expected_checkin, $errors, $assets_to_add, $license_seats_to_add, $consumables_to_add, $accessories_to_add, $note)
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index a2baa0c1a3..2dd49b489d 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -11761,3 +11761,9 @@ parameters:
count: 2
path: routes/web.php
+
+ -
+ message: '#^Using nullsafe property access "\?\-\>name" on left side of \?\? is unnecessary\. Use \-\> instead\.$#'
+ identifier: nullsafe.neverNull
+ count: 4
+ path: app/Services/PredefinedKitCheckoutService.php
diff --git a/tests/Feature/PredefinedKits/Ui/CheckoutKitFmcsTest.php b/tests/Feature/PredefinedKits/Ui/CheckoutKitFmcsTest.php
new file mode 100644
index 0000000000..208ee0b298
--- /dev/null
+++ b/tests/Feature/PredefinedKits/Ui/CheckoutKitFmcsTest.php
@@ -0,0 +1,184 @@
+<?php
+
+namespace Tests\Feature\PredefinedKits\Ui;
+
+use App\Models\Accessory;
+use App\Models\Asset;
+use App\Models\AssetModel;
+use App\Models\Company;
+use App\Models\Consumable;
+use App\Models\License;
+use App\Models\PredefinedKit;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * FMCS tenant-isolation coverage for the kit checkout route.
+ *
+ * The kit-checkout path historically didn't call canCheckoutTo, so a
+ * multi-company non-superuser could assign a company-A asset to a
+ * company-B-only user by routing through kits — bypassing the guard
+ * every other checkout sink (single, bulk, API, accessory, license,
+ * consumable) enforces. Each test in this file exercises the exact
+ * shape of that bypass and asserts the row/pivot state is unchanged.
+ */
+class CheckoutKitFmcsTest extends TestCase
+{
+ public function test_asset_from_wrong_company_is_not_checked_out_via_kit()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $companyA = Company::factory()->create();
+ $companyB = Company::factory()->create();
+
+ $actor = User::factory()->checkoutAssets()->create();
+ $actor->companies()->sync([$companyA->id, $companyB->id]);
+ $actor->syncLegacyCompanyIdMirror();
+
+ $victim = User::factory()->create();
+ $victim->companies()->sync([$companyB->id]);
+ $victim->syncLegacyCompanyIdMirror();
+
+ $model = AssetModel::factory()->create();
+ $asset = Asset::factory()->create([
+ 'model_id' => $model->id,
+ 'company_id' => $companyA->id,
+ 'assigned_to' => null,
+ ]);
+
+ $kit = PredefinedKit::factory()->create();
+ $kit->models()->attach($model->id, ['quantity' => 1]);
+
+ $this->actingAs($actor)
+ ->post(route('kits.checkout.store', $kit), ['user_id' => $victim->id]);
+
+ $this->assertNull(
+ $asset->fresh()->assigned_to,
+ 'Kit checkout must not assign a company-A asset to a company-B-only user.',
+ );
+ }
+
+ public function test_asset_from_same_company_is_checked_out_via_kit()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $companyA = Company::factory()->create();
+
+ $actor = User::factory()->checkoutAssets()->create();
+ $actor->companies()->sync([$companyA->id]);
+ $actor->syncLegacyCompanyIdMirror();
+
+ $target = User::factory()->create();
+ $target->companies()->sync([$companyA->id]);
+ $target->syncLegacyCompanyIdMirror();
+
+ $model = AssetModel::factory()->create();
+ $asset = Asset::factory()->create([
+ 'model_id' => $model->id,
+ 'company_id' => $companyA->id,
+ 'assigned_to' => null,
+ ]);
+
+ $kit = PredefinedKit::factory()->create();
+ $kit->models()->attach($model->id, ['quantity' => 1]);
+
+ $this->actingAs($actor)
+ ->post(route('kits.checkout.store', $kit), ['user_id' => $target->id]);
+
+ $this->assertSame(
+ $target->id,
+ $asset->fresh()->assigned_to,
+ 'Kit checkout to a same-company target must still succeed.',
+ );
+ }
+
+ public function test_license_from_wrong_company_is_not_checked_out_via_kit()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $companyA = Company::factory()->create();
+ $companyB = Company::factory()->create();
+
+ $actor = User::factory()->checkoutAssets()->create();
+ $actor->companies()->sync([$companyA->id, $companyB->id]);
+ $actor->syncLegacyCompanyIdMirror();
+
+ $victim = User::factory()->create();
+ $victim->companies()->sync([$companyB->id]);
+ $victim->syncLegacyCompanyIdMirror();
+
+ $license = License::factory()->create(['company_id' => $companyA->id, 'seats' => 3]);
+
+ $kit = PredefinedKit::factory()->create();
+ $kit->licenses()->attach($license->id, ['quantity' => 1]);
+
+ $this->actingAs($actor)
+ ->post(route('kits.checkout.store', $kit), ['user_id' => $victim->id]);
+
+ $this->assertSame(
+ 0,
+ $license->licenseseats()->where('assigned_to', $victim->id)->count(),
+ 'Kit checkout must not assign a company-A license seat to a company-B-only user.',
+ );
+ }
+
+ public function test_consumable_from_wrong_company_is_not_checked_out_via_kit()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $companyA = Company::factory()->create();
+ $companyB = Company::factory()->create();
+
+ $actor = User::factory()->checkoutAssets()->create();
+ $actor->companies()->sync([$companyA->id, $companyB->id]);
+ $actor->syncLegacyCompanyIdMirror();
+
+ $victim = User::factory()->create();
+ $victim->companies()->sync([$companyB->id]);
+ $victim->syncLegacyCompanyIdMirror();
+
+ $consumable = Consumable::factory()->create(['company_id' => $companyA->id, 'qty' => 5]);
+
+ $kit = PredefinedKit::factory()->create();
+ $kit->consumables()->attach($consumable->id, ['quantity' => 1]);
+
+ $this->actingAs($actor)
+ ->post(route('kits.checkout.store', $kit), ['user_id' => $victim->id]);
+
+ $this->assertSame(
+ 0,
+ \DB::table('consumables_users')->where('consumable_id', $consumable->id)->where('assigned_to', $victim->id)->count(),
+ 'Kit checkout must not assign a company-A consumable to a company-B-only user.',
+ );
+ }
+
+ public function test_accessory_from_wrong_company_is_not_checked_out_via_kit()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $companyA = Company::factory()->create();
+ $companyB = Company::factory()->create();
+
+ $actor = User::factory()->checkoutAssets()->create();
+ $actor->companies()->sync([$companyA->id, $companyB->id]);
+ $actor->syncLegacyCompanyIdMirror();
+
+ $victim = User::factory()->create();
+ $victim->companies()->sync([$companyB->id]);
+ $victim->syncLegacyCompanyIdMirror();
+
+ $accessory = Accessory::factory()->create(['company_id' => $companyA->id, 'qty' => 5]);
+
+ $kit = PredefinedKit::factory()->create();
+ $kit->accessories()->attach($accessory->id, ['quantity' => 1]);
+
+ $this->actingAs($actor)
+ ->post(route('kits.checkout.store', $kit), ['user_id' => $victim->id]);
+
+ $this->assertSame(
+ 0,
+ \DB::table('accessories_checkout')->where('accessory_id', $accessory->id)->where('assigned_to', $victim->id)->count(),
+ 'Kit checkout must not assign a company-A accessory to a company-B-only user.',
+ );
+ }
+}