Skip to content

Commit c8bb221

Browse files
committed
FMCS: Location scoping - prevent scoping by location if not enabled
1 parent 92c0411 commit c8bb221

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

app/Models/Traits/CompanyableTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Models\Traits;
44

55
use App\Models\CompanyableScope;
6+
use App\Models\Location;
67
use App\Models\Setting;
78
use App\Models\User;
89
use Illuminate\Database\Eloquent\Model;
@@ -38,6 +39,7 @@ public function effectiveFmcsCompanyId(): ?int
3839
* - FMCS is disabled, OR
3940
* - this item has no company (uncompanied items are unrestricted), OR
4041
* - target is a User whose company pivot includes this item's company, OR
42+
* - target is a Location and scope_locations_fmcs is disabled, OR
4143
* - target has no effective company and null_company_is_floater is enabled, OR
4244
* - target's effective company_id exactly matches this item's company_id.
4345
*/
@@ -61,6 +63,10 @@ public function canCheckoutTo(Model $target): bool
6163
return $target->canReceiveFromCompany((int) $this->company_id);
6264
}
6365

66+
if ($target instanceof Location && ! $settings->scope_locations_fmcs) {
67+
return true;
68+
}
69+
6470
$targetCompanyId = method_exists($target, 'effectiveFmcsCompanyId')
6571
? $target->effectiveFmcsCompanyId()
6672
: ($target->company_id ? (int) $target->company_id : null);

tests/Feature/Checkouts/Ui/AssetCheckoutTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,34 @@ public function test_can_checkout_to_user_with_multiple_companies_via_pivot_when
143143
Event::assertDispatched(CheckoutableCheckedOut::class);
144144
}
145145

146-
public function test_can_checkout_to_child_location_whose_parent_has_matching_company_when_fmcs_enabled()
146+
public function test_can_checkout_to_uncompanied_location_when_fmcs_enabled_without_location_scoping()
147147
{
148148
$this->settings->enableMultipleFullCompanySupport();
149149

150+
$company = Company::factory()->create();
151+
$location = Location::factory()->create(['company_id' => null]);
152+
$asset = Asset::factory()->for($company)->create();
153+
154+
$this->actingAs(User::factory()->superuser()->create())
155+
->post(route('hardware.checkout.store', $asset), [
156+
'checkout_to_type' => 'location',
157+
'assigned_location' => $location->id,
158+
'redirect_option' => 'index',
159+
])
160+
->assertSessionMissing('error')
161+
->assertRedirect();
162+
163+
Event::assertDispatched(CheckoutableCheckedOut::class);
164+
}
165+
166+
public function test_can_checkout_to_child_location_whose_parent_has_matching_company_when_location_scoping_enabled()
167+
{
168+
$this->settings->enableScopedLocationsWithFullMultipleCompanySupport();
169+
150170
$company = Company::factory()->create();
151171
$parentLocation = Location::factory()->for($company)->create();
152172
$childLocation = Location::factory()->create(['parent_id' => $parentLocation->id, 'company_id' => null]);
153-
$asset = Asset::factory()->for($company)->create();
173+
$asset = Asset::factory()->for($company)->create(['rtd_location_id' => $parentLocation->id]);
154174

155175
$this->actingAs(User::factory()->superuser()->create())
156176
->post(route('hardware.checkout.store', $asset), [
@@ -164,15 +184,15 @@ public function test_can_checkout_to_child_location_whose_parent_has_matching_co
164184
Event::assertDispatched(CheckoutableCheckedOut::class);
165185
}
166186

167-
public function test_cannot_checkout_to_child_location_whose_parent_has_different_company_when_fmcs_enabled()
187+
public function test_cannot_checkout_to_child_location_whose_parent_has_different_company_when_location_scoping_enabled()
168188
{
169-
$this->settings->enableMultipleFullCompanySupport();
189+
$this->settings->enableScopedLocationsWithFullMultipleCompanySupport();
170190

171191
$assetCompany = Company::factory()->create();
172192
$otherCompany = Company::factory()->create();
173193
$parentLocation = Location::factory()->for($otherCompany)->create();
174194
$childLocation = Location::factory()->create(['parent_id' => $parentLocation->id, 'company_id' => null]);
175-
$asset = Asset::factory()->for($assetCompany)->create();
195+
$asset = Asset::factory()->for($assetCompany)->create(['rtd_location_id' => null]);
176196

177197
$this->actingAs(User::factory()->superuser()->create())
178198
->post(route('hardware.checkout.store', $asset), [

0 commit comments

Comments
 (0)