Skip to content

A free open source IT asset/license management system

Fixed FD-57818 / qj4v-m8gh - tighter guards on permissions for user transfer

snipe0951f25eddf0
diff --git a/app/Http/Controllers/Users/UserItemTransferController.php b/app/Http/Controllers/Users/UserItemTransferController.php
index aad3d4e941..910f0ae30b 100644
--- a/app/Http/Controllers/Users/UserItemTransferController.php
+++ b/app/Http/Controllers/Users/UserItemTransferController.php
@@ -10,11 +10,13 @@ use App\Models\Accessory;
use App\Models\AccessoryCheckout;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
+use App\Models\License;
use App\Models\LicenseSeat;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Gate;
class UserItemTransferController extends Controller
{
@@ -29,15 +31,23 @@ class UserItemTransferController extends Controller
->whereNull('deleted_at')
->get();
- $accessoryCheckouts = AccessoryCheckout::with(['accessory.category', 'accessory.company'])
- ->where('assigned_to', $user->id)
- ->where('assigned_type', User::class)
- ->get();
-
- $licenseSeats = LicenseSeat::with(['license.category', 'license.company'])
- ->where('assigned_to', $user->id)
- ->whereNull('asset_id')
- ->get();
+ // Gate each section independently. An asset-focused operator without
+ // accessories.view or licenses.view can still legitimately use the
+ // page to transfer hardware, but should not see accessory or license
+ // metadata belonging to the source user.
+ $accessoryCheckouts = Gate::allows('view', Accessory::class)
+ ? AccessoryCheckout::with(['accessory.category', 'accessory.company'])
+ ->where('assigned_to', $user->id)
+ ->where('assigned_type', User::class)
+ ->get()
+ : collect();
+
+ $licenseSeats = Gate::allows('view', License::class)
+ ? LicenseSeat::with(['license.category', 'license.company'])
+ ->where('assigned_to', $user->id)
+ ->whereNull('asset_id')
+ ->get()
+ : collect();
if ($assets->isEmpty() && $accessoryCheckouts->isEmpty() && $licenseSeats->isEmpty()) {
return redirect()->route('users.show', $user)
diff --git a/tests/Feature/Users/TransferUserItemsPageSectionGatingTest.php b/tests/Feature/Users/TransferUserItemsPageSectionGatingTest.php
new file mode 100644
index 0000000000..cbc57da2e4
--- /dev/null
+++ b/tests/Feature/Users/TransferUserItemsPageSectionGatingTest.php
@@ -0,0 +1,136 @@
+<?php
+
+namespace Tests\Feature\Users;
+
+use App\Models\Accessory;
+use App\Models\AccessoryCheckout;
+use App\Models\Asset;
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\User;
+use PHPUnit\Framework\Attributes\Test;
+use Tests\TestCase;
+
+/**
+ * Regression tests for the accessory and license sections of the user-item
+ * transfer page. The GET page used to load AccessoryCheckout and LicenseSeat
+ * unconditionally after only asset-side authorization, so an asset-only
+ * operator could read accessory and license names assigned to the source
+ * user without accessories.view or licenses.view. The read-path gate on
+ * each section is independent so an actor with the asset perms can still
+ * use the page for its primary hardware-transfer purpose.
+ */
+class TransferUserItemsPageSectionGatingTest extends TestCase
+{
+ #[Test]
+ public function asset_only_operator_sees_assets_but_not_accessory_or_license_names(): void
+ {
+ $source = User::factory()->create();
+ Asset::factory()->create(['assigned_to' => $source->id, 'assigned_type' => User::class]);
+
+ $accessory = Accessory::factory()->create(['name' => 'CAND04-ACCESSORY-SECRET']);
+ AccessoryCheckout::factory()->create([
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $source->id,
+ 'assigned_type' => User::class,
+ ]);
+
+ $license = License::factory()->create(['name' => 'CAND04-LICENSE-SECRET', 'reassignable' => 1]);
+ LicenseSeat::factory()
+ ->assignedToUser($source)
+ ->create(['license_id' => $license->id]);
+
+ $actor = User::factory()
+ ->viewUsers()
+ ->checkinAssets()
+ ->checkoutAssets()
+ ->create();
+
+ $response = $this->actingAs($actor)
+ ->get(route('users.transfer.show', $source))
+ ->assertOk()
+ ->assertViewIs('users.transfer');
+
+ $response->assertDontSee('CAND04-ACCESSORY-SECRET');
+ $response->assertDontSee('CAND04-LICENSE-SECRET');
+ }
+
+ #[Test]
+ public function accessory_section_is_visible_when_actor_holds_accessories_view(): void
+ {
+ $source = User::factory()->create();
+
+ $accessory = Accessory::factory()->create(['name' => 'CAND04-ACCESSORY-VISIBLE']);
+ AccessoryCheckout::factory()->create([
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $source->id,
+ 'assigned_type' => User::class,
+ ]);
+
+ $actor = User::factory()
+ ->viewUsers()
+ ->checkinAssets()
+ ->checkoutAssets()
+ ->viewAccessories()
+ ->create();
+
+ $this->actingAs($actor)
+ ->get(route('users.transfer.show', $source))
+ ->assertOk()
+ ->assertSee('CAND04-ACCESSORY-VISIBLE');
+ }
+
+ #[Test]
+ public function license_section_is_visible_when_actor_holds_licenses_view(): void
+ {
+ $source = User::factory()->create();
+
+ $license = License::factory()->create(['name' => 'CAND04-LICENSE-VISIBLE', 'reassignable' => 1]);
+ LicenseSeat::factory()
+ ->assignedToUser($source)
+ ->create(['license_id' => $license->id]);
+
+ $actor = User::factory()
+ ->viewUsers()
+ ->checkinAssets()
+ ->checkoutAssets()
+ ->viewLicenses()
+ ->create();
+
+ $this->actingAs($actor)
+ ->get(route('users.transfer.show', $source))
+ ->assertOk()
+ ->assertSee('CAND04-LICENSE-VISIBLE');
+ }
+
+ #[Test]
+ public function asset_only_operator_is_redirected_when_source_user_has_only_accessories_and_licenses(): void
+ {
+ $source = User::factory()->create();
+
+ $accessory = Accessory::factory()->create();
+ AccessoryCheckout::factory()->create([
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $source->id,
+ 'assigned_type' => User::class,
+ ]);
+
+ $license = License::factory()->create(['reassignable' => 1]);
+ LicenseSeat::factory()
+ ->assignedToUser($source)
+ ->create(['license_id' => $license->id]);
+
+ $actor = User::factory()
+ ->viewUsers()
+ ->checkinAssets()
+ ->checkoutAssets()
+ ->create();
+
+ // With accessory + license sections gated off and no transferable
+ // assets, the page has nothing to offer this actor and the controller
+ // short-circuits back to the user profile.
+ $this->actingAs($actor)
+ ->get(route('users.transfer.show', $source))
+ ->assertRedirect(route('users.show', $source));
+ }
+}