A free open source IT asset/license management system
FMCS location scoping: Allow checkout to parent
snipe0ce35d845aa9
diff --git a/app/Models/Location.php b/app/Models/Location.phpindex 0835b82afc..09e75baa50 100755--- a/app/Models/Location.php+++ b/app/Models/Location.php@@ -263,6 +263,28 @@ class Location extends SnipeModel->with('parent');}+ /**+ * Walk up the parent chain to find the nearest ancestor with a company_id.+ * Used by FMCS checkout validation so that assets can be checked out to+ * child locations whose company is only set on a parent location.+ */+ public function effectiveFmcsCompanyId(): ?int+ {+ if ($this->company_id) {+ return (int) $this->company_id;+ }++ $ancestor = $this->parent()->withoutGlobalScopes()->first();+ while ($ancestor) {+ if ($ancestor->company_id) {+ return (int) $ancestor->company_id;+ }+ $ancestor = $ancestor->parent()->withoutGlobalScopes()->first();+ }++ return null;+ }+/*** Establishes the locations -> company relationship*diff --git a/app/Models/Traits/CompanyableTrait.php b/app/Models/Traits/CompanyableTrait.phpindex 2c00fc5698..4f72478aec 100644--- a/app/Models/Traits/CompanyableTrait.php+++ b/app/Models/Traits/CompanyableTrait.php@@ -22,6 +22,15 @@ trait CompanyableTraitstatic::addGlobalScope(new CompanyableScope);}+ /**+ * The company ID this model presents for FMCS checkout validation.+ * Location overrides this to walk the parent chain.+ */+ public function effectiveFmcsCompanyId(): ?int+ {+ return $this->company_id ? (int) $this->company_id : null;+ }+/*** Whether this item may be checked out to the given target under FMCS rules.*@@ -29,8 +38,8 @@ 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 has no company and null_company_is_floater is enabled, OR- * - target's company_id exactly matches this item's company_id.+ * - 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.*/public function canCheckoutTo(Model $target): bool{@@ -52,10 +61,14 @@ trait CompanyableTraitreturn $target->canReceiveFromCompany((int) $this->company_id);}- if (is_null($target->company_id)) {+ $targetCompanyId = method_exists($target, 'effectiveFmcsCompanyId')+ ? $target->effectiveFmcsCompanyId()+ : ($target->company_id ? (int) $target->company_id : null);++ if (is_null($targetCompanyId)) {return (bool) $settings->null_company_is_floater;}- return (int) $target->company_id === (int) $this->company_id;+ return $targetCompanyId === (int) $this->company_id;}}diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.phpindex 0d14c4260f..91772cbd95 100644--- a/app/Providers/ValidationServiceProvider.php+++ b/app/Providers/ValidationServiceProvider.php@@ -382,12 +382,14 @@ class ValidationServiceProvider extends ServiceProvider$location = Location::find($value);if ($location) {- if ($location->company_id === null) {- // Null-company location: accessible to companied users only when floater is on.+ $effectiveCompanyId = $location->effectiveFmcsCompanyId();++ if ($effectiveCompanyId === null) {+ // No company found anywhere in the parent chain — floater rule applies.return (bool) $settings->null_company_is_floater;}- if (! in_array($location->company_id, $companyIds)) {+ if (! in_array($effectiveCompanyId, $companyIds)) {return false;}}diff --git a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.phpindex 15c73bfb74..321efa7ed1 100644--- a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php@@ -143,6 +143,47 @@ class AssetCheckoutTest extends TestCaseEvent::assertDispatched(CheckoutableCheckedOut::class);}+ public function test_can_checkout_to_child_location_whose_parent_has_matching_company_when_fmcs_enabled()+ {+ $this->settings->enableMultipleFullCompanySupport();++ $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();++ $this->actingAs(User::factory()->superuser()->create())+ ->post(route('hardware.checkout.store', $asset), [+ 'checkout_to_type' => 'location',+ 'assigned_location' => $childLocation->id,+ 'redirect_option' => 'index',+ ])+ ->assertSessionMissing('error')+ ->assertRedirect();++ Event::assertDispatched(CheckoutableCheckedOut::class);+ }++ public function test_cannot_checkout_to_child_location_whose_parent_has_different_company_when_fmcs_enabled()+ {+ $this->settings->enableMultipleFullCompanySupport();++ $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();++ $this->actingAs(User::factory()->superuser()->create())+ ->post(route('hardware.checkout.store', $asset), [+ 'checkout_to_type' => 'location',+ 'assigned_location' => $childLocation->id,+ ])+ ->assertSessionHas('error');++ Event::assertNotDispatched(CheckoutableCheckedOut::class);+ }+public function test_page_renders(){$this->actingAs(User::factory()->superuser()->create())