A free open source IT asset/license management system
Scoping: Fixed #19187 - properly scope users within views
sniped922913addcd
diff --git a/app/Models/Company.php b/app/Models/Company.phpindex b4a90341ea..2370653768 100644--- a/app/Models/Company.php+++ b/app/Models/Company.php@@ -237,9 +237,9 @@ final class Company extends SnipeModel->pluck('company_id')->toArray();- // A user with no pivot rows is a null-company user; no intersection is possible.+ // A null-company user (no pivot rows) is accessible in floater mode.if (empty($companyableCompanyIds)) {- return false;+ return (bool) Setting::getSettings()->null_company_is_floater;}return ! empty(array_intersect($userCompanyIds, $companyableCompanyIds));@@ -249,6 +249,11 @@ final class Company extends SnipeModel? $companyable->id: $companyable->company_id;+ // Null-company items are accessible to company-scoped users only when floater is on.+ if (is_null($companyable_company_id)) {+ return (bool) Setting::getSettings()->null_company_is_floater;+ }+return in_array($companyable_company_id, $userCompanyIds);}@@ -404,21 +409,26 @@ final class Company extends SnipeModel// since a user may belong to multiple companies and company_id alone is insufficient.if ($query->getModel()->getTable() == 'users') {if (empty($companyIds)) {- // Floater: actor has no company and is unrestricted — see everyone.+ // Floater: null-company actor is unrestricted — see everyone.if ($floater) {return $query;}- // No pivot memberships: mirror old null-company behavior — show only users- // who are also not in any company via the pivot.+ // No pivot memberships and floater off: show only other null-company users.return $query->whereNotIn('users.id', function ($sub) {$sub->select('user_id')->from('company_user');});}- // Floater mode governs whether null-company users can *receive* items (canReceiveFromCompany),- // not whether company-scoped actors can *manage* them. A company-scoped actor must only- // ever see users who share at least one company via the pivot, regardless of floater mode.+ // Floater: also show null-company users (no pivot rows) to company-scoped actors.+ 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');+ });+ }+return $query->whereIn('users.id', function ($sub) use ($companyIds) {$sub->select('user_id')->from('company_user')->whereIn('company_id', $companyIds);});@@ -429,7 +439,7 @@ final class Company extends SnipeModel$table = ($table_name) ? $table_name.'.' : $query->getModel()->getTable().'.';if (empty($companyIds)) {- // Floater: actor has no company and is unrestricted — see everything.+ // Floater: null-company actor sees all items (they are unrestricted for assets/etc).if ($floater) {return $query;}diff --git a/tests/Unit/CompanyScopingTest.php b/tests/Unit/CompanyScopingTest.phpindex 20debb554a..1c1099139b 100644--- a/tests/Unit/CompanyScopingTest.php+++ b/tests/Unit/CompanyScopingTest.php@@ -209,7 +209,7 @@ class CompanyScopingTest extends TestCase$this->assertCanSee($companyItem);}- public function test_company_scoped_user_cannot_see_null_company_users_in_floater_mode()+ public function test_company_scoped_user_can_see_null_company_users_in_floater_mode(){$company = Company::factory()->create();$companyUser = $company->users()->save(User::factory()->make());@@ -218,7 +218,7 @@ class CompanyScopingTest extends TestCase$this->settings->enableFloaterMode();$this->actingAs($companyUser);- $this->assertCannotSee($nullCompanyUser);+ $this->assertCanSee($nullCompanyUser);}public function test_company_scoped_user_cannot_see_null_company_users_in_strict_mode()