Skip to content

A free open source IT asset/license management system

Fixed location scoping on user edit

snipe712256211da8
diff --git a/resources/views/users/edit.blade.php b/resources/views/users/edit.blade.php
index 7d9aa9edf4..cd7afb6923 100755
--- a/resources/views/users/edit.blade.php
+++ b/resources/views/users/edit.blade.php
@@ -356,9 +356,9 @@
{{-- selected reads from the company_user pivot only; the legacy
users.company_id scalar can lag behind (LDAP sync, pre-observer
rows, etc.) so we never fall back to it. --}}
- @foreach (old('company_ids', $user->companies->pluck('id')->toArray()) as $company_id)
- <option value="{{ $company_id }}" selected="selected" role="option" aria-selected="true">
- {{ \App\Models\Company::find($company_id)?->name }}
+ @foreach (old('company_ids', $user->companies->pluck('id')->toArray()) as $selectedCompanyId)
+ <option value="{{ $selectedCompanyId }}" selected="selected" role="option" aria-selected="true">
+ {{ \App\Models\Company::find($selectedCompanyId)?->name }}
</option>
@endforeach
</select>
diff --git a/tests/Feature/Users/Ui/UpdateUserTest.php b/tests/Feature/Users/Ui/UpdateUserTest.php
index 17c011fdd7..53e7969fc6 100644
--- a/tests/Feature/Users/Ui/UpdateUserTest.php
+++ b/tests/Feature/Users/Ui/UpdateUserTest.php
@@ -25,6 +25,37 @@ class UpdateUserTest extends TestCase
->assertOk();
}
+ public function test_edit_form_does_not_leak_company_id_into_location_select()
+ {
+ // Regression: the company_ids <select> looped with `@foreach (... as $company_id)`,
+ // and PHP's foreach doesn't scope the loop variable, so $company_id persisted into
+ // the outer view. The @include of location-select then inherited it and emitted
+ // data-company-id on the location dropdown, which restricted the AJAX call to
+ // only that one company's locations.
+ $companyA = Company::factory()->create();
+ $target = User::factory()->create();
+ $target->companies()->sync([$companyA->id]);
+
+ $this->actingAs(User::factory()->superuser()->create());
+
+ $html = $this->get(route('users.edit', $target))->assertOk()->getContent();
+
+ $this->assertStringNotContainsString(
+ 'data-company-id',
+ $this->extractLocationSelect($html),
+ 'Location select must not emit data-company-id from a leaked foreach variable.'
+ );
+ }
+
+ private function extractLocationSelect(string $html): string
+ {
+ if (! preg_match('/<select[^>]*id="location_id_location_select"[^>]*>/', $html, $matches)) {
+ $this->fail('Location select element not found in user edit HTML.');
+ }
+
+ return $matches[0];
+ }
+
public function test_uncompanied_user_can_edit_their_own_record_with_fmcs_on_and_floater_off()
{
// Regression: with FMCS on + floater off, the CompanyableScope lets an