A free open source IT asset/license management system
FMCS: Location scoping - prevent scoping by location if not enabled
snipec8bb221a16f4
diff --git a/app/Models/Traits/CompanyableTrait.php b/app/Models/Traits/CompanyableTrait.phpindex 4f72478aec..40644eeed7 100644--- a/app/Models/Traits/CompanyableTrait.php+++ b/app/Models/Traits/CompanyableTrait.php@@ -3,6 +3,7 @@namespace App\Models\Traits;use App\Models\CompanyableScope;+use App\Models\Location;use App\Models\Setting;use App\Models\User;use Illuminate\Database\Eloquent\Model;@@ -38,6 +39,7 @@ trait CompanyableTrait* - FMCS is disabled, OR* - this item has no company (uncompanied items are unrestricted), OR* - target is a User whose company pivot includes this item's company, OR+ * - target is a Location and scope_locations_fmcs is disabled, OR* - target has no effective company and null_company_is_floater is enabled, OR* - target's effective company_id exactly matches this item's company_id.*/@@ -61,6 +63,10 @@ trait CompanyableTraitreturn $target->canReceiveFromCompany((int) $this->company_id);}+ if ($target instanceof Location && ! $settings->scope_locations_fmcs) {+ return true;+ }+$targetCompanyId = method_exists($target, 'effectiveFmcsCompanyId')? $target->effectiveFmcsCompanyId(): ($target->company_id ? (int) $target->company_id : null);diff --git a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.phpindex 321efa7ed1..976684d789 100644--- a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php@@ -143,14 +143,34 @@ class AssetCheckoutTest extends TestCaseEvent::assertDispatched(CheckoutableCheckedOut::class);}- public function test_can_checkout_to_child_location_whose_parent_has_matching_company_when_fmcs_enabled()+ public function test_can_checkout_to_uncompanied_location_when_fmcs_enabled_without_location_scoping(){$this->settings->enableMultipleFullCompanySupport();+ $company = Company::factory()->create();+ $location = Location::factory()->create(['company_id' => null]);+ $asset = Asset::factory()->for($company)->create();++ $this->actingAs(User::factory()->superuser()->create())+ ->post(route('hardware.checkout.store', $asset), [+ 'checkout_to_type' => 'location',+ 'assigned_location' => $location->id,+ 'redirect_option' => 'index',+ ])+ ->assertSessionMissing('error')+ ->assertRedirect();++ Event::assertDispatched(CheckoutableCheckedOut::class);+ }++ public function test_can_checkout_to_child_location_whose_parent_has_matching_company_when_location_scoping_enabled()+ {+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();+$company = Company::factory()->create();$parentLocation = Location::factory()->for($company)->create();$childLocation = Location::factory()->create(['parent_id' => $parentLocation->id, 'company_id' => null]);- $asset = Asset::factory()->for($company)->create();+ $asset = Asset::factory()->for($company)->create(['rtd_location_id' => $parentLocation->id]);$this->actingAs(User::factory()->superuser()->create())->post(route('hardware.checkout.store', $asset), [@@ -164,15 +184,15 @@ class AssetCheckoutTest extends TestCaseEvent::assertDispatched(CheckoutableCheckedOut::class);}- public function test_cannot_checkout_to_child_location_whose_parent_has_different_company_when_fmcs_enabled()+ public function test_cannot_checkout_to_child_location_whose_parent_has_different_company_when_location_scoping_enabled(){- $this->settings->enableMultipleFullCompanySupport();+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();$assetCompany = Company::factory()->create();$otherCompany = Company::factory()->create();$parentLocation = Location::factory()->for($otherCompany)->create();$childLocation = Location::factory()->create(['parent_id' => $parentLocation->id, 'company_id' => null]);- $asset = Asset::factory()->for($assetCompany)->create();+ $asset = Asset::factory()->for($assetCompany)->create(['rtd_location_id' => null]);$this->actingAs(User::factory()->superuser()->create())->post(route('hardware.checkout.store', $asset), [