Skip to content

A free open source IT asset/license management system

One more user FMCS fix

snipef2d141ccb20e
diff --git a/app/Models/User.php b/app/Models/User.php
index 4c954ef5f6..f1f6b93239 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -854,23 +854,34 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
// editors also skip because their submission already
// represents full intent across all tenants.
$fmcsOn = (bool) Setting::getSettings()->full_multiple_companies_support;
- if (!$fmcsOn || $editor->isSuperUser()) {
+ if (! $fmcsOn || $editor->isSuperUser()) {
$this->syncCompaniesWithLogging($submitted);
return;
}
- $editorVisible = $editor->companies()
- ->pluck('companies.id')
- ->map(fn($id) => (int) $id)
+ // Read editor + target memberships directly from the pivot so
+ // Company's global CompanyableScope does not filter Companies
+ // down to what the acting user (the editor) can see before we
+ // compute the invisible-to-editor set. Going through
+ // $editor->companies() / $this->companies() under a scoped
+ // non-superuser editor would return only the editor's own
+ // companies from the Companies side of the join, which turns
+ // the whereNotIn below into an empty result and drops every
+ // preserved company. See GH #19569.
+ $editorVisible = DB::table('company_user')
+ ->where('user_id', $editor->id)
+ ->pluck('company_id')
+ ->map(fn ($id) => (int) $id)
->all();
$submittedVisible = array_values(array_intersect($submitted, $editorVisible));
- $invisiblePreserved = $this->companies()
- ->whereNotIn('companies.id', $editorVisible)
- ->pluck('companies.id')
- ->map(fn($id) => (int) $id)
+ $invisiblePreserved = DB::table('company_user')
+ ->where('user_id', $this->id)
+ ->whereNotIn('company_id', $editorVisible)
+ ->pluck('company_id')
+ ->map(fn ($id) => (int) $id)
->all();
$this->syncCompaniesWithLogging(
diff --git a/tests/Unit/Users/SyncCompaniesPreservingInvisibleTest.php b/tests/Unit/Users/SyncCompaniesPreservingInvisibleTest.php
index d9590fccba..cd526151b9 100644
--- a/tests/Unit/Users/SyncCompaniesPreservingInvisibleTest.php
+++ b/tests/Unit/Users/SyncCompaniesPreservingInvisibleTest.php
@@ -120,6 +120,54 @@ class SyncCompaniesPreservingInvisibleTest extends TestCase
);
}
+ #[Test]
+ public function scoped_editor_acting_as_user_preserves_invisible_memberships_from_pivot_read(): void
+ {
+ // Regression for the second hit on GH #19569 (swift2512). The
+ // earlier tests passed because they didn't set the acting
+ // user, so Company's CompanyableScope short-circuited on the
+ // no-auth branch and both editor + target reads through
+ // Eloquent came back unscoped. In the controller path the
+ // editor IS the acting user, so the scope filters Company
+ // rows to the editor's own pivot before whereNotIn runs,
+ // and the "invisible-to-editor" preservation set collapses
+ // to empty. Reads must go through the pivot table directly.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB, $companyC, $companyD, $companyE] = Company::factory()->count(5)->create();
+
+ $editor = User::factory()->create();
+ $editor->companies()->sync([$companyA->id, $companyB->id]);
+
+ $target = User::factory()->create();
+ $target->companies()->sync([$companyA->id, $companyC->id, $companyD->id, $companyE->id]);
+
+ // Act AS the editor so the CompanyableScope on Company is
+ // active. The old implementation lost target's [C, D, E]
+ // memberships here.
+ $this->actingAs($editor);
+
+ // Submission mirrors swift's scenario: no changes, so the
+ // form re-submits whatever the editor was able to see for
+ // the target (Company A only, since scope filters everything
+ // else out on the edit form's pre-populate query).
+ $target->syncCompaniesPreservingInvisibleTo($editor, [$companyA->id]);
+
+ // Assert directly against the pivot so the still-acting-as
+ // editor doesn't pull the read back through
+ // CompanyableScope and mask what the pivot actually holds.
+ $pivotRows = \DB::table('company_user')
+ ->where('user_id', $target->id)
+ ->pluck('company_id')
+ ->map(fn ($id) => (int) $id)
+ ->all();
+
+ $this->assertEqualsCanonicalizing(
+ [$companyA->id, $companyC->id, $companyD->id, $companyE->id],
+ $pivotRows,
+ );
+ }
+
#[Test]
public function empty_submission_still_preserves_invisible_memberships(): void
{