A free open source IT asset/license management system
Tweaks to make codacy happy
snipeaf74e17a1bce
diff --git a/app/Http/Controllers/Users/UserItemTransferController.php b/app/Http/Controllers/Users/UserItemTransferController.phpindex 1bb5c9840f..0231b10b64 100644--- a/app/Http/Controllers/Users/UserItemTransferController.php+++ b/app/Http/Controllers/Users/UserItemTransferController.php@@ -57,103 +57,134 @@ class UserItemTransferController extends Controller{$validated = $request->validated();$target = User::findOrFail($validated['target_user_id']);-- $assetIds = $validated['asset_ids'] ?? [];- $accessoryCheckoutIds = $validated['accessory_checkout_ids'] ?? [];- $licenseSeatIds = $validated['license_seat_ids'] ?? [];$note = $validated['note'];- $result = DB::transaction(function () use ($user, $target, $assetIds, $accessoryCheckoutIds, $licenseSeatIds, $note) {- $assetsTransferred = 0;- $accessoriesTransferred = 0;- $licensesTransferred = 0;- $skipped = [];+ $result = DB::transaction(fn () => [+ 'assets' => $this->transferAssets($validated['asset_ids'] ?? [], $user, $target, $note),+ 'accessories' => $this->transferAccessories($validated['accessory_checkout_ids'] ?? [], $user, $target, $note),+ 'licenses' => $this->transferLicenseSeats($validated['license_seat_ids'] ?? [], $user, $target, $note),+ ]);- foreach ($assetIds as $assetId) {- $asset = Asset::find($assetId);+ $flash = trans('admin/users/general.transfer.success', [+ 'assets' => $result['assets']['count'],+ 'accessories' => $result['accessories']['count'],+ 'licenses' => $result['licenses']['count'],+ 'target' => $target->display_name,+ ]);- if (! $asset || (int) $asset->assigned_to !== (int) $user->id || $asset->assigned_type !== User::class) {- $skipped[] = 'asset:'.$assetId;+ $redirect = redirect()->route('users.show', $target)->with('success', $flash);- continue;- }+ $skipped = array_merge(+ $result['assets']['skipped'],+ $result['accessories']['skipped'],+ $result['licenses']['skipped'],+ );- if (! $asset->canCheckoutTo($target)) {- $skipped[] = 'asset:'.$assetId;+ if (! empty($skipped)) {+ $redirect->with('warning', trans('admin/users/general.transfer.some_skipped', [+ 'count' => count($skipped),+ ]));+ }- continue;- }+ return $redirect;+ }- $this->checkInAsset($asset, $user, $note);- $asset->checkOut($target, auth()->user(), date('Y-m-d H:i:s'), null, $note);- $assetsTransferred++;- }+ /**+ * @return array{count: int, skipped: array<int, string>}+ */+ private function transferAssets(array $ids, User $source, User $target, ?string $note): array+ {+ $count = 0;+ $skipped = [];- foreach ($accessoryCheckoutIds as $checkoutId) {- $checkout = AccessoryCheckout::with('accessory')->find($checkoutId);+ foreach ($ids as $assetId) {+ $asset = Asset::find($assetId);+ if (! $this->assetBelongsToSource($asset, $source) || ! $asset->canCheckoutTo($target)) {+ $skipped[] = 'asset:'.$assetId;- if (! $checkout || (int) $checkout->assigned_to !== (int) $user->id || $checkout->assigned_type !== User::class) {- $skipped[] = 'accessory:'.$checkoutId;+ continue;+ }- continue;- }+ $this->checkInAsset($asset, $source, $note);+ $asset->checkOut($target, auth()->user(), date('Y-m-d H:i:s'), null, $note);+ $count++;+ }- $accessory = $checkout->accessory;+ return ['count' => $count, 'skipped' => $skipped];+ }- if (! $accessory || ! $accessory->canCheckoutTo($target)) {- $skipped[] = 'accessory:'.$checkoutId;+ /**+ * @return array{count: int, skipped: array<int, string>}+ */+ private function transferAccessories(array $ids, User $source, User $target, ?string $note): array+ {+ $count = 0;+ $skipped = [];- continue;- }+ foreach ($ids as $checkoutId) {+ $checkout = AccessoryCheckout::with('accessory')->find($checkoutId);+ $accessory = $checkout?->accessory;+ if (! $this->accessoryCheckoutBelongsToSource($checkout, $source) || ! $accessory?->canCheckoutTo($target)) {+ $skipped[] = 'accessory:'.$checkoutId;- $this->checkInAccessory($checkout, $accessory, $note);- $this->checkOutAccessory($accessory, $target, $note);- $accessoriesTransferred++;+ continue;}- foreach ($licenseSeatIds as $seatId) {- $seat = LicenseSeat::with('license')->find($seatId);-- if (! $seat || (int) $seat->assigned_to !== (int) $user->id || $seat->asset_id !== null) {- $skipped[] = 'license:'.$seatId;-- continue;- }+ $this->checkInAccessory($checkout, $accessory, $note);+ $this->checkOutAccessory($accessory, $target, $note);+ $count++;+ }- $license = $seat->license;+ return ['count' => $count, 'skipped' => $skipped];+ }- // Non-reassignable licenses stay with the original assignee.- // Transferring one would defeat the whole point of the flag,- // so we skip and surface it in the warning bucket.- if (! $license || ! $license->reassignable || ! $license->canCheckoutTo($target)) {- $skipped[] = 'license:'.$seatId;+ /**+ * Non-reassignable licenses stay with the original assignee. Transferring+ * one would defeat the whole point of the flag, so we drop it into the+ * skipped bucket and surface a warning.+ *+ * @return array{count: int, skipped: array<int, string>}+ */+ private function transferLicenseSeats(array $ids, User $source, User $target, ?string $note): array+ {+ $count = 0;+ $skipped = [];- continue;- }+ foreach ($ids as $seatId) {+ $seat = LicenseSeat::with('license')->find($seatId);+ $license = $seat?->license;+ if (! $this->licenseSeatBelongsToSource($seat, $source) || ! $license?->reassignable || ! $license->canCheckoutTo($target)) {+ $skipped[] = 'license:'.$seatId;- $this->transferLicenseSeat($seat, $user, $target, $note);- $licensesTransferred++;+ continue;}- return compact('assetsTransferred', 'accessoriesTransferred', 'licensesTransferred', 'skipped');- });+ $this->transferLicenseSeat($seat, $source, $target, $note);+ $count++;+ }- $flash = trans('admin/users/general.transfer.success', [- 'assets' => $result['assetsTransferred'],- 'accessories' => $result['accessoriesTransferred'],- 'licenses' => $result['licensesTransferred'],- 'target' => $target->display_name,- ]);+ return ['count' => $count, 'skipped' => $skipped];+ }- $redirect = redirect()->route('users.show', $target)->with('success', $flash);+ private function assetBelongsToSource(?Asset $asset, User $source): bool+ {+ return $asset !== null+ && (int) $asset->assigned_to === (int) $source->id+ && $asset->assigned_type === User::class;+ }- if (! empty($result['skipped'])) {- $redirect->with('warning', trans('admin/users/general.transfer.some_skipped', [- 'count' => count($result['skipped']),- ]));- }+ private function accessoryCheckoutBelongsToSource(?AccessoryCheckout $checkout, User $source): bool+ {+ return $checkout !== null+ && (int) $checkout->assigned_to === (int) $source->id+ && $checkout->assigned_type === User::class;+ }- return $redirect;+ private function licenseSeatBelongsToSource(?LicenseSeat $seat, User $source): bool+ {+ return $seat !== null+ && (int) $seat->assigned_to === (int) $source->id+ && $seat->asset_id === null;}private function checkInAsset(Asset $asset, User $source, ?string $note): voiddiff --git a/tests/Feature/Users/TransferUserItemsTest.php b/tests/Feature/Users/TransferUserItemsBehaviorTest.phpsimilarity index 54%rename from tests/Feature/Users/TransferUserItemsTest.phprename to tests/Feature/Users/TransferUserItemsBehaviorTest.phpindex 81ea2bef7a..6e7655ada1 100644--- a/tests/Feature/Users/TransferUserItemsTest.php+++ b/tests/Feature/Users/TransferUserItemsBehaviorTest.php@@ -10,46 +10,8 @@ use App\Models\LicenseSeat;use App\Models\User;use Tests\TestCase;-class TransferUserItemsTest extends TestCase+class TransferUserItemsBehaviorTest extends TestCase{- public function test_transfer_page_requires_authentication(): void- {- User::factory()->create();- $source = User::factory()->create();-- $this->get(route('users.transfer.show', $source))- ->assertRedirect(route('login'));- }-- public function test_transfer_page_requires_checkout_permission(): void- {- $source = User::factory()->create();-- $this->actingAs(User::factory()->viewUsers()->create())- ->get(route('users.transfer.show', $source))- ->assertForbidden();- }-- public function test_transfer_page_renders_when_source_has_items(): void- {- $source = User::factory()->create();- Asset::factory()->create(['assigned_to' => $source->id, 'assigned_type' => User::class]);-- $this->actingAs($this->transferActor())- ->get(route('users.transfer.show', $source))- ->assertOk()- ->assertViewIs('users.transfer');- }-- public function test_transfer_page_redirects_when_source_has_no_items(): void- {- $source = User::factory()->create();-- $this->actingAs($this->transferActor())- ->get(route('users.transfer.show', $source))- ->assertRedirect(route('users.show', $source));- }-public function test_transfer_moves_asset_from_source_to_target(): void{$source = User::factory()->create();@@ -186,98 +148,6 @@ class TransferUserItemsTest extends TestCase$this->assertSame($source->id, $unselectedAsset->assigned_to);}- public function test_transfer_rejects_same_source_and_target(): void- {- $source = User::factory()->create();- $asset = Asset::factory()->create([- 'assigned_to' => $source->id,- 'assigned_type' => User::class,- ]);-- $this->actingAs($this->transferActor())- ->from(route('users.transfer.show', $source))- ->post(route('users.transfer.store', $source), [- 'target_user_id' => $source->id,- 'asset_ids' => [$asset->id],- 'note' => 'accidental self-target',- ])- ->assertRedirect(route('users.transfer.show', $source))- ->assertSessionHasErrors('target_user_id');-- $asset->refresh();- $this->assertSame($source->id, $asset->assigned_to);- }-- public function test_transfer_rejects_empty_selection(): void- {- $source = User::factory()->create();- $target = User::factory()->create();-- $this->actingAs($this->transferActor())- ->from(route('users.transfer.show', $source))- ->post(route('users.transfer.store', $source), [- 'target_user_id' => $target->id,- 'note' => 'nothing selected',- ])- ->assertRedirect(route('users.transfer.show', $source))- ->assertSessionHasErrors('asset_ids');- }-- public function test_transfer_rejects_empty_note(): void- {- $source = User::factory()->create();- $target = User::factory()->create();- $asset = Asset::factory()->create([- 'assigned_to' => $source->id,- 'assigned_type' => User::class,- ]);-- $this->actingAs($this->transferActor())- ->from(route('users.transfer.show', $source))- ->post(route('users.transfer.store', $source), [- 'target_user_id' => $target->id,- 'asset_ids' => [$asset->id],- // note deliberately omitted- ])- ->assertRedirect(route('users.transfer.show', $source))- ->assertSessionHasErrors('note');-- $asset->refresh();- $this->assertSame($source->id, $asset->assigned_to);- }-- public function test_transfer_ignores_asset_not_actually_assigned_to_source(): void- {- // Someone tampering with the form to pass an asset ID that- // doesn't belong to the source user should not be able to- // hijack the transfer flow to reassign arbitrary assets.- $source = User::factory()->create();- $target = User::factory()->create();- $otherOwner = User::factory()->create();-- $sourceAsset = Asset::factory()->create([- 'assigned_to' => $source->id,- 'assigned_type' => User::class,- ]);- $foreignAsset = Asset::factory()->create([- 'assigned_to' => $otherOwner->id,- 'assigned_type' => User::class,- ]);-- $this->actingAs($this->transferActor())- ->post(route('users.transfer.store', $source), [- 'target_user_id' => $target->id,- 'asset_ids' => [$sourceAsset->id, $foreignAsset->id],- 'note' => 'attempting cross-user transfer',- ]);-- $sourceAsset->refresh();- $foreignAsset->refresh();-- $this->assertSame($target->id, $sourceAsset->assigned_to);- $this->assertSame($otherOwner->id, $foreignAsset->assigned_to);- }-private function transferActor(): User{return User::factory()diff --git a/tests/Feature/Users/TransferUserItemsPageAccessTest.php b/tests/Feature/Users/TransferUserItemsPageAccessTest.phpnew file mode 100644index 0000000000..a3fbf5985b--- /dev/null+++ b/tests/Feature/Users/TransferUserItemsPageAccessTest.php@@ -0,0 +1,57 @@+<?php++namespace Tests\Feature\Users;++use App\Models\Asset;+use App\Models\User;+use Tests\TestCase;++class TransferUserItemsPageAccessTest extends TestCase+{+ public function test_transfer_page_requires_authentication(): void+ {+ User::factory()->create();+ $source = User::factory()->create();++ $this->get(route('users.transfer.show', $source))+ ->assertRedirect(route('login'));+ }++ public function test_transfer_page_requires_checkout_permission(): void+ {+ $source = User::factory()->create();++ $this->actingAs(User::factory()->viewUsers()->create())+ ->get(route('users.transfer.show', $source))+ ->assertForbidden();+ }++ public function test_transfer_page_renders_when_source_has_items(): void+ {+ $source = User::factory()->create();+ Asset::factory()->create(['assigned_to' => $source->id, 'assigned_type' => User::class]);++ $this->actingAs($this->transferActor())+ ->get(route('users.transfer.show', $source))+ ->assertOk()+ ->assertViewIs('users.transfer');+ }++ public function test_transfer_page_redirects_when_source_has_no_items(): void+ {+ $source = User::factory()->create();++ $this->actingAs($this->transferActor())+ ->get(route('users.transfer.show', $source))+ ->assertRedirect(route('users.show', $source));+ }++ private function transferActor(): User+ {+ return User::factory()+ ->viewUsers()+ ->checkinAssets()+ ->checkoutAssets()+ ->create();+ }+}diff --git a/tests/Feature/Users/TransferUserItemsValidationTest.php b/tests/Feature/Users/TransferUserItemsValidationTest.phpnew file mode 100644index 0000000000..a862bcd88d--- /dev/null+++ b/tests/Feature/Users/TransferUserItemsValidationTest.php@@ -0,0 +1,111 @@+<?php++namespace Tests\Feature\Users;++use App\Models\Asset;+use App\Models\User;+use Tests\TestCase;++class TransferUserItemsValidationTest extends TestCase+{+ public function test_transfer_rejects_same_source_and_target(): void+ {+ $source = User::factory()->create();+ $asset = Asset::factory()->create([+ 'assigned_to' => $source->id,+ 'assigned_type' => User::class,+ ]);++ $this->actingAs($this->transferActor())+ ->from(route('users.transfer.show', $source))+ ->post(route('users.transfer.store', $source), [+ 'target_user_id' => $source->id,+ 'asset_ids' => [$asset->id],+ 'note' => 'accidental self-target',+ ])+ ->assertRedirect(route('users.transfer.show', $source))+ ->assertSessionHasErrors('target_user_id');++ $asset->refresh();+ $this->assertSame($source->id, $asset->assigned_to);+ }++ public function test_transfer_rejects_empty_selection(): void+ {+ $source = User::factory()->create();+ $target = User::factory()->create();++ $this->actingAs($this->transferActor())+ ->from(route('users.transfer.show', $source))+ ->post(route('users.transfer.store', $source), [+ 'target_user_id' => $target->id,+ 'note' => 'nothing selected',+ ])+ ->assertRedirect(route('users.transfer.show', $source))+ ->assertSessionHasErrors('asset_ids');+ }++ public function test_transfer_rejects_empty_note(): void+ {+ $source = User::factory()->create();+ $target = User::factory()->create();+ $asset = Asset::factory()->create([+ 'assigned_to' => $source->id,+ 'assigned_type' => User::class,+ ]);++ $this->actingAs($this->transferActor())+ ->from(route('users.transfer.show', $source))+ ->post(route('users.transfer.store', $source), [+ 'target_user_id' => $target->id,+ 'asset_ids' => [$asset->id],+ // note deliberately omitted+ ])+ ->assertRedirect(route('users.transfer.show', $source))+ ->assertSessionHasErrors('note');++ $asset->refresh();+ $this->assertSame($source->id, $asset->assigned_to);+ }++ public function test_transfer_ignores_asset_not_actually_assigned_to_source(): void+ {+ // Someone tampering with the form to pass an asset ID that+ // doesn't belong to the source user should not be able to+ // hijack the transfer flow to reassign arbitrary assets.+ $source = User::factory()->create();+ $target = User::factory()->create();+ $otherOwner = User::factory()->create();++ $sourceAsset = Asset::factory()->create([+ 'assigned_to' => $source->id,+ 'assigned_type' => User::class,+ ]);+ $foreignAsset = Asset::factory()->create([+ 'assigned_to' => $otherOwner->id,+ 'assigned_type' => User::class,+ ]);++ $this->actingAs($this->transferActor())+ ->post(route('users.transfer.store', $source), [+ 'target_user_id' => $target->id,+ 'asset_ids' => [$sourceAsset->id, $foreignAsset->id],+ 'note' => 'attempting cross-user transfer',+ ]);++ $sourceAsset->refresh();+ $foreignAsset->refresh();++ $this->assertSame($target->id, $sourceAsset->assigned_to);+ $this->assertSame($otherOwner->id, $foreignAsset->assigned_to);+ }++ private function transferActor(): User+ {+ return User::factory()+ ->viewUsers()+ ->checkinAssets()+ ->checkoutAssets()+ ->create();+ }+}