A free open source IT asset/license management system
Closer check on scopes for bulk user actions
snipec0e94879d5f4
diff --git a/app/Http/Controllers/Users/BulkUsersController.php b/app/Http/Controllers/Users/BulkUsersController.phpindex dec38c0721..f892654d9f 100644--- a/app/Http/Controllers/Users/BulkUsersController.php+++ b/app/Http/Controllers/Users/BulkUsersController.php@@ -6,6 +6,7 @@ use App\Events\UserMerged;use App\Helpers\Helper;use App\Http\Controllers\Controller;use App\Models\Accessory;+use App\Models\AccessoryCheckout;use App\Models\Actionlog;use App\Models\Asset;use App\Models\CheckoutAcceptance;@@ -482,12 +483,26 @@ class BulkUsersController extends Controller->pluck('id');ConsumableAssignment::whereIn('id', $scopedConsumableRowIds)->delete();- CheckoutAcceptance::pending()+ // Delete only the pivot rows we already permission-checked and+ // audit-logged.+ AccessoryCheckout::whereIn('id', $scopedAccessoryUserRows->pluck('id'))->delete();++ // Delete only pending acceptances whose underlying checkoutable is in scope.+ $scopedAcceptanceIds = CheckoutAcceptance::pending()->whereIn('assigned_to_id', $user_raw_array)- ->delete();+ ->get(['id', 'checkoutable_type', 'checkoutable_id'])+ ->filter(fn($acceptance) => match ($acceptance->checkoutable_type) {+ Asset::class => $assets->pluck('id')->contains($acceptance->checkoutable_id),+ Accessory::class => $scopedAccessoryUserRows->pluck('accessory_id')->contains($acceptance->checkoutable_id),+ LicenseSeat::class => $scopedLicenseSeats->pluck('id')->contains($acceptance->checkoutable_id),+ Consumable::class => $scopedConsumableIds->contains($acceptance->checkoutable_id),+ default => false,+ })+ ->pluck('id');++ CheckoutAcceptance::whereIn('id', $scopedAcceptanceIds)->delete();foreach ($users as $user) {- $user->accessories()->sync([]);if ($request->input('delete_user') == '1') {if (auth()->user()->cannot('delete', $user)) {return redirect()->route('users.index')->with('error', trans('general.insufficient_permissions'));diff --git a/tests/Feature/Users/Ui/BulkActions/BulkCheckinAcceptanceCrossTenantDeleteFmcsTest.php b/tests/Feature/Users/Ui/BulkActions/BulkCheckinAcceptanceCrossTenantDeleteFmcsTest.phpnew file mode 100644index 0000000000..edab636d09--- /dev/null+++ b/tests/Feature/Users/Ui/BulkActions/BulkCheckinAcceptanceCrossTenantDeleteFmcsTest.php@@ -0,0 +1,113 @@+<?php++namespace Tests\Feature\Users\Ui\BulkActions;++use App\Models\Accessory;+use App\Models\CheckoutAcceptance;+use App\Models\Company;+use App\Models\Statuslabel;+use App\Models\User;+use Illuminate\Support\Facades\DB;+use Tests\TestCase;++/**+ * Regression coverage for the bulk-checkin CheckoutAcceptance cross-tenant+ * delete, companion to BulkCheckinAccessoryCrossTenantDeleteFmcsTest. Before+ * the fix the destroy path ran+ * CheckoutAcceptance::pending()+ * ->whereIn('assigned_to_id', $user_raw_array)+ * ->delete()+ * which filtered on `assigned_to_id` alone and touched every pending+ * acceptance the target user held, including ones whose checkoutable+ * belonged to a company the caller could not see. Under FMCS floater mode+ * a company-A caller could silently clear pending acceptances the victim+ * company's checkout flow was waiting on, without any permission check+ * against the underlying checkoutable. This file locks in the fixed+ * behavior. Cross-company pending acceptances survive. Same-company+ * pending acceptances still get deleted (positive control).+ */+class BulkCheckinAcceptanceCrossTenantDeleteFmcsTest extends TestCase+{+ public function test_bulk_checkin_does_not_delete_foreign_company_pending_acceptance_for_floater_target(): void+ {+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->set(['null_company_is_floater' => 1]);++ $companyA = Company::factory()->create();+ $companyB = Company::factory()->create();++ $attacker = User::factory()->editUsers()->checkinAccessories()->create();+ $attacker->companies()->sync([$companyA->id]);+ $attacker->syncLegacyCompanyIdMirror();++ $floater = User::factory()->create();++ $foreignAccessory = Accessory::factory()->create(['company_id' => $companyB->id]);+ DB::table('accessories_checkout')->insert([+ 'accessory_id' => $foreignAccessory->id,+ 'assigned_to' => $floater->id,+ 'assigned_type' => User::class,+ 'created_by' => $floater->id,+ 'created_at' => now(),+ ]);++ $foreignAcceptance = CheckoutAcceptance::factory()->pending()->create([+ 'checkoutable_type' => Accessory::class,+ 'checkoutable_id' => $foreignAccessory->id,+ 'assigned_to_id' => $floater->id,+ ]);++ $this->actingAs($attacker)+ ->post(route('users/bulksave'), [+ 'ids' => [$floater->id],+ 'status_id' => Statuslabel::factory()->create()->id,+ ]);++ // Not just present but explicitly non-deleted: CheckoutAcceptance uses+ // SoftDeletes, so a row can remain in the table with deleted_at set+ // and still satisfy assertDatabaseHas.+ $this->assertDatabaseHas('checkout_acceptances', [+ 'id' => $foreignAcceptance->id,+ 'assigned_to_id' => $floater->id,+ 'deleted_at' => null,+ ]);+ }++ public function test_bulk_checkin_still_deletes_in_company_pending_acceptance(): void+ {+ $this->settings->enableMultipleFullCompanySupport();++ $companyA = Company::factory()->create();++ $actor = User::factory()->editUsers()->checkinAccessories()->create();+ $actor->companies()->sync([$companyA->id]);+ $actor->syncLegacyCompanyIdMirror();++ $target = User::factory()->create();+ $target->companies()->sync([$companyA->id]);+ $target->syncLegacyCompanyIdMirror();++ $accessory = Accessory::factory()->create(['company_id' => $companyA->id]);+ DB::table('accessories_checkout')->insert([+ 'accessory_id' => $accessory->id,+ 'assigned_to' => $target->id,+ 'assigned_type' => User::class,+ 'created_by' => $actor->id,+ 'created_at' => now(),+ ]);++ $acceptance = CheckoutAcceptance::factory()->pending()->create([+ 'checkoutable_type' => Accessory::class,+ 'checkoutable_id' => $accessory->id,+ 'assigned_to_id' => $target->id,+ ]);++ $this->actingAs($actor)+ ->post(route('users/bulksave'), [+ 'ids' => [$target->id],+ 'status_id' => Statuslabel::factory()->create()->id,+ ]);++ $this->assertSoftDeleted('checkout_acceptances', ['id' => $acceptance->id]);+ }+}diff --git a/tests/Feature/Users/Ui/BulkActions/BulkCheckinAccessoryCrossTenantDeleteFmcsTest.php b/tests/Feature/Users/Ui/BulkActions/BulkCheckinAccessoryCrossTenantDeleteFmcsTest.phpnew file mode 100644index 0000000000..b6a84cdf7d--- /dev/null+++ b/tests/Feature/Users/Ui/BulkActions/BulkCheckinAccessoryCrossTenantDeleteFmcsTest.php@@ -0,0 +1,83 @@+<?php++namespace Tests\Feature\Users\Ui\BulkActions;++use App\Models\Accessory;+use App\Models\Company;+use App\Models\Statuslabel;+use App\Models\User;+use Illuminate\Support\Facades\DB;+use Tests\TestCase;+++class BulkCheckinAccessoryCrossTenantDeleteFmcsTest extends TestCase+{+ public function test_bulk_checkin_does_not_delete_foreign_company_accessory_checkout_rows_for_floater_target(): void+ {+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->set(['null_company_is_floater' => 1]);++ $companyA = Company::factory()->create();+ $companyB = Company::factory()->create();++ $attacker = User::factory()->editUsers()->checkinAccessories()->create();+ $attacker->companies()->sync([$companyA->id]);+ $attacker->syncLegacyCompanyIdMirror();++ // Floater target: no company pivot rows, visible cross-company under null_company_is_floater.+ $floater = User::factory()->create();++ $foreignAccessory = Accessory::factory()->create(['company_id' => $companyB->id]);+ $pivotId = DB::table('accessories_checkout')->insertGetId([+ 'accessory_id' => $foreignAccessory->id,+ 'assigned_to' => $floater->id,+ 'assigned_type' => User::class,+ 'created_by' => $floater->id,+ 'created_at' => now(),+ ]);++ $this->actingAs($attacker)+ ->post(route('users/bulksave'), [+ 'ids' => [$floater->id],+ 'status_id' => Statuslabel::factory()->create()->id,+ ]);++ $this->assertDatabaseHas('accessories_checkout', [+ 'id' => $pivotId,+ 'accessory_id' => $foreignAccessory->id,+ 'assigned_to' => $floater->id,+ ]);+ }++ public function test_bulk_checkin_still_deletes_in_company_accessory_checkout_rows(): void+ {+ $this->settings->enableMultipleFullCompanySupport();++ $companyA = Company::factory()->create();++ $actor = User::factory()->editUsers()->checkinAccessories()->create();+ $actor->companies()->sync([$companyA->id]);+ $actor->syncLegacyCompanyIdMirror();++ $target = User::factory()->create();+ $target->companies()->sync([$companyA->id]);+ $target->syncLegacyCompanyIdMirror();++ $accessory = Accessory::factory()->create(['company_id' => $companyA->id]);+ $pivotId = DB::table('accessories_checkout')->insertGetId([+ 'accessory_id' => $accessory->id,+ 'assigned_to' => $target->id,+ 'assigned_type' => User::class,+ 'created_by' => $actor->id,+ 'created_at' => now(),+ ]);++ $this->actingAs($actor)+ ->post(route('users/bulksave'), [+ 'ids' => [$target->id],+ 'status_id' => Statuslabel::factory()->create()->id,+ ]);++ $this->assertDatabaseMissing('accessories_checkout', ['id' => $pivotId]);+ }+}