Skip to content

A free open source IT asset/license management system

Misc gate tightening + tests

snipe0179c5191912
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index b30d691e31..8df8ea42e5 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -778,6 +778,20 @@ class AssetsController extends Controller
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')));
}
+ // The create payload may include assigned_user / assigned_asset /
+ // assigned_location and normally triggers a real checkOut() alongside
+ // the create. A role with only assets.create (and an explicit deny on
+ // assets.checkout) would otherwise land a checkout event on the
+ // newly-fabricated asset, bypassing the checkout permission entirely.
+ // Rather than reject the whole request, drop the checkout side of the
+ // operation and keep the create. The response message flags the skipped checkout so they
+ // can detect the partial success.
+ $checkoutSkippedForPermission = $requestedCheckout && ! Gate::allows('checkout', $asset);
+ if ($checkoutSkippedForPermission) {
+ $requestedCheckout = false;
+ $target = null;
+ }
+
if ($requestedCheckout) {
$companyMismatchResponse = $this->checkoutCompanyMismatchResponse($asset, $target);
if ($companyMismatchResponse) {
@@ -804,7 +818,11 @@ class AssetsController extends Controller
$asset->image = $asset->getImageUrl();
}
- return response()->json(Helper::formatStandardApiResponse('success', $asset, trans('admin/hardware/message.create.success')));
+ $message = $checkoutSkippedForPermission
+ ? trans('admin/hardware/message.create.success_no_checkout')
+ : trans('admin/hardware/message.create.success');
+
+ return response()->json(Helper::formatStandardApiResponse('success', $asset, $message));
// below is what we want the _eventual_ return to look like - in a more standardized format.
// return response()->json(Helper::formatStandardApiResponse('success', (new AssetsTransformer)->transformAsset($asset), trans('admin/hardware/message.create.success')));
diff --git a/app/Http/Controllers/Api/LicenseSeatsController.php b/app/Http/Controllers/Api/LicenseSeatsController.php
index 1d1c7bc06d..eff1c8bb6d 100644
--- a/app/Http/Controllers/Api/LicenseSeatsController.php
+++ b/app/Http/Controllers/Api/LicenseSeatsController.php
@@ -14,6 +14,7 @@ use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
class LicenseSeatsController extends Controller
@@ -217,6 +218,19 @@ class LicenseSeatsController extends Controller
// Are the assignment fields cleared? If yes, this is a checkin operation.
$is_checkin = ($assignmentTouched && $licenseSeat->assigned_to === null && $licenseSeat->asset_id === null);
+ // The method-level authorize() above requires licenses.checkout
+ // because most calls to this endpoint reassign a seat. When the
+ // update instead clears the assignment (a checkin), require the
+ // dedicated licenses.checkin permission too. Gate on the
+ // specific license so FMCS company scoping runs too (a caller
+ // in a different company shouldn't be able to free a seat on
+ // this one even with the permission bit).
+ if ($is_checkin && ! Gate::allows('checkin', $license)) {
+ $errorResponse = response()->json(Helper::formatStandardApiResponse('error', null, trans('general.unauthorized')), 403);
+
+ return;
+ }
+
// The logging functions expect only one "target"; assets take precedence over users.
$target = null;
if ($licenseSeat->isDirty('assigned_to')) {
diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php
index 5e9b0aea69..42c4a015b9 100755
--- a/app/Http/Controllers/Assets/AssetsController.php
+++ b/app/Http/Controllers/Assets/AssetsController.php
@@ -107,6 +107,34 @@ class AssetsController extends Controller
{
$this->authorize(Asset::class);
+ // The create form accepts assigned_user / assigned_asset /
+ // assigned_location and normally triggers a real checkOut() on every
+ // newly-created asset in the multi-create loop. A role with only
+ // assets.create (and an explicit deny on assets.checkout) would
+ // otherwise land checkout events on the fabricated assets,
+ // bypassing the checkout permission entirely. Rather than reject
+ // the whole request, drop the checkout side and keep the create.
+ // The trailing flash notes the skipped checkout so the actor can
+ // see the partial success without hunting through the audit trail.
+ $requestedCheckout = $request->filled('assigned_user')
+ || $request->filled('assigned_asset')
+ || $request->filled('assigned_location');
+
+ $checkoutSkippedForPermission = $requestedCheckout && ! Gate::allows('checkout', Asset::class);
+ if ($checkoutSkippedForPermission) {
+ // Merge on both the injected FormRequest and the container's
+ // active Request instance so downstream `request()` helper
+ // reads inside the multi-create loop see the cleared values.
+ $clearAssignment = [
+ 'assigned_user' => null,
+ 'assigned_asset' => null,
+ 'assigned_location' => null,
+ 'assigned_to' => null,
+ ];
+ $request->merge($clearAssignment);
+ request()->merge($clearAssignment);
+ }
+
// There are a lot more rules to add here but prevents
// errors around `asset_tags` not being present below.
$this->validate($request, ['asset_tags' => ['required', 'array']]);
@@ -282,22 +310,27 @@ class AssetsController extends Controller
if ($successes) {
if ($failures) {
// some succeeded, some failed
- return Helper::getRedirectOption($request, $asset->id, 'Assets') // FIXME - not tested
+ $response = Helper::getRedirectOption($request, $asset->id, 'Assets') // FIXME - not tested
->with('success-unescaped', trans_choice('admin/hardware/message.create.multi_success_linked', $successes, ['links' => implode(', ', $successes)]))
->with('warning', trans_choice('admin/hardware/message.create.partial_failure', $failures, ['failures' => implode('; ', $failures)]));
} else {
if (count($successes) == 1) {
// the most common case, keeping it so we don't have to make every use of that translation string be trans_choice'ed
// and re-translated
- return Helper::getRedirectOption($request, $asset->id, 'Assets')
+ $response = Helper::getRedirectOption($request, $asset->id, 'Assets')
->with('success-unescaped', trans('admin/hardware/message.create.success_linked', ['link' => route('hardware.show', $asset), 'id', 'tag' => e($asset->asset_tag)]));
} else {
// multi-success
- return Helper::getRedirectOption($request, $asset->id, 'Assets')
+ $response = Helper::getRedirectOption($request, $asset->id, 'Assets')
->with('success-unescaped', trans_choice('admin/hardware/message.create.multi_success_linked', $successes, ['links' => implode(', ', $successes)]));
}
}
+ if ($checkoutSkippedForPermission) {
+ $response->with('warning', trans('admin/hardware/message.create.checkout_skipped_no_permission'));
+ }
+
+ return $response;
}
return redirect()->back()->withInput()->withErrors($asset->getErrors());
diff --git a/app/Http/Requests/TransferUserItemsRequest.php b/app/Http/Requests/TransferUserItemsRequest.php
index ae3f1d4573..8ccbae7090 100644
--- a/app/Http/Requests/TransferUserItemsRequest.php
+++ b/app/Http/Requests/TransferUserItemsRequest.php
@@ -2,7 +2,9 @@
namespace App\Http\Requests;
+use App\Models\Accessory;
use App\Models\Asset;
+use App\Models\License;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
@@ -13,10 +15,34 @@ class TransferUserItemsRequest extends Request
{
$sourceUser = $this->route('user');
- return $sourceUser
- && Gate::allows('view', $sourceUser)
- && Gate::allows('checkin', Asset::class)
- && Gate::allows('checkout', Asset::class);
+ if (! $sourceUser || ! Gate::allows('view', $sourceUser)) {
+ return false;
+ }
+
+ if (! Gate::allows('checkin', Asset::class) || ! Gate::allows('checkout', Asset::class)) {
+ return false;
+ }
+
+ // The store handler transfers accessory checkouts and license
+ // seats alongside assets. Each resource is governed by its own
+ // independent checkin / checkout permissions, so require them
+ // when the payload actually supplies items of that type. Without
+ // these guards, a role holding only assets.checkin / assets.checkout
+ // could move another user's accessories and license seats,
+ // bypassing the per-resource permission model.
+ if (! empty($this->input('accessory_checkout_ids'))
+ && (! Gate::allows('checkin', Accessory::class) || ! Gate::allows('checkout', Accessory::class))
+ ) {
+ return false;
+ }
+
+ if (! empty($this->input('license_seat_ids'))
+ && (! Gate::allows('checkin', License::class) || ! Gate::allows('checkout', License::class))
+ ) {
+ return false;
+ }
+
+ return true;
}
public function rules(): array
diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php
index 08ba9b71dc..c763a571d0 100644
--- a/app/Http/Transformers/ActionlogsTransformer.php
+++ b/app/Http/Transformers/ActionlogsTransformer.php
@@ -10,6 +10,7 @@ use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company;
use App\Models\CustomField;
+use App\Models\License;
use App\Models\Location;
use App\Models\Setting;
use App\Models\Statuslabel;
@@ -158,6 +159,39 @@ class ActionlogsTransformer
}
$clean_meta = $this->changedInfo($clean_meta);
+
+ // A license serial is the product key. When the current
+ // user does not hold licenses.keys / create / edit, the
+ // resource / index / export sinks all mask the raw key.
+ // The old / new pair in log_meta['serial'] on a license
+ // edit would otherwise leak both the previous and new
+ // key through the History tab and the activity report.
+ if ($actionlog->item instanceof License
+ && ! Gate::allows('viewKeys', $actionlog->item)
+ && isset($clean_meta['serial'])
+ && is_array($clean_meta['serial'])
+ ) {
+ if (isset($clean_meta['serial']['old'])) {
+ $clean_meta['serial']['old'] = License::PRODUCT_KEY_MASK;
+ }
+ if (isset($clean_meta['serial']['new'])) {
+ $clean_meta['serial']['new'] = License::PRODUCT_KEY_MASK;
+ }
+ }
+ }
+
+ // A license serial is the product key. When the current user
+ // does not hold licenses.keys / create / edit, mask it here the
+ // same way LicensesTransformer masks product_key. Without this
+ // the /api/v1/licenses/{id}/history and /api/v1/reports/activity
+ // sinks leak the raw key through actionlog rows.
+ $itemSerial = null;
+ if ($actionlog->item && $actionlog->item->serial) {
+ if ($actionlog->item instanceof License && ! Gate::allows('viewKeys', $actionlog->item)) {
+ $itemSerial = License::PRODUCT_KEY_MASK;
+ } else {
+ $itemSerial = e($actionlog->item->serial);
+ }
}
$array = [
@@ -177,7 +211,7 @@ class ActionlogsTransformer
'id' => (int) $actionlog->item->id,
'name' => e($actionlog->item->display_name) ?? null,
'type' => e($actionlog->itemType()),
- 'serial' => e($actionlog->item->serial) ? e($actionlog->item->serial) : null,
+ 'serial' => $itemSerial,
] : null,
'location' => ($actionlog->location) ? [
'id' => (int) $actionlog->location->id,
diff --git a/resources/lang/en-US/admin/hardware/message.php b/resources/lang/en-US/admin/hardware/message.php
index 8924cae372..7518b484cb 100644
--- a/resources/lang/en-US/admin/hardware/message.php
+++ b/resources/lang/en-US/admin/hardware/message.php
@@ -16,6 +16,8 @@ return [
'create' => [
'error' => 'Asset was not created, please try again. :(',
'success' => 'Asset created successfully. :)',
+ 'success_no_checkout' => 'Asset created successfully, but was not checked out because you do not have permission to check assets out.',
+ 'checkout_skipped_no_permission' => 'The asset was created, but was not checked out to the requested target because you do not have permission to check assets out.',
'success_linked' => 'Asset with tag :tag was created successfully. <strong><a href=":link" style="color: white;">Click here to view</a></strong>.',
'multi_success_linked' => 'Asset with tag :links was created successfully.|:count assets were created succesfully. :links.',
'partial_failure' => 'An asset was unable to be created. Reason: :failures|:count assets were unable to be created. Reasons: :failures',
diff --git a/tests/Feature/Assets/Api/StoreAssetCheckoutPermissionTest.php b/tests/Feature/Assets/Api/StoreAssetCheckoutPermissionTest.php
new file mode 100644
index 0000000000..7120260555
--- /dev/null
+++ b/tests/Feature/Assets/Api/StoreAssetCheckoutPermissionTest.php
@@ -0,0 +1,135 @@
+<?php
+
+namespace Tests\Feature\Assets\Api;
+
+use App\Models\Asset;
+use App\Models\AssetModel;
+use App\Models\Location;
+use App\Models\Statuslabel;
+use App\Models\User;
+use Tests\TestCase;
+
+class StoreAssetCheckoutPermissionTest extends TestCase
+{
+ public function test_create_with_assigned_user_without_checkout_permission_creates_asset_but_skips_checkout(): void
+ {
+ $creator = User::factory()->createAssets()->create();
+ $victim = User::factory()->create();
+ $model = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->readyToDeploy()->create();
+
+ $tag = 'CHECKOUT-GATE-U-'.uniqid();
+ $response = $this->actingAsForApi($creator)
+ ->postJson(route('api.assets.store'), [
+ 'model_id' => $model->id,
+ 'status_id' => $status->id,
+ 'asset_tag' => $tag,
+ 'assigned_user' => $victim->id,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success')
+ ->assertMessagesAre(trans('admin/hardware/message.create.success_no_checkout'));
+
+ // Asset persists.
+ $this->assertDatabaseHas('assets', [
+ 'asset_tag' => $tag,
+ 'assigned_to' => null,
+ ]);
+
+ // No checkout event on the victim.
+ $this->assertDatabaseMissing('action_logs', [
+ 'target_id' => $victim->id,
+ 'target_type' => User::class,
+ 'action_type' => 'checkout',
+ ]);
+ }
+
+ public function test_create_with_assigned_asset_without_checkout_permission_creates_asset_but_skips_checkout(): void
+ {
+ $creator = User::factory()->createAssets()->create();
+ $parentAsset = Asset::factory()->create();
+ $model = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->readyToDeploy()->create();
+
+ $tag = 'CHECKOUT-GATE-A-'.uniqid();
+ $this->actingAsForApi($creator)
+ ->postJson(route('api.assets.store'), [
+ 'model_id' => $model->id,
+ 'status_id' => $status->id,
+ 'asset_tag' => $tag,
+ 'assigned_asset' => $parentAsset->id,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $this->assertDatabaseHas('assets', [
+ 'asset_tag' => $tag,
+ 'assigned_to' => null,
+ ]);
+ }
+
+ public function test_create_with_assigned_location_without_checkout_permission_creates_asset_but_skips_checkout(): void
+ {
+ $creator = User::factory()->createAssets()->create();
+ $location = Location::factory()->create();
+ $model = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->readyToDeploy()->create();
+
+ $tag = 'CHECKOUT-GATE-L-'.uniqid();
+ $this->actingAsForApi($creator)
+ ->postJson(route('api.assets.store'), [
+ 'model_id' => $model->id,
+ 'status_id' => $status->id,
+ 'asset_tag' => $tag,
+ 'assigned_location' => $location->id,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $this->assertDatabaseHas('assets', [
+ 'asset_tag' => $tag,
+ 'assigned_to' => null,
+ ]);
+ }
+
+ public function test_create_without_assignment_returns_default_success_message(): void
+ {
+ $creator = User::factory()->createAssets()->create();
+ $model = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->readyToDeploy()->create();
+
+ $this->actingAsForApi($creator)
+ ->postJson(route('api.assets.store'), [
+ 'model_id' => $model->id,
+ 'status_id' => $status->id,
+ 'asset_tag' => 'CREATE-ONLY',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success')
+ ->assertMessagesAre(trans('admin/hardware/message.create.success'));
+ }
+
+ public function test_create_with_assignment_succeeds_and_checks_out_when_checkout_permission_granted(): void
+ {
+ $creator = User::factory()->createAssets()->checkoutAssets()->create();
+ $victim = User::factory()->create();
+ $model = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->readyToDeploy()->create();
+
+ $this->actingAsForApi($creator)
+ ->postJson(route('api.assets.store'), [
+ 'model_id' => $model->id,
+ 'status_id' => $status->id,
+ 'asset_tag' => 'CHECKOUT-OK',
+ 'assigned_user' => $victim->id,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success')
+ ->assertMessagesAre(trans('admin/hardware/message.create.success'));
+
+ $this->assertDatabaseHas('assets', [
+ 'asset_tag' => 'CHECKOUT-OK',
+ 'assigned_to' => $victim->id,
+ ]);
+ }
+}
diff --git a/tests/Feature/Assets/Api/StoreAssetTest.php b/tests/Feature/Assets/Api/StoreAssetTest.php
index 87a6ab8711..fb19eee810 100644
--- a/tests/Feature/Assets/Api/StoreAssetTest.php
+++ b/tests/Feature/Assets/Api/StoreAssetTest.php
@@ -33,7 +33,7 @@ class StoreAssetTest extends TestCase
$rtdLocation = Location::factory()->create();
$status = Statuslabel::factory()->readyToDeploy()->create();
$supplier = Supplier::factory()->create();
- $user = User::factory()->createAssets()->create();
+ $user = User::factory()->createAssets()->checkoutAssets()->create();
$userAssigned = User::factory()->create();
$response = $this->actingAsForApi($user)
@@ -567,7 +567,7 @@ class StoreAssetTest extends TestCase
{
$model = AssetModel::factory()->create();
$status = Statuslabel::factory()->readyToDeploy()->create();
- $user = User::factory()->createAssets()->create();
+ $user = User::factory()->createAssets()->checkoutAssets()->create();
$userAssigned = User::factory()->create();
$this->settings->enableAutoIncrement();
@@ -596,7 +596,7 @@ class StoreAssetTest extends TestCase
[$companyA, $companyB] = Company::factory()->count(2)->create();
- $actorInCompanyA = User::factory()->createAssets()->forCompany($companyA)->create();
+ $actorInCompanyA = User::factory()->createAssets()->checkoutAssets()->forCompany($companyA)->create();
$targetUserInCompanyB = User::factory()->forCompany($companyB)->create();
$model = AssetModel::factory()->create();
@@ -690,7 +690,7 @@ class StoreAssetTest extends TestCase
$model = AssetModel::factory()->create();
$status = Statuslabel::factory()->readyToDeploy()->create();
$location = Location::factory()->create();
- $user = User::factory()->createAssets()->create();
+ $user = User::factory()->createAssets()->checkoutAssets()->create();
$this->settings->enableAutoIncrement();
@@ -717,7 +717,7 @@ class StoreAssetTest extends TestCase
$model = AssetModel::factory()->create();
$status = Statuslabel::factory()->readyToDeploy()->create();
$asset = Asset::factory()->create();
- $user = User::factory()->createAssets()->create();
+ $user = User::factory()->createAssets()->checkoutAssets()->create();
$this->settings->enableAutoIncrement();
diff --git a/tests/Feature/Assets/Ui/StoreAssetCheckoutPermissionTest.php b/tests/Feature/Assets/Ui/StoreAssetCheckoutPermissionTest.php
new file mode 100644
index 0000000000..9824140f8e
--- /dev/null
+++ b/tests/Feature/Assets/Ui/StoreAssetCheckoutPermissionTest.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Tests\Feature\Assets\Ui;
+
+use App\Models\AssetModel;
+use App\Models\Statuslabel;
+use App\Models\User;
+use Tests\TestCase;
+
+class StoreAssetCheckoutPermissionTest extends TestCase
+{
+ public function test_web_create_with_assigned_user_without_checkout_permission_creates_asset_and_flags_skipped_checkout(): void
+ {
+ $creator = User::factory()->createAssets()->create();
+ $victim = User::factory()->create();
+ $model = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->readyToDeploy()->create();
+
+ $tag = 'CHECKOUT-GATE-UI-U-'.uniqid();
+ $this->actingAs($creator)
+ ->post(route('hardware.store'), [
+ 'asset_tags' => [1 => $tag],
+ 'model_id' => $model->id,
+ 'status_id' => $status->id,
+ 'assigned_user' => $victim->id,
+ ])
+ ->assertRedirect()
+ ->assertSessionHas('warning', trans('admin/hardware/message.create.checkout_skipped_no_permission'));
+
+ $this->assertDatabaseHas('assets', [
+ 'asset_tag' => $tag,
+ 'assigned_to' => null,
+ ]);
+ $this->assertDatabaseMissing('action_logs', [
+ 'target_id' => $victim->id,
+ 'target_type' => User::class,
+ 'action_type' => 'checkout',
+ ]);
+ }
+
+ public function test_web_create_without_assignment_does_not_flag_skipped_checkout(): void
+ {
+ $creator = User::factory()->createAssets()->create();
+ $model = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->readyToDeploy()->create();
+
+ $tag = 'CREATE-ONLY-UI-'.uniqid();
+ $response = $this->actingAs($creator)
+ ->post(route('hardware.store'), [
+ 'model_id' => $model->id,
+ 'status_id' => $status->id,
+ 'asset_tags' => [1 => $tag],
+ ])
+ ->assertRedirect();
+
+ $this->assertDatabaseHas('assets', ['asset_tag' => $tag]);
+ $this->assertNotSame(
+ trans('admin/hardware/message.create.checkout_skipped_no_permission'),
+ session('warning')
+ );
+ }
+
+ public function test_web_create_with_assignment_succeeds_when_checkout_permission_granted(): void
+ {
+ $creator = User::factory()->createAssets()->checkoutAssets()->create();
+ $victim = User::factory()->create();
+ $model = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->readyToDeploy()->create();
+
+ $tag = 'CHECKOUT-OK-UI-'.uniqid();
+ $this->actingAs($creator)
+ ->post(route('hardware.store'), [
+ 'model_id' => $model->id,
+ 'status_id' => $status->id,
+ 'asset_tags' => [1 => $tag],
+ 'assigned_user' => $victim->id,
+ ])
+ ->assertRedirect();
+
+ $this->assertDatabaseHas('assets', [
+ 'asset_tag' => $tag,
+ 'assigned_to' => $victim->id,
+ ]);
+ }
+}
diff --git a/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php b/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php
index c999ee3e36..da39535914 100644
--- a/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php
+++ b/tests/Feature/LicenseSeats/Api/LicenseSeatUpdateTest.php
@@ -99,7 +99,7 @@ class LicenseSeatUpdateTest extends TestCase
$asset = Asset::factory()->create();
$licenseSeat = LicenseSeat::factory()->assignedToAsset($asset)->create();
- $this->actingAsForApi(User::factory()->checkoutLicenses()->create())
+ $this->actingAsForApi(User::factory()->checkoutLicenses()->checkinLicenses()->create())
->patchJson($this->route($licenseSeat), [
'assigned_to' => null,
'asset_id' => null,
@@ -271,7 +271,7 @@ class LicenseSeatUpdateTest extends TestCase
'unreassignable_seat' => false,
]);
- $this->actingAsForApi(User::factory()->checkoutLicenses()->create())
+ $this->actingAsForApi(User::factory()->checkoutLicenses()->checkinLicenses()->create())
->patchJson($this->route($licenseSeat), [
'asset_id' => null,
'notes' => 'Checking in the seat',
@@ -303,7 +303,7 @@ class LicenseSeatUpdateTest extends TestCase
'unreassignable_seat' => false,
]);
- $this->actingAsForApi(User::factory()->checkoutLicenses()->create())
+ $this->actingAsForApi(User::factory()->checkoutLicenses()->checkinLicenses()->create())
->patchJson($this->route($licenseSeat), [
'assigned_to' => null,
'notes' => 'Checking in the seat',
@@ -330,7 +330,7 @@ class LicenseSeatUpdateTest extends TestCase
{
$licenseSeat = LicenseSeat::factory()->create(['asset_id' => 100000]);
- $this->actingAsForApi(User::factory()->checkoutLicenses()->create())
+ $this->actingAsForApi(User::factory()->checkoutLicenses()->checkinLicenses()->create())
->patchJson($this->route($licenseSeat), [
'asset_id' => null,
'assigned_to' => null,
@@ -360,7 +360,7 @@ class LicenseSeatUpdateTest extends TestCase
{
$licenseSeat = LicenseSeat::factory()->unreassignable()->create(['assigned_to' => 100000]);
- $this->actingAsForApi(User::factory()->checkoutLicenses()->create())
+ $this->actingAsForApi(User::factory()->checkoutLicenses()->checkinLicenses()->create())
->patchJson($this->route($licenseSeat), [
// purposefully leaving asset_id off here
// because it is a realistic scenario.
@@ -393,7 +393,7 @@ class LicenseSeatUpdateTest extends TestCase
$licenseSeat = LicenseSeat::factory()->assignedToAsset($asset)->create();
$licenseSeat->asset->delete();
- $this->actingAsForApi(User::factory()->checkoutLicenses()->create())
+ $this->actingAsForApi(User::factory()->checkoutLicenses()->checkinLicenses()->create())
->patchJson($this->route($licenseSeat), [
'asset_id' => null,
'notes' => 'Checking in the seat',
@@ -422,7 +422,7 @@ class LicenseSeatUpdateTest extends TestCase
$licenseSeat = LicenseSeat::factory()->unreassignable()->assignedToUser($user)->create();
$licenseSeat->user->delete();
- $this->actingAsForApi(User::factory()->checkoutLicenses()->create())
+ $this->actingAsForApi(User::factory()->checkoutLicenses()->checkinLicenses()->create())
->patchJson($this->route($licenseSeat), [
'assigned_to' => null,
'notes' => 'Checking in the seat',
diff --git a/tests/Feature/Licenses/Api/LicenseSeatUpdateCheckinPermissionTest.php b/tests/Feature/Licenses/Api/LicenseSeatUpdateCheckinPermissionTest.php
new file mode 100644
index 0000000000..6e6307d0d9
--- /dev/null
+++ b/tests/Feature/Licenses/Api/LicenseSeatUpdateCheckinPermissionTest.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Tests\Feature\Licenses\Api;
+
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\User;
+use Tests\TestCase;
+
+class LicenseSeatUpdateCheckinPermissionTest extends TestCase
+{
+ public function test_seat_update_that_clears_assignment_requires_checkin_permission(): void
+ {
+ $license = License::factory()->create();
+ $assignee = User::factory()->create();
+ $seat = LicenseSeat::factory()->for($license)->create([
+ 'assigned_to' => $assignee->id,
+ ]);
+
+ $actor = User::factory()->checkoutLicenses()->viewLicenses()->create();
+
+ $this->actingAsForApi($actor)
+ ->patchJson(route('api.licenses.seats.update', ['license' => $license->id, 'seat' => $seat->id]), [
+ 'assigned_to' => null,
+ ])
+ ->assertForbidden();
+
+ $seat->refresh();
+ $this->assertSame($assignee->id, $seat->assigned_to);
+ }
+
+ public function test_seat_update_that_clears_assignment_succeeds_when_checkin_permission_granted(): void
+ {
+ $license = License::factory()->create();
+ $assignee = User::factory()->create();
+ $seat = LicenseSeat::factory()->for($license)->create([
+ 'assigned_to' => $assignee->id,
+ ]);
+
+ $actor = User::factory()->checkoutLicenses()->checkinLicenses()->viewLicenses()->create();
+
+ $this->actingAsForApi($actor)
+ ->patchJson(route('api.licenses.seats.update', ['license' => $license->id, 'seat' => $seat->id]), [
+ 'assigned_to' => null,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $seat->refresh();
+ $this->assertNull($seat->assigned_to);
+ }
+
+ public function test_seat_update_that_reassigns_still_only_requires_checkout_permission(): void
+ {
+ $license = License::factory()->create();
+ $oldAssignee = User::factory()->create();
+ $newAssignee = User::factory()->create();
+ $seat = LicenseSeat::factory()->for($license)->create([
+ 'assigned_to' => $oldAssignee->id,
+ ]);
+
+ $actor = User::factory()->checkoutLicenses()->viewLicenses()->create();
+
+ $this->actingAsForApi($actor)
+ ->patchJson(route('api.licenses.seats.update', ['license' => $license->id, 'seat' => $seat->id]), [
+ 'assigned_to' => $newAssignee->id,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $seat->refresh();
+ $this->assertSame($newAssignee->id, $seat->assigned_to);
+ }
+}
diff --git a/tests/Feature/Users/Api/UserLicensesAuthorizationTest.php b/tests/Feature/Users/Api/UserLicensesAuthorizationTest.php
new file mode 100644
index 0000000000..bb19378ceb
--- /dev/null
+++ b/tests/Feature/Users/Api/UserLicensesAuthorizationTest.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Tests\Feature\Users\Api;
+
+use App\Models\Company;
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\User;
+use Tests\TestCase;
+
+class UserLicensesAuthorizationTest extends TestCase
+{
+ public function test_licenses_requires_view_users_permission(): void
+ {
+ $target = User::factory()->create();
+
+ $this->actingAsForApi(User::factory()->viewLicenses()->create())
+ ->getJson(route('api.users.licenselist', $target))
+ ->assertForbidden();
+ }
+
+ public function test_licenses_requires_view_licenses_permission(): void
+ {
+ $target = User::factory()->create();
+
+ $this->actingAsForApi(User::factory()->viewUsers()->create())
+ ->getJson(route('api.users.licenselist', $target))
+ ->assertForbidden();
+ }
+
+ public function test_licenses_blocks_cross_company_user_target_under_fmcs(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $admin = User::factory()->viewUsers()->viewLicenses()->forCompany($companyA)->create();
+ $target = User::factory()->forCompany($companyB)->create();
+ $license = License::factory()->create(['company_id' => $companyB->id, 'name' => 'ProbeCrossCompanyLicense']);
+ LicenseSeat::factory()->for($license)->create(['assigned_to' => $target->id]);
+
+ $response = $this->actingAsForApi($admin)
+ ->getJson(route('api.users.licenselist', $target));
+
+ // Either 403 (authorize on $user throws) or a JSON error response.
+ // Either way, no license row for a cross-company seat may appear.
+ $body = $response->getContent();
+ $this->assertStringNotContainsString($license->name, $body);
+ $this->assertStringNotContainsString('"total":1', $body);
+ }
+
+ public function test_licenses_returns_seats_for_same_company_target_under_fmcs(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $company = Company::factory()->create();
+ $admin = User::factory()->viewUsers()->viewLicenses()->forCompany($company)->create();
+ $target = User::factory()->forCompany($company)->create();
+ $license = License::factory()->create(['company_id' => $company->id]);
+ LicenseSeat::factory()->for($license)->create(['assigned_to' => $target->id]);
+
+ $this->actingAsForApi($admin)
+ ->getJson(route('api.users.licenselist', $target))
+ ->assertOk()
+ ->assertJsonPath('total', 1)
+ ->assertJsonPath('rows.0.name', $license->name);
+ }
+}
diff --git a/tests/Feature/Users/TransferUserItemsBehaviorTest.php b/tests/Feature/Users/TransferUserItemsBehaviorTest.php
index 6e7655ada1..247a49eb1a 100644
--- a/tests/Feature/Users/TransferUserItemsBehaviorTest.php
+++ b/tests/Feature/Users/TransferUserItemsBehaviorTest.php
@@ -154,6 +154,10 @@ class TransferUserItemsBehaviorTest extends TestCase
->viewUsers()
->checkinAssets()
->checkoutAssets()
+ ->checkinAccessories()
+ ->checkoutAccessories()
+ ->checkinLicenses()
+ ->checkoutLicenses()
->create();
}
}
diff --git a/tests/Feature/Users/TransferUserItemsPermissionScopingTest.php b/tests/Feature/Users/TransferUserItemsPermissionScopingTest.php
new file mode 100644
index 0000000000..28a8a796ff
--- /dev/null
+++ b/tests/Feature/Users/TransferUserItemsPermissionScopingTest.php
@@ -0,0 +1,133 @@
+<?php
+
+namespace Tests\Feature\Users;
+
+use App\Models\Accessory;
+use App\Models\AccessoryCheckout;
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\User;
+use Tests\TestCase;
+
+class TransferUserItemsPermissionScopingTest extends TestCase
+{
+ public function test_transfer_with_accessory_ids_requires_accessory_permissions(): void
+ {
+ $source = User::factory()->create();
+ $target = User::factory()->create();
+ $accessory = Accessory::factory()->create();
+ $checkout = AccessoryCheckout::create([
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $source->id,
+ 'assigned_type' => User::class,
+ 'created_by' => User::factory()->create()->id,
+ ]);
+
+ $assetOnlyActor = User::factory()
+ ->viewUsers()
+ ->checkinAssets()
+ ->checkoutAssets()
+ ->create();
+
+ $this->actingAs($assetOnlyActor)
+ ->post(route('users.transfer.store', $source), [
+ 'target_user_id' => $target->id,
+ 'accessory_checkout_ids' => [$checkout->id],
+ 'note' => 'test',
+ ])
+ ->assertForbidden();
+
+ $this->assertDatabaseHas('accessories_checkout', [
+ 'id' => $checkout->id,
+ 'assigned_to' => $source->id,
+ ]);
+ }
+
+ public function test_transfer_with_license_seat_ids_requires_license_permissions(): void
+ {
+ $source = User::factory()->create();
+ $target = User::factory()->create();
+ $license = License::factory()->create();
+ $seat = LicenseSeat::factory()->for($license)->create([
+ 'assigned_to' => $source->id,
+ ]);
+
+ $assetOnlyActor = User::factory()
+ ->viewUsers()
+ ->checkinAssets()
+ ->checkoutAssets()
+ ->create();
+
+ $this->actingAs($assetOnlyActor)
+ ->post(route('users.transfer.store', $source), [
+ 'target_user_id' => $target->id,
+ 'license_seat_ids' => [$seat->id],
+ 'note' => 'test',
+ ])
+ ->assertForbidden();
+
+ $seat->refresh();
+ $this->assertSame($source->id, $seat->assigned_to);
+ }
+
+ public function test_transfer_with_accessory_ids_succeeds_when_accessory_perms_granted(): void
+ {
+ $source = User::factory()->create();
+ $target = User::factory()->create();
+ $accessory = Accessory::factory()->create();
+ $checkout = AccessoryCheckout::create([
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $source->id,
+ 'assigned_type' => User::class,
+ 'created_by' => User::factory()->create()->id,
+ ]);
+
+ $actor = User::factory()
+ ->viewUsers()
+ ->checkinAssets()
+ ->checkoutAssets()
+ ->checkinAccessories()
+ ->checkoutAccessories()
+ ->create();
+
+ $this->actingAs($actor)
+ ->post(route('users.transfer.store', $source), [
+ 'target_user_id' => $target->id,
+ 'accessory_checkout_ids' => [$checkout->id],
+ 'note' => 'test',
+ ])
+ ->assertRedirect(route('users.show', $target));
+
+ $this->assertDatabaseHas('accessories_checkout', [
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $target->id,
+ ]);
+ }
+
+ public function test_asset_only_transfer_still_works_with_only_asset_perms(): void
+ {
+ $source = User::factory()->create();
+ $target = User::factory()->create();
+ $asset = \App\Models\Asset::factory()->create([
+ 'assigned_to' => $source->id,
+ 'assigned_type' => User::class,
+ ]);
+
+ $assetOnlyActor = User::factory()
+ ->viewUsers()
+ ->checkinAssets()
+ ->checkoutAssets()
+ ->create();
+
+ $this->actingAs($assetOnlyActor)
+ ->post(route('users.transfer.store', $source), [
+ 'target_user_id' => $target->id,
+ 'asset_ids' => [$asset->id],
+ 'note' => 'test',
+ ])
+ ->assertRedirect(route('users.show', $target));
+
+ $asset->refresh();
+ $this->assertSame($target->id, $asset->assigned_to);
+ }
+}