Skip to content

A free open source IT asset/license management system

Sync Adapters: Added default fieldset selection for newly created models

snipe2c2b592e9726
diff --git a/app/SyncAdapters/SyncAdapter.php b/app/SyncAdapters/SyncAdapter.php
index ae171bdc9c..390fc41a6a 100644
--- a/app/SyncAdapters/SyncAdapter.php
+++ b/app/SyncAdapters/SyncAdapter.php
@@ -563,6 +563,7 @@ abstract class SyncAdapter
$slug.'_asset_tag_pattern' => ['nullable', 'string', 'max:191', new \App\Rules\AssetTagPatternRule],
$slug.'_default_category_id' => ['nullable', 'integer', 'exists:categories,id'],
$slug.'_default_status_id' => ['required', 'integer', 'exists:status_labels,id'],
+ $slug.'_default_fieldset_id' => ['nullable', 'integer', 'exists:custom_fieldsets,id'],
$slug.'_user_match_strategy' => ['nullable', 'string', 'in:none,email,username,username_then_email'],
$slug.'_adopt_by_serial' => ['nullable', 'boolean'],
$slug.'_create_snipeit_assets_on_pull' => ['nullable', 'boolean'],
@@ -836,6 +837,17 @@ abstract class SyncAdapter
(string) $request->input($slug.'_default_status_id', ''),
);
+ // Default fieldset for AssetModels auto-created from this
+ // adapter. Empty leaves fieldset_id null on new models (today's
+ // behavior). Admins pick a fieldset here so custom fields show
+ // on devices discovered by the adapter without having to
+ // revisit each new model by hand.
+ SyncAdapterConfig::put(
+ $this->instance->id,
+ 'default_fieldset_id',
+ (string) $request->input($slug.'_default_fieldset_id', ''),
+ );
+
// User match strategy for sync-driven assignments. 'none'
// disables the lookup entirely. Anything else the accessor
// doesn't recognize also degrades to 'none'.
@@ -1597,6 +1609,21 @@ abstract class SyncAdapter
return $stored === null || $stored === '' ? null : (int) $stored;
}
+ /**
+ * Default fieldset for AssetModels auto-created from this adapter.
+ * When set, resolveModelIdByName stamps this id on the new model's
+ * fieldset_id so custom fields show on synced devices without the
+ * admin revisiting each new model after a pull. Null leaves
+ * fieldset_id unset (today's behavior for installs that haven't
+ * configured this).
+ */
+ public function defaultFieldsetId(): ?int
+ {
+ $stored = SyncAdapterConfig::get($this->instance->id, 'default_fieldset_id');
+
+ return $stored === null || $stored === '' ? null : (int) $stored;
+ }
+
/**
* How the sync path resolves a Snipe-IT user from the vendor's
* assigned-user data. Returns one of:
diff --git a/app/SyncAdapters/SyncsHostFromRecord.php b/app/SyncAdapters/SyncsHostFromRecord.php
index 155f8214af..95311feac5 100644
--- a/app/SyncAdapters/SyncsHostFromRecord.php
+++ b/app/SyncAdapters/SyncsHostFromRecord.php
@@ -1091,6 +1091,10 @@ trait SyncsHostFromRecord
$model->name = $modelName;
$model->category_id = self::categoryIdFor($instance, $record);
$model->manufacturer_id = self::manufacturerIdFor($record);
+ $adapter = $instance?->adapter();
+ if ($adapter instanceof SyncAdapter) {
+ $model->fieldset_id = $adapter->defaultFieldsetId();
+ }
$model->save();
return $model->id;
diff --git a/resources/lang/en-US/admin/settings/sync_adapters.php b/resources/lang/en-US/admin/settings/sync_adapters.php
index 2e3d9734b1..a4b05d7aa6 100644
--- a/resources/lang/en-US/admin/settings/sync_adapters.php
+++ b/resources/lang/en-US/admin/settings/sync_adapters.php
@@ -110,6 +110,8 @@ return [
'default_category_help' => 'When a device from this adapter reports a hardware model that doesn\'t exist in Snipe-IT yet, a new asset model is created and placed in this category. Leave blank to use the "Discovered Hardware" category.',
'default_status' => 'Default Status Label',
'default_status_help' => 'When a device from this adapter is created in Snipe-IT for the first time, its status label is set to this value. Leave blank to use the first deployable status label (or the first status label if none are marked deployable).',
+ 'default_fieldset' => 'Model Fieldset',
+ 'default_fieldset_help' => 'When this adapter has to create a new asset model (because the vendor reported a hardware model Snipe-IT doesn\'t know about), the new model is linked to this fieldset so your custom fields show on devices the adapter discovers. Leave blank to leave the fieldset unset on new models.',
'user_match_strategy' => 'Assign to Snipe-IT user',
'user_match_strategy_help' => 'When a match is found, the asset checks out to that user. Missing or unmatched users are skipped and written to <code>storage/logs/sync-adapters.log</code> as warnings. Existing assignments are never cleared by a payload that omits the user field.',
'user_match_none' => 'Do not assign users',
diff --git a/resources/views/blade/sync-adapter/form.blade.php b/resources/views/blade/sync-adapter/form.blade.php
index 866adfcd6f..05ceb5674c 100644
--- a/resources/views/blade/sync-adapter/form.blade.php
+++ b/resources/views/blade/sync-adapter/form.blade.php
@@ -159,10 +159,27 @@
:help_text="trans('admin/settings/sync_adapters.default_category_help')"
/>
- {{-- Default status label for auto-created assets. Required so
- admins have to consciously pick a workflow bucket for
- discovered devices instead of Snipe-IT defaulting to
- something surprising. --}}
+ {{-- Default fieldset for AssetModels auto-created --}}
+ <x-form.row
+ :label="trans('admin/settings/sync_adapters.default_fieldset')"
+ :name="$slug . '_default_fieldset_id'"
+ input_div_class="col-md-7"
+ :help_text="trans('admin/settings/sync_adapters.default_fieldset_help')"
+ >
+ <x-slot:input>
+ <x-input.select
+ :name="$slug . '_default_fieldset_id'"
+ :id="$slug . '_default_fieldset_id'"
+ :options="\App\Models\CustomFieldset::orderBy('name')->pluck('name', 'id')->all()"
+ :selected="old($slug . '_default_fieldset_id', $adapter->defaultFieldsetId())"
+ :includeEmpty="true"
+ style="width: 100%"
+ :disabled="$locked"
+ />
+ </x-slot:input>
+ </x-form.row>
+
+ {{-- Default status label for auto-created assets --}}
<x-form.row
:label="trans('admin/settings/sync_adapters.default_status')"
:name="$slug . '_default_status_id'"
diff --git a/tests/Feature/SyncAdapters/DefaultFieldsetAutoCreateTest.php b/tests/Feature/SyncAdapters/DefaultFieldsetAutoCreateTest.php
new file mode 100644
index 0000000000..8005b47f3e
--- /dev/null
+++ b/tests/Feature/SyncAdapters/DefaultFieldsetAutoCreateTest.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Models\AssetModel;
+use App\Models\CustomFieldset;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\HostInventoryRecord;
+use App\SyncAdapters\SyncAdapter;
+use Tests\TestCase;
+
+class DefaultFieldsetAutoCreateTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_auto_created_model_gets_adapter_configured_default_fieldset()
+ {
+ $fieldset = CustomFieldset::create(['name' => 'Fleet Hardware Fieldset']);
+
+ $instance = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'default_fieldset_id', (string) $fieldset->id);
+
+ SyncAdapter::syncFromRecord(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: '42',
+ hostname: 'wksn-42',
+ hardwareSerial: 'SN-DEFAULT-FIELDSET-1',
+ hardwareModel: 'ThinkPad X1',
+ manufacturer: 'Lenovo',
+ ));
+
+ $model = AssetModel::where('name', 'ThinkPad X1')->first();
+ $this->assertNotNull($model);
+ $this->assertSame(
+ $fieldset->id,
+ $model->fieldset_id,
+ 'Auto-created AssetModel must inherit the adapter instance\'s configured default_fieldset_id so custom fields show on synced devices.'
+ );
+ }
+
+ public function test_auto_created_model_has_null_fieldset_when_adapter_not_configured()
+ {
+ SyncAdapter::syncFromRecord(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: '43',
+ hostname: 'wksn-43',
+ hardwareSerial: 'SN-DEFAULT-FIELDSET-2',
+ hardwareModel: 'ThinkPad X2',
+ manufacturer: 'Lenovo',
+ ));
+
+ $model = AssetModel::where('name', 'ThinkPad X2')->first();
+ $this->assertNotNull($model);
+ $this->assertNull(
+ $model->fieldset_id,
+ 'When default_fieldset_id is unset, new auto-created models must stay fieldset-less. This preserves pre-feature behavior for installs that have not opted in.'
+ );
+ }
+
+ public function test_existing_model_match_does_not_overwrite_existing_fieldset()
+ {
+ $vendorFieldset = CustomFieldset::create(['name' => 'Vendor Default Fieldset']);
+ $adminChosenFieldset = CustomFieldset::create(['name' => 'Admin Chosen Fieldset']);
+
+ $preExisting = AssetModel::factory()->create([
+ 'name' => 'MacBook Pro 16',
+ 'fieldset_id' => $adminChosenFieldset->id,
+ ]);
+
+ $instance = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'default_fieldset_id', (string) $vendorFieldset->id);
+
+ SyncAdapter::syncFromRecord(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: '44',
+ hostname: 'wksn-44',
+ hardwareSerial: 'SN-DEFAULT-FIELDSET-3',
+ hardwareModel: 'MacBook Pro 16',
+ manufacturer: 'Apple',
+ ));
+
+ $preExisting->refresh();
+ $this->assertSame(
+ $adminChosenFieldset->id,
+ $preExisting->fieldset_id,
+ 'An existing AssetModel matched by exact name must keep its own fieldset. default_fieldset_id only stamps new models, never overwrites existing ones.'
+ );
+ }
+}