Skip to content

A free open source IT asset/license management system

Tighter check for FMCS

snipeb5c2d8968c08
diff --git a/app/Http/Controllers/BulkAccessoriesController.php b/app/Http/Controllers/BulkAccessoriesController.php
index a0cab5ace5..8f153106fb 100644
--- a/app/Http/Controllers/BulkAccessoriesController.php
+++ b/app/Http/Controllers/BulkAccessoriesController.php
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Models\Accessory;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
@@ -17,6 +18,10 @@ class BulkAccessoriesController extends Controller
$success_count = 0;
foreach ((array) $request->input('ids', []) as $id) {
+ // Accessory uses CompanyableTrait, so Accessory::find() is
+ // filtered by CompanyableScope — cross-company rows return null
+ // and fall into the "does_not_exist" bucket without ever
+ // reaching authorization.
$accessory = Accessory::find($id);
if (is_null($accessory)) {
$errors[] = trans('admin/accessories/message.does_not_exist', ['id' => $id]);
@@ -24,6 +29,16 @@ class BulkAccessoriesController extends Controller
continue;
}
+ // Per-row auth as belt-and-braces on top of the query scope.
+ // Catches FMCS mismatches that a superuser scope-bypass could
+ // let through, plus any future policy tightening (e.g. per-role
+ // company-based delete gating).
+ if (! Gate::allows('delete', $accessory)) {
+ $errors[] = trans('general.unauthorized');
+
+ continue;
+ }
+
$accessory->loadCount('checkouts as checkouts_count');
if (! $accessory->isDeletable()) {
$errors[] = trans('general.bulk_delete_associations.assoc_checkouts_no_count', [
diff --git a/tests/Feature/Accessories/Ui/BulkDeleteAccessoriesTest.php b/tests/Feature/Accessories/Ui/BulkDeleteAccessoriesTest.php
index d46b1df493..0e2cfcdf32 100644
--- a/tests/Feature/Accessories/Ui/BulkDeleteAccessoriesTest.php
+++ b/tests/Feature/Accessories/Ui/BulkDeleteAccessoriesTest.php
@@ -4,6 +4,7 @@ namespace Tests\Feature\Accessories\Ui;
use App\Models\Accessory;
use App\Models\AccessoryCheckout;
+use App\Models\Company;
use App\Models\User;
use Tests\TestCase;
@@ -82,6 +83,54 @@ class BulkDeleteAccessoriesTest extends TestCase
$this->assertSoftDeleted('accessories', ['id' => $deletable->id]);
}
+ public function test_respects_fmcs_scoping_for_non_superuser()
+ {
+ // Caller scoped to company A asking to bulk delete [A-owned, B-owned]
+ // sees the B-owned row hidden by CompanyableScope (Accessory::find()
+ // returns null), which falls into the "does_not_exist" error bucket.
+ // The A-owned row deletes normally, so the request is a partial success.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $accessoryA = Accessory::factory()->for($companyA)->create();
+ $accessoryB = Accessory::factory()->for($companyB)->create();
+ $userA = User::factory()->for($companyA)->deleteAccessories()->create();
+
+ $this->actingAs($userA)
+ ->post(route('accessories.bulk.delete'), [
+ 'ids' => [$accessoryA->id, $accessoryB->id],
+ ])
+ ->assertRedirect(route('accessories.index'))
+ ->assertSessionHas('success')
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertSoftDeleted('accessories', ['id' => $accessoryA->id]);
+ $this->assertDatabaseHas('accessories', ['id' => $accessoryB->id, 'deleted_at' => null]);
+ }
+
+ public function test_superuser_bypasses_fmcs_and_deletes_accessories_in_any_company()
+ {
+ // FMCS on, but a superuser is exempt from CompanyableScope. A bulk
+ // delete touching accessories in two different companies should
+ // remove both without any "does_not_exist" errors.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $accessoryA = Accessory::factory()->for($companyA)->create();
+ $accessoryB = Accessory::factory()->for($companyB)->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.bulk.delete'), [
+ 'ids' => [$accessoryA->id, $accessoryB->id],
+ ])
+ ->assertRedirect(route('accessories.index'))
+ ->assertSessionHas('success')
+ ->assertSessionMissing('multi_error_messages');
+
+ $this->assertSoftDeleted('accessories', ['id' => $accessoryA->id]);
+ $this->assertSoftDeleted('accessories', ['id' => $accessoryB->id]);
+ }
+
public function test_bulk_success_message_pluralizes_by_count()
{
$solo = Accessory::factory()->create();