Skip to content

A free open source IT asset/license management system

Fixed gcm2-5g9j - check scope on FMCS+locations

snipe2e0a3d95e129
diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php
index 52a578ea02..2ce570d30c 100755
--- a/app/Http/Controllers/ProfileController.php
+++ b/app/Http/Controllers/ProfileController.php
@@ -18,6 +18,7 @@ use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
@@ -81,6 +82,19 @@ class ProfileController extends Controller
}
if (Gate::allows('self.edit_location') && (! config('app.lock_passwords'))) {
+ /*
+ * Check that the location they're trying to update is one they have access to.
+ */
+ Validator::make(
+ [
+ 'location_id' => $request->input('location_id'),
+ 'company_ids' => $user->companies()->pluck('companies.id')->toArray(),
+ ],
+ [
+ 'location_id' => 'nullable|integer|exists:locations,id|fmcs_location',
+ ],
+ )->validate();
+
$user->location_id = $request->input('location_id');
}
diff --git a/tests/Feature/Fmcs/FmcsSelfServiceLocationTest.php b/tests/Feature/Fmcs/FmcsSelfServiceLocationTest.php
new file mode 100644
index 0000000000..19ba62db72
--- /dev/null
+++ b/tests/Feature/Fmcs/FmcsSelfServiceLocationTest.php
@@ -0,0 +1,123 @@
+<?php
+
+namespace Tests\Feature\Fmcs;
+
+use App\Models\Company;
+use App\Models\Location;
+use App\Models\User;
+use Tests\TestCase;
+
+class FmcsSelfServiceLocationTest extends TestCase
+{
+ public function test_self_service_cannot_set_cross_company_location(): void
+ {
+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $actor = User::factory()->canEditOwnLocation()->forCompany($companyA)->create();
+
+ $foreignLocation = Location::factory()->make();
+ $foreignLocation->company_id = $companyB->id;
+ $foreignLocation->setValidating(false);
+ $foreignLocation->save();
+
+ $this->actingAs($actor)
+ ->from(route('profile'))
+ ->post(route('profile'), [
+ 'first_name' => $actor->first_name,
+ 'location_id' => $foreignLocation->id,
+ ])
+ ->assertSessionHasErrors('location_id');
+
+ $this->assertNotSame(
+ $foreignLocation->id,
+ (int) $actor->fresh()->location_id,
+ 'Cross-company location_id must not persist on self-service profile update.',
+ );
+ }
+
+ public function test_self_service_can_set_same_company_location(): void
+ {
+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();
+
+ $company = Company::factory()->create();
+ $actor = User::factory()->canEditOwnLocation()->forCompany($company)->create();
+
+ $location = Location::factory()->make();
+ $location->company_id = $company->id;
+ $location->setValidating(false);
+ $location->save();
+
+ $this->actingAs($actor)
+ ->from(route('profile'))
+ ->post(route('profile'), [
+ 'first_name' => $actor->first_name,
+ 'location_id' => $location->id,
+ ])
+ ->assertSessionHasNoErrors();
+
+ $this->assertSame($location->id, (int) $actor->fresh()->location_id);
+ }
+
+ public function test_self_service_null_location_passes(): void
+ {
+ /*
+ * Clearing the location (user has one, wants none) must stay
+ * possible. The nullable guard on the fmcs rule chain already
+ * does this, pin the behavior.
+ */
+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();
+
+ $company = Company::factory()->create();
+ $location = Location::factory()->make();
+ $location->company_id = $company->id;
+ $location->setValidating(false);
+ $location->save();
+
+ $actor = User::factory()->canEditOwnLocation()->forCompany($company)->create([
+ 'location_id' => $location->id,
+ ]);
+
+ $this->actingAs($actor)
+ ->from(route('profile'))
+ ->post(route('profile'), [
+ 'first_name' => $actor->first_name,
+ 'location_id' => null,
+ ])
+ ->assertSessionHasNoErrors();
+
+ $this->assertNull($actor->fresh()->location_id);
+ }
+
+ public function test_scope_locations_fmcs_off_does_not_block_cross_company_location(): void
+ {
+ /*
+ * fmcs_location is gated on scope_locations_fmcs = 1. When that
+ * admin setting is off, locations are treated as unscoped and the
+ * cross-company check does not fire on EITHER the admin or
+ * self-service path. Pin that behavior so future tightening is a
+ * deliberate decision rather than a silent regression.
+ */
+ $this->settings->enableMultipleFullCompanySupport();
+ // scope_locations_fmcs remains off (default)
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = User::factory()->canEditOwnLocation()->forCompany($companyA)->create();
+
+ $foreignLocation = Location::factory()->make();
+ $foreignLocation->company_id = $companyB->id;
+ $foreignLocation->setValidating(false);
+ $foreignLocation->save();
+
+ $this->actingAs($actor)
+ ->from(route('profile'))
+ ->post(route('profile'), [
+ 'first_name' => $actor->first_name,
+ 'location_id' => $foreignLocation->id,
+ ])
+ ->assertSessionHasNoErrors();
+
+ $this->assertSame($foreignLocation->id, (int) $actor->fresh()->location_id);
+ }
+}