A free open source IT asset/license management system
Fixed FD-57180 - location scoping even when scope locations is off
snipe46a1d9da0a8c
diff --git a/app/Models/Company.php b/app/Models/Company.phpindex 14ecc5f2e8..0909b1fced 100644--- a/app/Models/Company.php+++ b/app/Models/Company.php@@ -591,6 +591,21 @@ final class Company extends SnipeModel{$companyIds = self::getCurrentUserCompanyIds();+ // Location scoping is opt-in even under FMCS: setting+ // scope_locations_fmcs = 0 tells Snipe-IT that locations are meant+ // to be shared across tenants. Without this short-circuit the+ // global scope still filters locations by the caller's pivot+ // memberships, which hides null-company locations from every+ // non-superuser (strict mode) and breaks Location::find() lookups+ // on the checkin / checkout / audit paths that were tightened in+ // v8.7.0. CompanyableTrait::canCheckoutTo() and+ // LocationsController::store already respect this setting; the+ // global scope needs to as well.+ if ($query->getModel()->getTable() === 'locations'+ && ! Setting::getSettings()->scope_locations_fmcs) {+ return $query;+ }+// If we are scoping the companies table itself, look for the company.idif ($query->getModel()->getTable() == 'companies') {if (empty($companyIds)) {diff --git a/tests/Feature/Assets/Ui/CheckinAssetLocationScopingTest.php b/tests/Feature/Assets/Ui/CheckinAssetLocationScopingTest.phpnew file mode 100644index 0000000000..6080b82c88--- /dev/null+++ b/tests/Feature/Assets/Ui/CheckinAssetLocationScopingTest.php@@ -0,0 +1,189 @@+<?php++namespace Tests\Feature\Assets\Ui;++use App\Models\Asset;+use App\Models\Company;+use App\Models\Location;+use App\Models\User;+use Tests\TestCase;++/**+ * End-to-end regression coverage for the v8.7.0 check-in location+ * regression. Complements the scope-level pin in+ * tests/Feature/Fmcs/LocationScopingSettingTest.php.+ *+ * Symptom (reported by a paying customer on 8.7.2, three-tier admin+ * setup with Super / State / Campus): non-superuser scoped admins with+ * `assets.checkin` permission get "The assigned location could not be+ * found" on the check-in submit path. Super Admin unaffected because+ * they bypass FMCS entirely. Root cause was+ * `Company::scopeCompanyablesDirectly()` ignoring the+ * scope_locations_fmcs setting for the locations table, so+ * `AssetCheckinController::store()`'s `Location::find($id)` returned+ * null for any location outside the actor's pivot memberships even+ * when the install had opted out of location-level tenant scoping.+ *+ * These tests hit the real HTTP endpoint so any future controller+ * regression (added scope-respecting find, tightened validation on the+ * FormRequest, etc.) is caught end-to-end and not just at the model+ * layer.+ */+class CheckinAssetLocationScopingTest extends TestCase+{+ private Company $companyA;++ private Company $companyB;++ private User $scopedAdmin;++ private User $checkedOutTo;++ protected function setUp(): void+ {+ parent::setUp();++ [$this->companyA, $this->companyB] = Company::factory()->count(2)->create();++ // Non-superuser scoped admin, pivoted to Company A only, holding+ // assets.checkin. This is the "Campus Admin" tier in the+ // customer's setup.+ $this->scopedAdmin = $this->companyA->users()->save(+ User::factory()->checkinAssets()->make()+ );++ // Somebody to check the asset out to so it's in a checked-out+ // state at the top of the check-in flow. Their tenant doesn't+ // matter for the scope check on the LOCATION field.+ $this->checkedOutTo = $this->companyA->users()->save(User::factory()->make());+ }++ /**+ * Seed a location without going through Watson\ValidatingTrait's+ * fmcs_company validator. Same rationale as the sibling scope test+ * file: we assert read-side scope behavior on the check-in path,+ * so the write-side rule is out of scope for the fixture.+ */+ private function seedLocation(?int $companyId): Location+ {+ $location = Location::factory()->make();+ $location->company_id = $companyId;+ $location->setValidating(false);+ $location->save();++ return $location->refresh();+ }++ private function seedCheckedOutAsset(Location $location): Asset+ {+ // Use the assignedToUser() factory state so the resulting asset+ // matches the shape Snipe-IT normally produces for a checked-out+ // asset (assigned_to + assigned_type + last_checkout in sync)+ // rather than a hand-rolled combination. rtd_location_id is+ // still forced to the fixture location so the check-in form+ // pre-populates the picker with the target.+ return Asset::factory()+ ->for($this->companyA)+ ->assignedToUser($this->checkedOutTo)+ ->create([+ 'rtd_location_id' => $location->id,+ 'location_id' => $location->id,+ ]);+ }++ // -----------------------------------------------------------------+ // The exact customer configuration: FMCS on, location scoping off,+ // floater off. Scoped admin must be able to check in an asset even+ // when the submitted location is one their pivot memberships would+ // hide under strict scoping.+ // -----------------------------------------------------------------++ public function test_scoped_admin_can_check_in_when_location_scoping_is_off_and_target_is_null_company(): void+ {+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $nullCompanyLocation = $this->seedLocation(null);+ $asset = $this->seedCheckedOutAsset($nullCompanyLocation);+ $this->actingAs($this->scopedAdmin)+ ->post(route('hardware.checkin.store', $asset->id), [+ 'name' => $asset->name,+ 'location_id' => $nullCompanyLocation->id,+ 'rtd_location_id' => $nullCompanyLocation->id,+ 'redirect_option' => 'index',+ ])+ ->assertRedirect(route('hardware.index'))+ ->assertSessionHas('success')+ ->assertSessionMissing('error');++ $this->assertNull(+ $asset->fresh()->assigned_to,+ 'Asset should have been checked in (assigned_to cleared).'+ );+ }++ public function test_scoped_admin_can_check_in_when_location_scoping_is_off_and_target_is_cross_company(): void+ {+ // Location assigned to Company B, actor pivoted only to Company A.+ // With location scoping off, cross-company location targets are+ // supposed to work.+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $companyBLocation = $this->seedLocation($this->companyB->id);+ $asset = $this->seedCheckedOutAsset($companyBLocation);+ $this->actingAs($this->scopedAdmin)+ ->post(route('hardware.checkin.store', $asset->id), [+ 'name' => $asset->name,+ 'location_id' => $companyBLocation->id,+ 'rtd_location_id' => $companyBLocation->id,+ 'redirect_option' => 'index',+ ])+ ->assertRedirect(route('hardware.index'))+ ->assertSessionHas('success')+ ->assertSessionMissing('error');++ $this->assertNull($asset->fresh()->assigned_to);+ }++ // -----------------------------------------------------------------+ // scope_locations_fmcs ON: regression guard so the fix doesn't+ // silently loosen the strict-scoping path. When location scoping IS+ // on and the location is cross-tenant, the check-in submit should+ // still refuse the location, matching pre-fix strict semantics.+ // -----------------------------------------------------------------++ public function test_scoped_admin_still_blocked_when_location_scoping_is_on_and_target_is_cross_company(): void+ {+ // Regression guard for the strict-scoping path. Seed the asset+ // at its own tenant's location (which the scoped admin CAN see),+ // then have the check-in SUBMIT try to route it to a cross-+ // tenant location. Under scope_locations_fmcs=1, Location::find+ // inside the check-in controller returns null on the cross-+ // tenant id and the submit gets rejected. Seeding the asset the+ // other way around (rtd_location_id already cross-tenant) is not+ // a valid state under strict scoping — the fmcs_location+ // validator would reject that save regardless of actor.+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();+ $this->settings->disableFloaterMode();++ $companyALocation = $this->seedLocation($this->companyA->id);+ $companyBLocation = $this->seedLocation($this->companyB->id);+ $asset = $this->seedCheckedOutAsset($companyALocation);++ $this->actingAs($this->scopedAdmin)+ ->post(route('hardware.checkin.store', $asset->id), [+ 'name' => $asset->name,+ 'location_id' => $companyBLocation->id,+ 'rtd_location_id' => $companyBLocation->id,+ 'redirect_option' => 'index',+ ])+ ->assertRedirect()+ ->assertSessionHas('error');++ $this->assertNotNull(+ $asset->fresh()->assigned_to,+ 'Asset should NOT have been checked in — cross-tenant location under strict scoping must be refused.'+ );+ }+}diff --git a/tests/Feature/Fmcs/HttpCrossCompanyLeakTest.php b/tests/Feature/Fmcs/HttpCrossCompanyLeakTest.phpindex 044246fdb6..ffd184c3d8 100644--- a/tests/Feature/Fmcs/HttpCrossCompanyLeakTest.php+++ b/tests/Feature/Fmcs/HttpCrossCompanyLeakTest.php@@ -142,13 +142,19 @@ class HttpCrossCompanyLeakTest extends TestCasepublic function test_api_locations_index_hides_other_company_rows(): void{+ // Location tenant-scoping under FMCS is opt-in via scope_locations_fmcs.+ // With that setting off (the default, and the "locations are shared+ // across tenants" configuration many installs use), the scope+ // intentionally does not fire on the locations table. Enable it here+ // so the cross-company leak assertion below has semantic meaning.$companyBLocation = Location::factory()->for($this->companyB)->create();$caller = $this->callerScopedToCompanyA(['locations']);- $this->settings->enableMultipleFullCompanySupport();+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();$this->assertCompanyBRowHidden(route('api.locations.index'), $companyBLocation, $caller);$this->settings->enableFloaterMode();+ $this->settings->set(['scope_locations_fmcs' => 1]);$this->assertCompanyBRowHidden(route('api.locations.index'), $companyBLocation, $caller);}diff --git a/tests/Feature/Fmcs/LocationScopingSettingTest.php b/tests/Feature/Fmcs/LocationScopingSettingTest.phpnew file mode 100644index 0000000000..655be1270f--- /dev/null+++ b/tests/Feature/Fmcs/LocationScopingSettingTest.php@@ -0,0 +1,254 @@+<?php++namespace Tests\Feature\Fmcs;++use App\Models\Asset;+use App\Models\Company;+use App\Models\Location;+use App\Models\User;+use Tests\TestCase;++/**+ * Regression coverage for the v8.7.0 asset check-in regression where+ * scoped admins (State / Campus tier — non-superusers with limited+ * company_user pivot memberships) started hitting "The assigned+ * location could not be found" on the check-in submit path.+ *+ * Root cause: Company::scopeCompanyablesDirectly() was applying the+ * CompanyableScope to the locations table unconditionally under FMCS,+ * regardless of the scope_locations_fmcs setting. Snipe-IT already+ * treats scope_locations_fmcs as the opt-in switch for location+ * tenant-scoping elsewhere (CompanyableTrait::canCheckoutTo(),+ * LocationsController write paths, the location select2 endpoint's+ * parent-hierarchy filter). The global scope needed to as well.+ *+ * Behavior pinned here:+ * - scope_locations_fmcs OFF (default): all locations visible to+ * every actor, regardless of pivot memberships. This is the+ * "shared locations across tenants" configuration.+ * - scope_locations_fmcs ON: the CompanyableScope fires normally and+ * locations are filtered to the actor's pivot memberships (with+ * the null-company / floater rules the scope already implements).+ *+ * The end-to-end check-in HTTP regression is covered in+ * tests/Feature/Assets/Ui/CheckinAssetLocationScopingTest.php; this+ * file gates the scope semantics that make it work.+ */+class LocationScopingSettingTest extends TestCase+{+ private Company $companyA;++ private Company $companyB;++ private User $scopedActor;++ protected function setUp(): void+ {+ parent::setUp();++ [$this->companyA, $this->companyB] = Company::factory()->count(2)->create();++ // Scoped admin pinned to company A only. Under strict FMCS this+ // actor sees only company A's rows through any Companyable model.+ // For the locations table specifically, whether they can see+ // company B's locations OR null-company locations depends on the+ // scope_locations_fmcs setting.+ $this->scopedActor = $this->companyA->users()->save(User::factory()->make());++ // NOTE: don't actingAs here. Seeding the fixture locations first+ // needs to happen without the fmcs_company validator blocking a+ // non-superuser save under strict FMCS. Each test enables the+ // needed settings, seeds its fixtures WITHOUT an authed user,+ // then swaps to the scoped actor before asserting scope behavior.+ }++ /**+ * Seed a location without going through Watson\ValidatingTrait's+ * fmcs_company validator, which would reject a null-company save+ * under strict FMCS from a non-superuser and prevent the fixture+ * from landing. We are asserting read-side scope behavior; the+ * write-side rule is a separate concern with its own tests. Turn+ * off model-level validation for the seed, then re-enable it.+ */+ private function seedLocation(?int $companyId): Location+ {+ $location = Location::factory()->make();+ $location->company_id = $companyId;+ $location->setValidating(false);+ $location->save();+ $location->setValidating(true);++ return $location->refresh();+ }++ // -----------------------------------------------------------------+ // scope_locations_fmcs OFF: locations are shared across tenants.+ // Every actor sees every location. This is the "opted out of+ // location scoping" configuration many installs run.+ // -----------------------------------------------------------------++ public function test_scoped_actor_sees_null_company_location_when_location_scoping_is_off(): void+ {+ // The customer's exact case: FMCS on, location scoping off,+ // floater off. The scoped actor should still see a location+ // whose Company field is blank.+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $nullCompanyLocation = $this->seedLocation(null);+ $this->actingAs($this->scopedActor);++ $this->assertNotNull(+ Location::find($nullCompanyLocation->id),+ 'Location::find() must resolve the null-company location for a scoped actor under scope_locations_fmcs=0.'+ );+ }++ public function test_scoped_actor_sees_other_company_location_when_location_scoping_is_off(): void+ {+ // Regression: with location scoping off, a company A scoped+ // actor should see a location assigned to company B. The+ // "shared locations across tenants" semantic requires it.+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $companyBLocation = $this->seedLocation($this->companyB->id);+ $this->actingAs($this->scopedActor);++ $this->assertNotNull(+ Location::find($companyBLocation->id),+ 'Location::find() must resolve a cross-company location for a scoped actor under scope_locations_fmcs=0.'+ );+ }++ public function test_scoped_actor_sees_null_company_location_in_floater_mode_with_scoping_off(): void+ {+ // Floater is orthogonal to scope_locations_fmcs. With scope off,+ // all locations are visible regardless of floater state — no+ // regression on this axis.+ $this->settings->enableFloaterMode();++ $nullCompanyLocation = $this->seedLocation(null);+ $this->actingAs($this->scopedActor);++ $this->assertNotNull(Location::find($nullCompanyLocation->id));+ }++ // -----------------------------------------------------------------+ // scope_locations_fmcs ON: locations are tenant-scoped as before.+ // The scope fires normally, and the pre-existing null-company /+ // floater / pivot-hierarchy behavior kicks in. Regression guards+ // for the strict-scoping path so this fix does not silently open+ // the door on installs that WANT location isolation.+ // -----------------------------------------------------------------++ public function test_scoped_actor_cannot_see_other_company_location_when_location_scoping_is_on(): void+ {+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();+ $this->settings->disableFloaterMode();++ $companyBLocation = $this->seedLocation($this->companyB->id);+ $this->actingAs($this->scopedActor);++ $this->assertNull(+ Location::find($companyBLocation->id),+ 'Under scope_locations_fmcs=1, a scoped actor must not see a cross-company location.'+ );+ }++ public function test_scoped_actor_sees_own_company_location_when_location_scoping_is_on(): void+ {+ // Positive-side regression guard for the strict-scoping path.+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();+ $this->settings->disableFloaterMode();++ $companyALocation = $this->seedLocation($this->companyA->id);+ $this->actingAs($this->scopedActor);++ $this->assertNotNull(Location::find($companyALocation->id));+ }++ public function test_scoped_actor_cannot_see_null_company_location_in_strict_mode_with_scoping_on(): void+ {+ // scope on + floater off + null-company location = hidden. The+ // pre-fix "null is safe" branch does not apply here; that lives+ // in the action_logs / global-config path, not the locations+ // path.+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();+ $this->settings->disableFloaterMode();++ $nullCompanyLocation = $this->seedLocation(null);+ $this->actingAs($this->scopedActor);++ $this->assertNull(Location::find($nullCompanyLocation->id));+ }++ public function test_scoped_actor_sees_null_company_location_in_floater_mode_with_scoping_on(): void+ {+ // Floater flips the null-company visibility. Documented behavior.+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();+ $this->settings->enableFloaterMode();++ $nullCompanyLocation = $this->seedLocation(null);+ $this->actingAs($this->scopedActor);++ $this->assertNotNull(Location::find($nullCompanyLocation->id));+ }++ // -----------------------------------------------------------------+ // Superuser + FMCS-off baseline: not the target of the fix but+ // guards against accidentally over-tightening.+ // -----------------------------------------------------------------++ public function test_superuser_sees_all_locations_regardless_of_setting(): void+ {+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();+ $this->settings->disableFloaterMode();++ $companyBLocation = $this->seedLocation($this->companyB->id);+ $nullCompanyLocation = $this->seedLocation(null);++ $superuser = User::factory()->superuser()->create();+ $this->actingAs($superuser);++ $this->assertNotNull(Location::find($companyBLocation->id));+ $this->assertNotNull(Location::find($nullCompanyLocation->id));+ }++ public function test_fmcs_off_baseline_all_locations_visible(): void+ {+ // With FMCS entirely off the scope short-circuits at the top+ // of Company::scopeCompanyables. Guard against over-tightening.+ $this->settings->disableMultipleFullCompanySupport();++ $companyBLocation = $this->seedLocation($this->companyB->id);+ $nullCompanyLocation = $this->seedLocation(null);+ $this->actingAs($this->scopedActor);++ $this->assertNotNull(Location::find($companyBLocation->id));+ $this->assertNotNull(Location::find($nullCompanyLocation->id));+ }++ // -----------------------------------------------------------------+ // Cross-model regression guard: the fix must ONLY affect the+ // locations table. Asset / Accessory / etc scoping must be+ // unchanged.+ // -----------------------------------------------------------------++ public function test_other_companyable_models_still_scoped_when_only_location_scoping_is_off(): void+ {+ // scope_locations_fmcs off must not accidentally disable+ // scoping on OTHER companyable tables. Assets in company B+ // should stay hidden from a company A scoped actor.+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $companyBAsset = Asset::factory()->for($this->companyB)->create();+ $this->actingAs($this->scopedActor);++ $this->assertNull(+ Asset::find($companyBAsset->id),+ 'scope_locations_fmcs=0 must only affect the locations table; asset scoping must remain intact.'+ );+ }+}