Skip to content

A free open source IT asset/license management system

Merge branch 'develop' into fixes/17404-prevent-bulk-checkout-across-companies

Marcus Moorefb9fb9c0978c
diff --cc app/Http/Controllers/Assets/BulkAssetsController.php
index 463c7d2dbd,4b9ccb6753..cb4fa5fa5b
--- a/app/Http/Controllers/Assets/BulkAssetsController.php
+++ b/app/Http/Controllers/Assets/BulkAssetsController.php
@@@ -647,20 -647,15 +647,30 @@@ class BulkAssetsController extends Cont
$assets = Asset::findOrFail($asset_ids);
+ // Prevent checking out assets that are already checked out
+ if ($assets->pluck('assigned_to')->unique()->filter()->isNotEmpty()) {
+ // re-add the asset ids so the assets select is re-populated
+ $request->session()->flashInput(['selected_assets' => $asset_ids]);
+
+ return redirect(route('hardware.bulkcheckout.show'))
+ ->with('error', trans('general.error_assets_already_checked_out'));
+ }
+
++ // Prevent checking out assets across companies if FMCS enabled
+ if (Setting::getSettings()->full_multiple_companies_support && $target->company_id) {
+ $company_ids = $assets->pluck('company_id')->unique();
+
+ // if there is more than one unique company id or the singular company id does not match
+ // then the checkout is invalid
+ if ($company_ids->count() > 1 || $company_ids->first() != $target->company_id) {
+ // re-add the asset ids so the assets select is re-populated
+ $request->session()->flashInput(['selected_assets' => $asset_ids]);
+
+ return redirect(route('hardware.bulkcheckout.show'))
+ ->with('error', trans('general.error_user_company_multiple'));
+ }
+ }
+
if (request('checkout_to_type') == 'asset') {
foreach ($asset_ids as $asset_id) {
if ($target->id == $asset_id) {
diff --cc resources/lang/en-US/general.php
index 5941682bfc,42751c5cf9..3d7b8ca6bf
--- a/resources/lang/en-US/general.php
+++ b/resources/lang/en-US/general.php
@@@ -519,8 -519,8 +519,9 @@@ return
'item_notes' => ':item Notes',
'item_name_var' => ':item Name',
'error_user_company' => 'Checkout target company and asset company do not match',
+ 'error_user_company_multiple' => 'One or more of the checkout target company and asset company do not match',
'error_user_company_accept_view' => 'An Asset assigned to you belongs to a different company so you can\'t accept nor deny it, please check with your manager',
+ 'error_assets_already_checked_out' => 'One or more of the assets are already checked out',
'importer' => [
'checked_out_to_fullname' => 'Checked Out to: Full Name',
'checked_out_to_first_name' => 'Checked Out to: First Name',
diff --cc tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php
index 8750087394,2f325ba68a..6f6f250b90
--- a/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php
+++ b/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php
@@@ -124,38 -123,31 +124,67 @@@ class BulkAssetCheckoutTest extends Tes
}
#[DataProvider('checkoutTargets')]
+ public function test_adheres_to_full_multiple_company_support($data)
+ {
+ ['type' => $type, 'target' => $target] = $data();
+
+ $this->settings->enableMultipleFullCompanySupport();
+
+ // create two companies
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ // create an asset for each company
+ $assetForCompanyA = Asset::factory()->for($companyA)->create();
+ $assetForCompanyB = Asset::factory()->for($companyB)->create();
+
+ $this->assertNull($assetForCompanyA->assigned_to, 'Asset should not be assigned before attempting this test case.');
+ $this->assertNull($assetForCompanyB->assigned_to, 'Asset should not be assigned before attempting this test case.');
+
+ // attempt to bulk checkout both items to the target
+ $response = $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('hardware.bulkcheckout.store'), [
+ 'selected_assets' => [
+ $assetForCompanyA->id,
+ $assetForCompanyB->id,
+ ],
+ 'checkout_to_type' => $type,
+ "assigned_$type" => $target->id,
+ ]);
+
+ // ensure bulk checkout is blocked
+ $this->assertNull($assetForCompanyA->fresh()->assigned_to, 'Asset was checked out across companies.');
+ $this->assertNull($assetForCompanyB->fresh()->assigned_to, 'Asset was checked out across companies.');
+
+ // ensure redirected back
+ $response->assertRedirectToRoute('hardware.bulkcheckout.show');
+ }
++
++ #[DataProvider('checkoutTargets')]
+ public function test_prevents_checkouts_of_checked_out_items($data)
+ {
+ ['type' => $type, 'target' => $target] = $data();
+
+ $asset = Asset::factory()->create();
+ $checkedOutAsset = Asset::factory()->assignedToUser()->create();
+ $existingUserId = $checkedOutAsset->assigned_to;
+
+ $response = $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('hardware.bulkcheckout.store'), [
+ 'selected_assets' => [
+ $asset->id,
+ $checkedOutAsset->id,
+ ],
+ 'checkout_to_type' => $type,
+ "assigned_$type" => $target->id,
+ ]);
+
+ $this->assertEquals(
+ $existingUserId,
+ $checkedOutAsset->fresh()->assigned_to,
+ 'Asset was checked out when it should have been prevented.'
+ );
+
+ // ensure redirected back
+ $response->assertRedirectToRoute('hardware.bulkcheckout.show');
+ }
}