A free open source IT asset/license management system
Fixed #19569 - preserve original companies on user edit from non-superadmin
snipef4bae4423264
diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.phpindex b07685b03a..410565da69 100644--- a/app/Http/Controllers/Api/UsersController.php+++ b/app/Http/Controllers/Api/UsersController.php@@ -739,11 +739,14 @@ class UsersController extends Controller$user->groups()->sync($request->input('groups'));}- // company_ids (new format) = full replacement sync.+ // company_ids (new format) = full replacement sync, with+ // the target's invisible-to-editor memberships preserved+ // so a scoped editor's save can't detach the target from+ // tenants outside the editor's own membership.// Legacy company_id = add without removing other associations.if ($request->has('company_ids')) {- $companyIds = array_filter(array_map('intval', (array) $request->input('company_ids')));- $user->syncCompaniesWithLogging(Company::getIdsForCurrentUser($companyIds));+ $companyIds = array_values(array_filter(array_map('intval', (array) $request->input('company_ids'))));+ $user->syncCompaniesPreservingInvisibleTo(auth()->user(), $companyIds);} elseif ($request->filled('company_id')) {$filtered = Company::getIdsForCurrentUser([(int) $request->input('company_id')]);if (! empty($filtered)) {diff --git a/app/Http/Controllers/Users/UsersController.php b/app/Http/Controllers/Users/UsersController.phpindex 3611cae3c0..71ca4061e3 100755--- a/app/Http/Controllers/Users/UsersController.php+++ b/app/Http/Controllers/Users/UsersController.php@@ -350,7 +350,7 @@ class UsersController extends Controllersession()->put(['redirect_option' => $request->input('redirect_option')]);if ($user->save()) {- $user->syncCompaniesWithLogging(Company::getIdsForCurrentUser($companyIds));+ $user->syncCompaniesPreservingInvisibleTo(auth()->user(), $companyIds);// Redirect to the user pagereturn Helper::getRedirectOption($request, $user->id, 'Users')diff --git a/app/Models/User.php b/app/Models/User.phpindex cc6720ab87..4c954ef5f6 100644--- a/app/Models/User.php+++ b/app/Models/User.php@@ -835,6 +835,49 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo$logAction->logaction('update');}+ /**+ * FMCS-safe wrapper around syncCompaniesWithLogging() for the user+ * update path. Folds the target's memberships in companies the+ * editor cannot see back into the submitted list before syncing,+ * so a scoped editor's save can't silently detach the target from+ * tenants outside the editing user's own membership.+ *+ * Superuser editors skip the merge because they can see every+ * company, so their submission already represents full intent.+ */+ public function syncCompaniesPreservingInvisibleTo(User $editor, array $submittedCompanyIds): void+ {+ $submitted = array_map('intval', $submittedCompanyIds);++ // FMCS is off, so every user can see every company+ // and there is no invisible-to-editor set to preserve. Superuser+ // 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()) {+ $this->syncCompaniesWithLogging($submitted);++ return;+ }++ $editorVisible = $editor->companies()+ ->pluck('companies.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)+ ->all();++ $this->syncCompaniesWithLogging(+ array_values(array_unique(array_merge($submittedVisible, $invisiblePreserved))),+ );+ }+/*** Update the legacy users.company_id column so that it mirrors the* company_user pivot: empty pivot writes NULL; a non-empty pivot writesdiff --git a/tests/Unit/Users/SyncCompaniesPreservingInvisibleTest.php b/tests/Unit/Users/SyncCompaniesPreservingInvisibleTest.phpnew file mode 100644index 0000000000..d9590fccba--- /dev/null+++ b/tests/Unit/Users/SyncCompaniesPreservingInvisibleTest.php@@ -0,0 +1,142 @@+<?php++namespace Tests\Unit\Users;++use App\Models\Company;+use App\Models\User;+use PHPUnit\Framework\Attributes\Test;+use Tests\TestCase;++/**+ * Regression coverage for GH #19569: a scoped editor's user save+ * silently detached the target from every FMCS tenant the editor was+ * not a member of, because the update path did a full-replacement+ * sync on the editor's submitted list without folding the target's+ * invisible-to-editor memberships back in.+ */+class SyncCompaniesPreservingInvisibleTest extends TestCase+{+ #[Test]+ public function scoped_editor_save_preserves_target_memberships_editor_cannot_see(): void+ {+ // Editor is a member of Company A only; target belongs to+ // A + B + C + D. On save, the form only offered A so the+ // submission is [A]. Without the merge, sync would strip+ // B / C / D.+ $this->settings->enableMultipleFullCompanySupport();++ [$companyA, $companyB, $companyC, $companyD] = Company::factory()->count(4)->create();++ $editor = User::factory()->create();+ $editor->companies()->sync([$companyA->id]);++ $target = User::factory()->create();+ $target->companies()->sync([$companyA->id, $companyB->id, $companyC->id, $companyD->id]);++ $target->syncCompaniesPreservingInvisibleTo($editor, [$companyA->id]);++ $this->assertEqualsCanonicalizing(+ [$companyA->id, $companyB->id, $companyC->id, $companyD->id],+ $target->companies()->pluck('companies.id')->all(),+ );+ }++ #[Test]+ public function fmcs_off_bypasses_the_merge_and_does_a_straight_sync(): void+ {+ // FMCS is off, so every user can see every company. There's+ // no invisible-to-editor set to preserve, and the editor's+ // submission must land exactly as submitted even if the+ // editor's own pivot is empty.+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $editor = User::factory()->create();+ $target = User::factory()->create();+ $target->companies()->sync([$companyA->id]);++ $target->syncCompaniesPreservingInvisibleTo($editor, [$companyB->id]);++ $this->assertSame([$companyB->id], $target->companies()->pluck('companies.id')->all());+ }++ #[Test]+ public function superuser_editor_gets_full_replacement_semantics(): void+ {+ [$companyA, $companyB, $companyC] = Company::factory()->count(3)->create();++ $editor = User::factory()->superuser()->create();+ $target = User::factory()->create();+ $target->companies()->sync([$companyA->id, $companyB->id, $companyC->id]);++ $target->syncCompaniesPreservingInvisibleTo($editor, [$companyA->id]);++ $this->assertSame([$companyA->id], $target->companies()->pluck('companies.id')->all());+ }++ #[Test]+ public function editor_can_still_remove_a_company_they_are_a_member_of_from_target(): void+ {+ // Editor is in A + B, target in A + B + C. Editor drops B+ // from the picker and saves ([A]). Expected: pivot is [A, C].+ $this->settings->enableMultipleFullCompanySupport();++ [$companyA, $companyB, $companyC] = Company::factory()->count(3)->create();++ $editor = User::factory()->create();+ $editor->companies()->sync([$companyA->id, $companyB->id]);++ $target = User::factory()->create();+ $target->companies()->sync([$companyA->id, $companyB->id, $companyC->id]);++ $target->syncCompaniesPreservingInvisibleTo($editor, [$companyA->id]);++ $this->assertEqualsCanonicalizing(+ [$companyA->id, $companyC->id],+ $target->companies()->pluck('companies.id')->all(),+ );+ }++ #[Test]+ public function editor_cannot_smuggle_a_company_they_are_not_a_member_of(): void+ {+ // Editor is in A. Target is in A + B. Editor's form submits+ // [A, C] where C is a company the editor is not a member of.+ // C must be dropped; B must be preserved.+ $this->settings->enableMultipleFullCompanySupport();++ [$companyA, $companyB, $companyC] = Company::factory()->count(3)->create();++ $editor = User::factory()->create();+ $editor->companies()->sync([$companyA->id]);++ $target = User::factory()->create();+ $target->companies()->sync([$companyA->id, $companyB->id]);++ $target->syncCompaniesPreservingInvisibleTo($editor, [$companyA->id, $companyC->id]);++ $this->assertEqualsCanonicalizing(+ [$companyA->id, $companyB->id],+ $target->companies()->pluck('companies.id')->all(),+ );+ }++ #[Test]+ public function empty_submission_still_preserves_invisible_memberships(): void+ {+ // Editor is in A, target in A + B. Editor unchecks A and+ // saves ([]). Target pivot should end up [B].+ $this->settings->enableMultipleFullCompanySupport();++ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $editor = User::factory()->create();+ $editor->companies()->sync([$companyA->id]);++ $target = User::factory()->create();+ $target->companies()->sync([$companyA->id, $companyB->id]);++ $target->syncCompaniesPreservingInvisibleTo($editor, []);++ $this->assertSame([$companyB->id], $target->companies()->pluck('companies.id')->all());+ }+}