Skip to content

A free open source IT asset/license management system

Item Requests: Fixed FD-56095 - apply FMCS to requestable items listing

snipefbf441f2a218
diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php
index 3a99734b24..de63a2cb16 100755
--- a/app/Http/Controllers/Assets/AssetsController.php
+++ b/app/Http/Controllers/Assets/AssetsController.php
@@ -1120,14 +1120,22 @@ class AssetsController extends Controller
public function getRequestedIndex($user_id = null)
{
$this->authorize('index', Asset::class);
- $requestedItems = CheckoutRequest::with('user', 'requestedItem')->whereNull('canceled_at')->with('user', 'requestedItem');
+
+ $requestedItems = CheckoutRequest::with('user', 'requestedItem')->whereNull('canceled_at');
if ($user_id) {
- $requestedItems->where('user_id', $user_id)->get();
+ $requestedItems->where('user_id', $user_id);
}
$requestedItems = $requestedItems->orderBy('created_at', 'desc')->get();
+ if (Company::isFullMultipleCompanySupportEnabled() && ! auth()->user()->isSuperUser()) {
+ $requestedItems = $requestedItems->filter(
+ fn (CheckoutRequest $request) => $request->requestable
+ && Company::isCurrentUserHasAccess($request->requestable)
+ )->values();
+ }
+
return view('hardware/requested', compact('requestedItems'));
}
}
diff --git a/tests/Feature/Requests/Ui/AssetRequestIndexTest.php b/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
index 474b1c5c26..cc1bcab927 100644
--- a/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
+++ b/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
@@ -2,7 +2,9 @@
namespace Tests\Feature\Requests\Ui;
+use App\Models\Asset;
use App\Models\CheckoutRequest;
+use App\Models\Company;
use App\Models\User;
use Tests\TestCase;
@@ -25,4 +27,114 @@ class AssetRequestIndexTest extends TestCase
->assertViewHas('requestedItems')
->assertSeeText($checkoutRequest->requestedItem->asset_tag);
}
+
+ public function test_user_sees_requests_for_assets_in_their_own_company()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA] = Company::factory()->count(2)->create();
+
+ $assetA = Asset::factory()->create(['company_id' => $companyA->id]);
+ $request = CheckoutRequest::factory()->create(['requestable_id' => $assetA->id, 'requestable_type' => Asset::class]);
+ $viewer = User::factory()->viewAssets()->create(['company_id' => $companyA->id]);
+
+ $this->actingAs($viewer)
+ ->get(route('assets.requested'))
+ ->assertOk()
+ ->assertSeeText($request->requestedItem->asset_tag);
+ }
+
+ public function test_user_does_not_see_requests_for_assets_in_another_company()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
+ $crossCompanyRequest = CheckoutRequest::factory()->create(['requestable_id' => $assetB->id, 'requestable_type' => Asset::class]);
+ $viewer = User::factory()->viewAssets()->create(['company_id' => $companyA->id]);
+
+ $this->actingAs($viewer)
+ ->get(route('assets.requested'))
+ ->assertOk()
+ ->assertViewHas(
+ 'requestedItems',
+ fn ($items) => $items->doesntContain(fn ($r) => $r->id === $crossCompanyRequest->id)
+ );
+ }
+
+ public function test_pivot_only_user_does_not_see_requests_for_assets_in_another_company()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
+ $crossCompanyRequest = CheckoutRequest::factory()->create(['requestable_id' => $assetB->id, 'requestable_type' => Asset::class]);
+ $viewer = User::factory()->viewAssets()->create(['company_id' => null]);
+ $viewer->companies()->sync([$companyA->id]);
+
+ $this->actingAs($viewer)
+ ->get(route('assets.requested'))
+ ->assertOk()
+ ->assertViewHas(
+ 'requestedItems',
+ fn ($items) => $items->doesntContain(fn ($r) => $r->id === $crossCompanyRequest->id)
+ );
+ }
+
+ public function test_floater_mode_makes_null_company_requests_visible_to_company_scoped_users()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->enableFloaterMode();
+
+ [$companyA] = Company::factory()->count(2)->create();
+
+ $floaterAsset = Asset::factory()->create(['company_id' => null]);
+ $request = CheckoutRequest::factory()->create(['requestable_id' => $floaterAsset->id, 'requestable_type' => Asset::class]);
+ $viewer = User::factory()->viewAssets()->create(['company_id' => $companyA->id]);
+
+ $this->actingAs($viewer)
+ ->get(route('assets.requested'))
+ ->assertOk()
+ ->assertViewHas(
+ 'requestedItems',
+ fn ($items) => $items->contains(fn ($r) => $r->id === $request->id)
+ );
+ }
+
+ public function test_superuser_sees_requests_across_all_companies()
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetA = Asset::factory()->create(['company_id' => $companyA->id]);
+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
+ $requestA = CheckoutRequest::factory()->create(['requestable_id' => $assetA->id, 'requestable_type' => Asset::class]);
+ $requestB = CheckoutRequest::factory()->create(['requestable_id' => $assetB->id, 'requestable_type' => Asset::class]);
+ $superuser = User::factory()->superuser()->create(['company_id' => $companyA->id]);
+
+ $this->actingAs($superuser)
+ ->get(route('assets.requested'))
+ ->assertOk()
+ ->assertSeeText($requestA->requestedItem->asset_tag)
+ ->assertSeeText($requestB->requestedItem->asset_tag);
+ }
+
+ public function test_company_scoping_not_enforced_when_fmcs_disabled()
+ {
+ $this->settings->disableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
+ $request = CheckoutRequest::factory()->create(['requestable_id' => $assetB->id, 'requestable_type' => Asset::class]);
+ $viewer = User::factory()->viewAssets()->create(['company_id' => $companyA->id]);
+
+ $this->actingAs($viewer)
+ ->get(route('assets.requested'))
+ ->assertOk()
+ ->assertSeeText($request->requestedItem->asset_tag);
+ }
}