A free open source IT asset/license management system
Updated tests
snipe77f9b49f7c90
diff --git a/app/Models/Company.php b/app/Models/Company.phpindex 15a3052d09..da3696633c 100644--- a/app/Models/Company.php+++ b/app/Models/Company.php@@ -599,28 +599,14 @@ 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);- })->orWhereNotIn('users.id', function ($sub) {- $sub->select('user_id')->from('company_user');- });- });- }-+ // Users, unlike other companyable items, are NOT treated as+ // floaters just because they have no company. A company-scoped+ // caller sees only users whose pivot rows include one of their+ // companies. Floater users (no pivot rows) are only visible to+ // other floater users (caller with no pivot rows, handled above)+ // and superusers (handled at the top of scopeCompanyables).+ // Same query in floater and strict mode for the company-scoped+ // caller: item-level floater rules don't apply to users.return $query->whereIn('users.id', function ($sub) use ($companyIds) {$sub->select('user_id')->from('company_user')->whereIn('company_id', $companyIds);});@@ -669,14 +655,15 @@ final class Company extends SnipeModel*/public static function scopeUsersByCompanyIds($query, array $companyIds): mixed{- if (Setting::getSettings()->null_company_is_floater) {- return $query->where(function ($q) use ($companyIds) {- $q->whereHas('companies', fn ($q2) => $q2->whereIn('companies.id', $companyIds))- ->orWhereDoesntHave('companies');- });- }-- return $query->whereHas('companies', fn ($q) => $q->whereIn('companies.id', $companyIds));+ // Users are never treated as floaters here. A caller filtering the+ // users list by a specific set of company ids gets exactly those+ // users, regardless of null_company_is_floater. Item-level floater+ // rules (visible to any caller if the item has no company) don't+ // apply to users. Superuser bypass happens upstream in+ // scopeCompanyables and in the caller's own conditional.+ return $query->whereIn('users.id', function ($sub) use ($companyIds) {+ $sub->select('user_id')->from('company_user')->whereIn('company_id', $companyIds);+ });}/**diff --git a/tests/Feature/Users/Api/IndexUsersTest.php b/tests/Feature/Users/Api/IndexUsersTest.phpindex 4ace1214d2..9df6570895 100644--- a/tests/Feature/Users/Api/IndexUsersTest.php+++ b/tests/Feature/Users/Api/IndexUsersTest.php@@ -141,14 +141,16 @@ 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.+ * see users pivoted to a different company, and must NOT see floater+ * (null-company) users. Floater USERS are only visible to other+ * floaters and to superusers, unlike floater ASSETS / LOCATIONS which+ * are visible to everyone under floater mode.** 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.+ * user listing page saw every user in the system. See the same fix+ * at 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()+ public function test_users_index_hides_other_companies_and_floater_users_from_company_scoped_caller(){[$companyA, $companyB] = Company::factory()->count(2)->create();@@ -168,12 +170,8 @@ class IndexUsersTest extends TestCase$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.',- );+ $this->assertNotContains($companyBUser->id, $visibleIds, 'Company B user leaked to a company A caller.');+ $this->assertNotContains($floater->id, $visibleIds, 'Floater user leaked to a company A caller.');}public function test_returns_result_via_filter()diff --git a/tests/Unit/CompanyScopingTest.php b/tests/Unit/CompanyScopingTest.phpindex 20691257e7..57b63cabcf 100644--- a/tests/Unit/CompanyScopingTest.php+++ b/tests/Unit/CompanyScopingTest.php@@ -209,7 +209,14 @@ class CompanyScopingTest extends TestCase$this->assertCanSee($companyItem);}- public function test_company_scoped_user_can_see_null_company_users_in_floater_mode()+ /**+ * Unlike other companyable items, USERS are never floaters from a+ * company-scoped caller's perspective. A null-company user is only+ * visible to other null-company users (see the "floater sees everyone"+ * branch handled elsewhere) and superusers. Confirms the policy in+ * both floater and strict modes.+ */+ public function test_company_scoped_user_cannot_see_null_company_users_in_floater_mode(){$company = Company::factory()->create();$companyUser = $company->users()->save(User::factory()->make());@@ -218,7 +225,7 @@ class CompanyScopingTest extends TestCase$this->settings->enableFloaterMode();$this->actingAs($companyUser);- $this->assertCanSee($nullCompanyUser);+ $this->assertCannotSee($nullCompanyUser);}public function test_company_scoped_user_cannot_see_null_company_users_in_strict_mode()@@ -234,30 +241,47 @@ class CompanyScopingTest extends TestCase}/**- * 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.+ * Floater callers are unrestricted, so they see everyone (their own+ * kind and every company-scoped user). Superuser bypass is handled+ * upstream in scopeCompanyables.+ */+ public function test_null_company_user_can_see_null_company_users_in_floater_mode()+ {+ $company = Company::factory()->create();+ $companyUser = $company->users()->save(User::factory()->make());+ $nullCompanyCaller = User::factory()->create(['company_id' => null]);+ $anotherNullCompanyUser = User::factory()->create(['company_id' => null]);++ $this->settings->enableFloaterMode();++ $this->actingAs($nullCompanyCaller);+ $this->assertCanSee($anotherNullCompanyUser);+ $this->assertCanSee($companyUser);+ }++ /**+ * FMCS + floaters on: a company A caller sees ONLY users pivoted to+ * their own company (or their reachable hierarchy). They do NOT see+ * users from other companies, and they do NOT see null-company (floater)+ * users. Regression pin for support ticket 56305 and the follow-up+ * clarification that floater USERS are not visible to company-scoped+ * callers, unlike floater ASSETS / LOCATIONS / etc.*/- public function test_company_scoped_user_cannot_see_users_from_other_companies_in_floater_mode()+ public function test_company_scoped_user_only_sees_own_company_users_in_floater_mode(){[$companyA, $companyB] = Company::factory()->count(2)->create();- $companyAUser = $companyA->users()->save(User::factory()->make());+ $companyACaller = $companyA->users()->save(User::factory()->make());+ $companyAPeer = $companyA->users()->save(User::factory()->make());$companyBUser = $companyB->users()->save(User::factory()->make());+ $floaterUser = User::factory()->create(['company_id' => null]);$this->settings->enableFloaterMode();- $this->actingAs($companyAUser);+ $this->actingAs($companyACaller);+ $this->assertCanSee($companyAPeer);$this->assertCannotSee($companyBUser);+ $this->assertCannotSee($floaterUser);}private function assertCanSee(Model $model)