Skip to content

A free open source IT asset/license management system

Tighter FMCS control on bulk edit

snipe66f61c053772
diff --git a/app/Http/Controllers/Components/BulkComponentsController.php b/app/Http/Controllers/Components/BulkComponentsController.php
index 34cb61d59a..1afa39b825 100644
--- a/app/Http/Controllers/Components/BulkComponentsController.php
+++ b/app/Http/Controllers/Components/BulkComponentsController.php
@@ -3,11 +3,13 @@
namespace App\Http\Controllers\Components;
use App\Http\Controllers\Controller;
+use App\Models\Company;
use App\Models\Component;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
+use Illuminate\Validation\ValidationException;
class BulkComponentsController extends Controller
{
@@ -98,6 +100,22 @@ class BulkComponentsController extends Controller
$updateData[$field] = $value;
}
+ // Route company_id through getIdForCurrentUser so a non-superuser
+ // can't hand-craft a POST that writes a cross-tenant company_id
+ // across the selection. Mirrors the single-component edit path at
+ // ComponentsController::update. Throws ValidationException on a
+ // submitted-but-foreign company, which lets the request fail with
+ // a proper field error rather than a silent nullout.
+ if (array_key_exists('company_id', $updateData)) {
+ try {
+ $updateData['company_id'] = Company::getIdForCurrentUser($updateData['company_id']);
+ } catch (ValidationException $e) {
+ return redirect()->route('components.index')
+ ->withErrors($e->errors())
+ ->with('error', trans('general.unauthorized'));
+ }
+ }
+
// Opt-in null-outs. Checkbox named null_{field}=1 clears that
// column across the selection, giving admins a way to blank out
// nullable columns like location/supplier/manufacturer.
diff --git a/tests/Feature/Components/Ui/BulkComponentsFmcsScopingTest.php b/tests/Feature/Components/Ui/BulkComponentsFmcsScopingTest.php
index 3ef36c9988..80e9212eec 100644
--- a/tests/Feature/Components/Ui/BulkComponentsFmcsScopingTest.php
+++ b/tests/Feature/Components/Ui/BulkComponentsFmcsScopingTest.php
@@ -7,6 +7,7 @@ use App\Models\Asset;
use App\Models\Company;
use App\Models\Component;
use App\Models\ComponentAssignment;
+use App\Models\Location;
use App\Models\User;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
@@ -120,6 +121,55 @@ class BulkComponentsFmcsScopingTest extends TestCase
Event::assertNothingDispatched();
}
+ public function test_bulk_edit_rejects_write_to_cross_company_company_id_under_fmcs(): void
+ {
+ // Even if a non-superuser hand-crafted a POST with a company_id
+ // outside their pivot set, the fmcs_company validator + policy
+ // layer should block the write. Pin so the picker-level
+ // filtering can't be the only defense.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->editComponents()->viewComponents()->make());
+ $target = Component::factory()->for($companyA)->create();
+
+ $this->actingAs($actor)
+ ->post(route('components.bulkedit.store'), [
+ 'ids' => [$target->id],
+ 'company_id' => (string) $companyB->id,
+ ])
+ ->assertRedirect(route('components.index'));
+
+ $this->assertSame($companyA->id, $target->fresh()->company_id);
+ }
+
+ public function test_bulk_edit_rejects_cross_company_location_under_scope_locations_fmcs(): void
+ {
+ // scope_locations_fmcs=1 means a location "belongs" to the
+ // companies in its parent chain. fmcs_location validator must
+ // reject a bulk-edit that pairs location X with company Y when
+ // X doesn't live in Y.
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->editComponents()->viewComponents()->make());
+ $homeLocation = Location::factory()->for($companyA)->create();
+ $target = Component::factory()->for($companyA)->create(['location_id' => $homeLocation->id]);
+ $foreignLocation = Location::factory()->for($companyB)->create();
+
+ // Flip scope_locations_fmcs AFTER factory setup so cross-company
+ // location-vs-company validation is in force only for the actual
+ // bulk-update request, not during the test's own data seeding.
+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();
+
+ $this->actingAs($actor)
+ ->post(route('components.bulkedit.store'), [
+ 'ids' => [$target->id],
+ 'location_id' => (string) $foreignLocation->id,
+ ])
+ ->assertRedirect(route('components.index'));
+
+ $this->assertSame($homeLocation->id, $target->fresh()->location_id);
+ }
+
public function test_bulk_checkin_floater_can_checkin_null_company_pivot(): void
{
// Floater ON: company A actor is allowed to touch null-company