A free open source IT asset/license management system
Add optional ‘reassign’ to licenses API to qiuckly checkin and re-checkout
snipe0bdc819d20a9
diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.phpindex 515d8a7242..cabe1dda96 100644--- a/app/Http/Controllers/Api/LicensesController.php+++ b/app/Http/Controllers/Api/LicensesController.php@@ -12,6 +12,7 @@ use App\Http\Transformers\LicenseSeatsTransformer;use App\Http\Transformers\LicensesTransformer;use App\Http\Transformers\SelectlistTransformer;use App\Models\Asset;+use App\Models\CheckoutAcceptance;use App\Models\Company;use App\Models\License;use App\Models\LicenseSeat;@@ -325,6 +326,8 @@ class LicensesController extends Controller'assigned_to' => 'required_if:target_type,user|integer|nullable','asset_id' => 'required_if:target_type,asset|integer|nullable','notes' => 'sometimes|string|nullable',+ // Opt-in flag for instantly reassigning an occupied seat.+ 'reassign' => 'sometimes|boolean',]);if ($license->isInactive()) {@@ -354,6 +357,45 @@ class LicensesController extends Controllerreturn;}+ // GHSA-r25g-f428-466r: reject occupied seats on explicit-id+ // checkout by default.+ if ($licenseSeat->assigned_to !== null || $licenseSeat->asset_id !== null) {+ if (!($validated['reassign'] ?? false)) {+ $errorResponse = response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/licenses/message.checkout.unavailable')));++ return;+ }++ if (!$licenseSeat->license->reassignable) {+ $errorResponse = response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/licenses/message.checkout.unavailable')));++ return;+ }++ // Fire CheckoutableCheckedIn for the+ // current holder and delete their pending acceptance+ $displaced = $licenseSeat->assigned_to+ ? User::withoutGlobalScopes()->find($licenseSeat->assigned_to)+ : ($licenseSeat->asset_id ? Asset::withoutGlobalScopes()->find($licenseSeat->asset_id) : null);++ if ($displaced instanceof User) {+ CheckoutAcceptance::pending()+ ->where('checkoutable_type', LicenseSeat::class)+ ->where('checkoutable_id', $licenseSeat->id)+ ->where('assigned_to_id', $displaced->id)+ ->get()+ ->each(fn($a) => $a->delete());+ }++ $licenseSeat->assigned_to = null;+ $licenseSeat->asset_id = null;+ $licenseSeat->save();++ if ($displaced) {+ event(new CheckoutableCheckedIn($licenseSeat, $displaced, auth()->user(), $validated['notes'] ?? null));+ }+ }+if ($validated['target_type'] === 'user') {$target = User::withoutGlobalScopes()->whereNull('deleted_at')->find($validated['assigned_to'] ?? null);if (! $target) {diff --git a/app/Http/Controllers/Licenses/LicenseCheckoutController.php b/app/Http/Controllers/Licenses/LicenseCheckoutController.phpindex a45c877f9b..9d49bc300a 100644--- a/app/Http/Controllers/Licenses/LicenseCheckoutController.php+++ b/app/Http/Controllers/Licenses/LicenseCheckoutController.php@@ -215,6 +215,16 @@ class LicenseCheckoutController extends Controllerthrow new HttpResponseException(redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkout.mismatch')));}+ // GHSA-r25g-f428-466r: reject retired-unreassignable seats.+ if ($licenseSeat->unreassignable_seat) {+ throw new HttpResponseException(redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkout.unavailable')));+ }++ // GHSA-r25g-f428-466r: reject occupied seats.+ if ($licenseSeat->assigned_to !== null || $licenseSeat->asset_id !== null) {+ throw new HttpResponseException(redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkout.unavailable')));+ }+return $licenseSeat;}diff --git a/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.php b/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.phpindex 439e68e4c8..255e3704e0 100644--- a/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.php@@ -196,4 +196,65 @@ class LicenseCheckoutTest extends TestCase$response->assertSessionHas('sign_in_place', true);}++ // -----------------------------------------------------------------------+ // GHSA-r25g-f428-466r regression coverage.+ //+ // Web license checkout must reject explicit-seat-id requests that target+ // a retired unreassignable seat (which would push a non-reassignable+ // license past its intended activation count) or an already-occupied seat+ // (which would silently displace the current holder with no audit trail).+ // The web path has no reassign opt-in flag: any reassignment goes through+ // the API's `reassign: true` request or through the UserItemTransferController+ // seat-transfer flow, which produce a proper checkin-then-checkout event+ // pair.+ // -----------------------------------------------------------------------++ public function test_web_checkout_by_explicit_seat_id_rejects_retired_unreassignable_seat()+ {+ $license = License::factory()->create(['seats' => 2, 'reassignable' => 0]);+ $seats = $license->licenseseats()->orderBy('id')->get();+ $retiredSeat = $seats[0];+ // unreassignable_seat is not fillable, so ->update() would silently+ // skip it. Direct property assignment + save persists it.+ $retiredSeat->unreassignable_seat = true;+ $retiredSeat->assigned_to = null;+ $retiredSeat->save();++ $target = User::factory()->create();++ $this->actingAs(User::factory()->admin()->create())+ ->post(route('licenses.checkout.save', ['licenseId' => $license->id, 'seatId' => $retiredSeat->id]), [+ 'assigned_user' => $target->id,+ 'redirect_option' => 'index',+ ])+ ->assertSessionHas('error');++ $this->assertNull($retiredSeat->fresh()->assigned_to);+ $this->assertTrue((bool) $retiredSeat->fresh()->unreassignable_seat);+ }++ public function test_web_checkout_by_explicit_seat_id_rejects_seat_already_assigned_to_user()+ {+ $license = License::factory()->create(['seats' => 2]);+ $seats = $license->licenseseats()->orderBy('id')->get();++ $originalHolder = User::factory()->create();+ $seats[0]->update(['assigned_to' => $originalHolder->id]);++ $newTarget = User::factory()->create();++ $this->actingAs(User::factory()->admin()->create())+ ->post(route('licenses.checkout.save', ['licenseId' => $license->id, 'seatId' => $seats[0]->id]), [+ 'assigned_user' => $newTarget->id,+ 'redirect_option' => 'index',+ ])+ ->assertSessionHas('error');++ $this->assertEquals(+ $originalHolder->id,+ $seats[0]->fresh()->assigned_to,+ 'Web checkout must never silently displace the current holder.',+ );+ }}diff --git a/tests/Feature/Licenses/Api/LicenseCheckoutCheckinTest.php b/tests/Feature/Licenses/Api/LicenseCheckoutCheckinTest.phpindex 3c9a85537f..c2c41c520d 100644--- a/tests/Feature/Licenses/Api/LicenseCheckoutCheckinTest.php+++ b/tests/Feature/Licenses/Api/LicenseCheckoutCheckinTest.php@@ -5,6 +5,7 @@ namespace Tests\Feature\Licenses\Api;use App\Events\CheckoutableCheckedIn;use App\Events\CheckoutableCheckedOut;use App\Models\Asset;+use App\Models\CheckoutAcceptance;use App\Models\License;use App\Models\LicenseSeat;use App\Models\User;@@ -315,4 +316,217 @@ class LicenseCheckoutCheckinTest extends TestCaseEvent::assertDispatched(CheckoutableCheckedOut::class);Event::assertDispatched(CheckoutableCheckedIn::class);}++ // ---------------------------------------------------------------------------+ // GHSA-r25g-f428-466r regression coverage.+ //+ // Explicit-seat-id checkouts must reject retired-unreassignable seats and+ // must reject already-occupied seats by default. Occupied-seat checkout is+ // only allowed when the caller opts in with `reassign: true`, in which+ // case the API does a proper checkin-then-checkout (fires+ // CheckoutableCheckedIn for the displaced holder, deletes their pending+ // acceptance, then fires CheckoutableCheckedOut for the new target).+ // Non-reassignable licenses reject even with the flag set.+ // ---------------------------------------------------------------------------++ #[Test]+ public function checkout_by_explicit_seat_id_rejects_retired_unreassignable_seat(): void+ {+ Event::fake([CheckoutableCheckedOut::class]);++ // Non-reassignable license: checking a seat back in retires it by+ // flipping unreassignable_seat=1 and leaving assigned_to null.+ $license = License::factory()->create(['seats' => 2, 'reassignable' => 0]);+ $seats = $license->licenseseats()->orderBy('id')->get();+ $retiredSeat = $seats[0];+ // unreassignable_seat is not fillable, so ->update() would silently+ // skip it. Direct property assignment + save persists it.+ $retiredSeat->unreassignable_seat = true;+ $retiredSeat->assigned_to = null;+ $retiredSeat->save();++ $target = User::factory()->create();++ $this->actingAsForApi(User::factory()->checkoutLicenses()->create())+ ->postJson(route('api.licenses.checkout', $license->id), [+ 'seat_id' => $retiredSeat->id,+ 'target_type' => 'user',+ 'assigned_to' => $target->id,+ ])+ ->assertJson(['status' => 'error']);++ $this->assertNull($retiredSeat->fresh()->assigned_to);+ $this->assertTrue((bool) $retiredSeat->fresh()->unreassignable_seat);++ Event::assertNotDispatched(CheckoutableCheckedOut::class);+ }++ #[Test]+ public function checkout_by_explicit_seat_id_rejects_seat_already_assigned_to_user_by_default(): void+ {+ Event::fake([CheckoutableCheckedOut::class, CheckoutableCheckedIn::class]);++ $license = License::factory()->create(['seats' => 2]);+ $seats = $license->licenseseats()->orderBy('id')->get();++ $originalHolder = User::factory()->create();+ $seats[0]->update(['assigned_to' => $originalHolder->id]);++ $newTarget = User::factory()->create();++ $this->actingAsForApi(User::factory()->checkoutLicenses()->create())+ ->postJson(route('api.licenses.checkout', $license->id), [+ 'seat_id' => $seats[0]->id,+ 'target_type' => 'user',+ 'assigned_to' => $newTarget->id,+ ])+ ->assertJson(['status' => 'error']);++ $this->assertEquals(+ $originalHolder->id,+ $seats[0]->fresh()->assigned_to,+ 'Default checkout must not silently displace the current holder.',+ );++ Event::assertNotDispatched(CheckoutableCheckedOut::class);+ Event::assertNotDispatched(CheckoutableCheckedIn::class);+ }++ #[Test]+ public function checkout_by_explicit_seat_id_rejects_seat_already_assigned_to_asset_by_default(): void+ {+ Event::fake([CheckoutableCheckedOut::class]);++ $license = License::factory()->create(['seats' => 2]);+ $seats = $license->licenseseats()->orderBy('id')->get();++ $asset = Asset::factory()->create();+ $seats[0]->update(['asset_id' => $asset->id]);++ $newTarget = User::factory()->create();++ $this->actingAsForApi(User::factory()->checkoutLicenses()->create())+ ->postJson(route('api.licenses.checkout', $license->id), [+ 'seat_id' => $seats[0]->id,+ 'target_type' => 'user',+ 'assigned_to' => $newTarget->id,+ ])+ ->assertJson(['status' => 'error']);++ $this->assertEquals(+ $asset->id,+ $seats[0]->fresh()->asset_id,+ 'A seat already assigned to an asset must not be silently reassigned to a user.',+ );++ Event::assertNotDispatched(CheckoutableCheckedOut::class);+ }++ #[Test]+ public function checkout_with_reassign_flag_reassigns_occupied_seat_and_fires_both_events(): void+ {+ Event::fake([CheckoutableCheckedOut::class, CheckoutableCheckedIn::class]);++ $license = License::factory()->create(['seats' => 2, 'reassignable' => 1]);+ $seats = $license->licenseseats()->orderBy('id')->get();++ $originalHolder = User::factory()->create();+ $seats[0]->update(['assigned_to' => $originalHolder->id]);++ $newTarget = User::factory()->create();++ $this->actingAsForApi(User::factory()->checkoutLicenses()->create())+ ->postJson(route('api.licenses.checkout', $license->id), [+ 'seat_id' => $seats[0]->id,+ 'target_type' => 'user',+ 'assigned_to' => $newTarget->id,+ 'reassign' => true,+ ])+ ->assertOk()+ ->assertJson(['status' => 'success']);++ $this->assertEquals(+ $newTarget->id,+ $seats[0]->fresh()->assigned_to,+ 'With reassign flag, the seat must transfer to the new target.',+ );++ Event::assertDispatched(+ CheckoutableCheckedIn::class,+ fn (CheckoutableCheckedIn $event) => $event->checkedOutTo->is($originalHolder),+ );+ Event::assertDispatched(+ CheckoutableCheckedOut::class,+ fn (CheckoutableCheckedOut $event) => $event->checkedOutTo->is($newTarget),+ );+ }++ #[Test]+ public function checkout_with_reassign_flag_rejects_when_license_is_non_reassignable(): void+ {+ Event::fake([CheckoutableCheckedOut::class, CheckoutableCheckedIn::class]);++ $license = License::factory()->create(['seats' => 2, 'reassignable' => 0]);+ $seats = $license->licenseseats()->orderBy('id')->get();++ $originalHolder = User::factory()->create();+ $seats[0]->update(['assigned_to' => $originalHolder->id]);++ $newTarget = User::factory()->create();++ $this->actingAsForApi(User::factory()->checkoutLicenses()->create())+ ->postJson(route('api.licenses.checkout', $license->id), [+ 'seat_id' => $seats[0]->id,+ 'target_type' => 'user',+ 'assigned_to' => $newTarget->id,+ 'reassign' => true,+ ])+ ->assertJson(['status' => 'error']);++ $this->assertEquals(+ $originalHolder->id,+ $seats[0]->fresh()->assigned_to,+ 'Non-reassignable licenses must reject the reassign flag entirely.',+ );++ Event::assertNotDispatched(CheckoutableCheckedOut::class);+ Event::assertNotDispatched(CheckoutableCheckedIn::class);+ }++ #[Test]+ public function checkout_with_reassign_flag_deletes_pending_acceptance_for_displaced_holder(): void+ {+ $license = License::factory()->create(['seats' => 2, 'reassignable' => 1]);+ $seats = $license->licenseseats()->orderBy('id')->get();++ $originalHolder = User::factory()->create();+ $seats[0]->update(['assigned_to' => $originalHolder->id]);++ // Seed a pending acceptance for the original holder against this+ // seat. The reassign flow must delete it as part of the displacement.+ // Direct property assignment because these columns are not in the+ // model's fillable list. The acceptance table only records+ // assigned_to_id (user-scoped), so no type discriminator here.+ $pendingAcceptance = new CheckoutAcceptance;+ $pendingAcceptance->checkoutable_type = LicenseSeat::class;+ $pendingAcceptance->checkoutable_id = $seats[0]->id;+ $pendingAcceptance->assigned_to_id = $originalHolder->id;+ $pendingAcceptance->save();++ $newTarget = User::factory()->create();++ $this->actingAsForApi(User::factory()->checkoutLicenses()->create())+ ->postJson(route('api.licenses.checkout', $license->id), [+ 'seat_id' => $seats[0]->id,+ 'target_type' => 'user',+ 'assigned_to' => $newTarget->id,+ 'reassign' => true,+ ])+ ->assertOk();++ $this->assertNull(+ CheckoutAcceptance::find($pendingAcceptance->id),+ 'Reassign must delete the displaced holder\'s pending acceptance row so the audit trail does not carry a stale acceptance for an item they no longer hold.',+ );+ }}