A free open source IT asset/license management system
Small FMCS fix
snipeb96f5664db09
diff --git a/app/Models/Company.php b/app/Models/Company.phpindex 665cbc17f4..15a3052d09 100644--- a/app/Models/Company.php+++ b/app/Models/Company.php@@ -600,11 +600,24 @@ final class Company extends SnipeModel}// Floater: also show null-company users (no pivot rows) to company-scoped actors.+ //+ // The "no pivot rows" branch queries the company_user pivot+ // directly rather than using whereDoesntHave('companies'). Going+ // through the relation applies the companies-table CompanyableScope+ // to the subquery, which restricts the JOIN to the caller's own+ // companies. A user whose only pivot rows point at OTHER companies+ // would then look pivot-less under that scoping and get picked up+ // by the floater branch — leaking cross-company users into the+ // caller's list. Reading the pivot directly bypasses that+ // recursive scope and matches the intended "genuinely no pivot+ // rows at all" semantics.if ($floater) {return $query->where(function ($q) use ($companyIds) {$q->whereIn('users.id', function ($sub) use ($companyIds) {$sub->select('user_id')->from('company_user')->whereIn('company_id', $companyIds);- })->orWhereDoesntHave('companies');+ })->orWhereNotIn('users.id', function ($sub) {+ $sub->select('user_id')->from('company_user');+ });});}diff --git a/tests/Feature/Users/Api/IndexUsersTest.php b/tests/Feature/Users/Api/IndexUsersTest.phpindex a9d4013298..4ace1214d2 100644--- a/tests/Feature/Users/Api/IndexUsersTest.php+++ b/tests/Feature/Users/Api/IndexUsersTest.php@@ -3,6 +3,7 @@namespace Tests\Feature\Users\Api;use App\Models\Asset;+use App\Models\Company;use App\Models\Location;use App\Models\Maintenance;use App\Models\User;@@ -138,6 +139,43 @@ class IndexUsersTest extends TestCase});}+ /**+ * FMCS + floaters on: a company-scoped, non-superuser caller must not+ * see users pivoted to a different company. They should still see their+ * own company members and floater (null-company) users.+ *+ * Regression pin for the ticket where a company-A user going to the+ * user listing page saw every user in the system. See the same fix in+ * tests/Unit/CompanyScopingTest.php for the model-level equivalent.+ */+ public function test_users_index_hides_other_companies_users_from_company_scoped_caller_in_floater_mode()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $companyACaller = $companyA->users()->save(User::factory()->viewUsers()->make());+ $companyAPeer = $companyA->users()->save(User::factory()->make(['first_name' => 'PeerInA']));+ $companyBUser = $companyB->users()->save(User::factory()->make(['first_name' => 'UserInB']));+ $floater = User::factory()->create(['company_id' => null, 'first_name' => 'FloaterUser']);++ $this->settings->enableFloaterMode();++ $rows = $this->actingAsForApi($companyACaller)+ ->getJson(route('api.users.index'))+ ->assertOk()+ ->json('rows');++ $visibleIds = collect($rows)->pluck('id')->all();++ $this->assertContains($companyACaller->id, $visibleIds);+ $this->assertContains($companyAPeer->id, $visibleIds);+ $this->assertContains($floater->id, $visibleIds);+ $this->assertNotContains(+ $companyBUser->id,+ $visibleIds,+ 'Company B user leaked into a company A caller\'s user list under FMCS + floater mode.',+ );+ }+public function test_returns_result_via_filter(){diff --git a/tests/Unit/CompanyScopingTest.php b/tests/Unit/CompanyScopingTest.phpindex 1c1099139b..20691257e7 100644--- a/tests/Unit/CompanyScopingTest.php+++ b/tests/Unit/CompanyScopingTest.php@@ -233,6 +233,33 @@ class CompanyScopingTest extends TestCase$this->assertCannotSee($nullCompanyUser);}+ /**+ * FMCS + floaters on: a company A caller should see themselves and+ * null-company (floater) users, but NOT a user whose pivot points at a+ * different company.+ *+ * Regression pin for support ticket 56305. Root cause was that the+ * "no pivot rows at all" branch of the floater query in+ * Company::scopeCompanyablesDirectly went through+ * whereDoesntHave('companies'), whose relation subquery had the+ * companies-table CompanyableScope applied recursively. That scope+ * filtered the JOIN to the caller's own companies, so a user pivoted+ * only to OUT-OF-SCOPE companies looked pivot-less and got picked up+ * by the floater branch. Fix reads the company_user pivot directly.+ */+ public function test_company_scoped_user_cannot_see_users_from_other_companies_in_floater_mode()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $companyAUser = $companyA->users()->save(User::factory()->make());+ $companyBUser = $companyB->users()->save(User::factory()->make());++ $this->settings->enableFloaterMode();++ $this->actingAs($companyAUser);+ $this->assertCannotSee($companyBUser);+ }+private function assertCanSee(Model $model){$this->assertTrue(