A free open source IT asset/license management system
License Importer: Do not require seats if just assigning licenses
snipe1125e595b41a
diff --git a/app/Importer/LicenseImporter.php b/app/Importer/LicenseImporter.phpindex 66cf3b2cba..a12707a93f 100644--- a/app/Importer/LicenseImporter.php+++ b/app/Importer/LicenseImporter.php@@ -237,6 +237,29 @@ class LicenseImporter extends ItemImporter$targetLicense = $license->freeSeat();if (is_null($targetLicense)) {+ // No free seats left on the license. The row's license+ // create/update already succeeded (or was correctly skipped as+ // a duplicate), but the seat-assignment portion cannot proceed.+ // Surface this as an errored row so the wizard tally reflects+ // that the caller's intent was only partially fulfilled, and+ // log a specific message the caller can act on. Without this,+ // a seat-assignment CSV whose row count exceeds the license's+ // available seats silently drops the trailing rows on the+ // floor (reported in #19467 follow-up).+ $target_label = $checkout_target?->name+ ?? $checkout_target?->username+ ?? ($asset ? $asset->display_name : trans('general.unassigned'));++ $this->log(trans('admin/licenses/message.import.no_free_seats', [+ 'license' => $license->name,+ 'target' => $target_label,+ ]));+ $this->addErrorToBag($license, 'seats', trans('admin/licenses/message.import.no_free_seats', [+ 'license' => $license->name,+ 'target' => $target_label,+ ]));+ $this->recordErrored();+return;}diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.phpindex bc09c83716..27f6ea8a01 100644--- a/app/Livewire/Importer.php+++ b/app/Livewire/Importer.php@@ -161,7 +161,19 @@ class Importer extends Component'accessory' => [Accessory::class, ['item_name' => 'name', 'category' => 'category_id']],'consumable' => [Consumable::class, ['item_name' => 'name', 'category' => 'category_id']],'component' => [ComponentModel::class, ['item_name' => 'name', 'category' => 'category_id']],- 'license' => [License::class, ['item_name' => 'name', 'seats' => 'seats']],+ // license: only item_name is enforced at wizard level. `seats` is+ // the license's total capacity count (not a per-seat pivot id).+ // License imports currently drive three different intents depending+ // on the row shape: (1) create a new License with N seats,+ // (2) update an existing License's capacity, (3) assign a user or+ // asset to one of an existing License's free seats. The importer+ // figures out which path each row takes at process time. seats is+ // only needed for (1) and (2); (3) doesn't reference it. Since the+ // wizard can't know per-row which path a caller intends, we don't+ // enforce seats at the wizard level. Server-side validation still+ // enforces `seats` on the create path per License::$rules. See+ // issue #19467.+ 'license' => [License::class, ['item_name' => 'name']],'user' => [User::class, ['first_name' => 'first_name', 'username' => 'username']],'location' => [Location::class, ['name' => 'name']],'supplier' => [Supplier::class, ['name' => 'name']],diff --git a/resources/lang/en-US/admin/licenses/message.php b/resources/lang/en-US/admin/licenses/message.phpindex 8b589e4f8e..95c344ec1d 100644--- a/resources/lang/en-US/admin/licenses/message.php+++ b/resources/lang/en-US/admin/licenses/message.php@@ -62,4 +62,8 @@ return ['success' => 'The license was checked in successfully',],+ 'import' => [+ 'no_free_seats' => 'License ":license" has no free seats. ":target" was not assigned to a seat.',+ ],+];diff --git a/tests/Feature/Importing/Api/ImportLicenseTest.php b/tests/Feature/Importing/Api/ImportLicenseTest.phpindex 76c1c7dcca..daac9be30f 100644--- a/tests/Feature/Importing/Api/ImportLicenseTest.php+++ b/tests/Feature/Importing/Api/ImportLicenseTest.php@@ -635,6 +635,53 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe$this->assertSame(2, LicenseSeat::where('license_id', $existingLicense->id)->whereNotNull('assigned_to')->count());}+ #[Test]+ public function import_records_error_when_license_runs_out_of_free_seats(): void+ {+ // Regression for #19467 follow-up. If a seat-assignment CSV exceeds+ // the target license's free-seat count, the trailing rows should+ // surface as errored tally entries with a specific "no free seats"+ // message, not silently be dropped on the floor.+ [$firstUser, $secondUser, $thirdUser] = User::factory()->count(3)->create();+ $existingLicense = License::factory()->create([+ 'serial' => null,+ 'seats' => 2,+ ]);++ $rowShape = [+ 'licenseName' => $existingLicense->name,+ 'category' => 'Software',+ 'seats' => 2,+ ];++ $file = new ImportFileBuilder([+ array_merge($rowShape, ['checkoutUsername' => $firstUser->username]),+ array_merge($rowShape, ['checkoutUsername' => $secondUser->username]),+ array_merge($rowShape, ['checkoutUsername' => $thirdUser->username]),+ ]);++ $import = Import::factory()->license()->create(['file_path' => $file->saveToImportsDirectory()]);++ $this->actingAsForApi(User::factory()->superuser()->create());+ $response = $this->importFileResponse([+ 'import' => $import->id,+ 'import-update' => true,+ ]);++ // The API convention for import runs with any errored rows is HTTP+ // 500 (see Api\ImportController::importFile line 352). The interesting+ // assertion is that the tally reflects the failure and the message+ // surfaces the specific "no free seats" reason.+ $response->assertStatus(500);+ $response->assertJson(['status' => 'import-errors']);+ $this->assertSame(1, $response->json('payload.tally.errored'), 'The row that had no free seat should be counted as errored in the tally.');+ $this->assertStringContainsString('no free seats', $response->content());++ // Two seats successfully assigned (rows 1 and 2), third row's seat+ // assignment failed and was recorded.+ $this->assertSame(2, LicenseSeat::where('license_id', $existingLicense->id)->whereNotNull('assigned_to')->count());+ }+#[Test]public function update_matches_existing_license_with_null_serial_when_csv_omits_serial(): void{diff --git a/tests/Feature/Livewire/ImporterTest.php b/tests/Feature/Livewire/ImporterTest.phpindex 70cd4eeefa..942f82828f 100644--- a/tests/Feature/Livewire/ImporterTest.php+++ b/tests/Feature/Livewire/ImporterTest.php@@ -510,6 +510,27 @@ class ImporterTest extends TestCase});}+ public function test_license_required_fields_do_not_include_seats(): void+ {+ // Regression for #19467 follow-up. License::$rules requires seats+ // for the create path, but a seat-assignment import (matching an+ // existing license) does not use seats. The wizard cannot know+ // per-row whether the caller intends create or update, so it must+ // not enforce seats at wizard level. Per-row server-side validation+ // still enforces seats on License::save() for genuine creates.+ Livewire::actingAs(User::factory()->canImport()->create())+ ->test(Importer::class)+ ->tap(function ($c) {+ $required = $c->instance()->requiredForType('license');+ $this->assertNotContains(+ 'seats',+ $required,+ 'Seats should not be flagged as required at the wizard level for license imports.',+ );+ $this->assertContains('item_name', $required, 'item_name should still be required at the wizard level for license imports.');+ });+ }+public function test_asset_required_fields_do_not_include_custom_fields_required_in_some_fieldsets_only(): void{// Regression for #19468. A CSV of assets can span multiple asset