A free open source IT asset/license management system
Make config a JSON blob :(
snipe2a13b9c0df73
diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.phpindex 6a56a8456d..73d534dfec 100644--- a/app/Http/Controllers/SettingsController.php+++ b/app/Http/Controllers/SettingsController.php@@ -1005,22 +1005,13 @@ class SettingsController extends Controller$clone->created_by = auth()->id();$clone->save();- // Copy every config row keyed to the source instance into the- // new one. Encrypted secrets copy as-is since both rows use- // the same APP_KEY. Runtime state (last_synced_at, etc.)- // lives on the instance row, not in config, so nothing- // survives from the source's operational history.- $sourceConfig = \App\Models\SyncAdapterConfig::query()- ->where('sync_adapter_instance_id', $instance->id)- ->get();-- foreach ($sourceConfig as $row) {- \App\Models\SyncAdapterConfig::query()->create([- 'sync_adapter_instance_id' => $clone->id,- 'config_key' => $row->config_key,- 'value' => $row->value,- ]);- }+ // Copy the whole config blob to the new instance. Encrypted+ // secrets copy as-is since both rows use the same APP_KEY.+ // Runtime state (last_synced_at, etc.) lives on its own+ // instance columns, not in config, so nothing survives from+ // the source's operational history.+ $clone->config = $instance->config;+ $clone->save();return redirect()->route('settings.adapters.index', ['adapter' => $clone->slug])->with('success', trans('admin/settings/sync_adapters.instance_cloned', ['label' => $instance->label]));@@ -1393,7 +1384,7 @@ class SettingsController extends Controller/*** Refresh the cached list of vendor groups for an adapter* instance. Calls the adapter's fetchGroups() and persists the- * result to sync_adapter_settings so the settings page can render+ * result to the instance's config blob so the settings page can render* the mapping table without hitting the vendor on every page load.** Only meaningful for adapters that opt into supportsGroupScoping().@@ -1449,7 +1440,7 @@ class SettingsController extends Controller* adapter that opts into supportsVendorCustomFields(). Parallel* to postAdapterRefreshGroups: hits the vendor's custom-fields* listing endpoint, stores the normalized list under- * sync_adapter_settings.vendor_custom_fields, and redirects back+ * the instance's config.vendor_custom_fields key, and redirects back* to the adapter settings page with a count.*/public function postAdapterRefreshCustomFields(\App\Models\SyncAdapterInstance $instance): RedirectResponse@@ -1505,10 +1496,8 @@ class SettingsController extends Controllerreturn redirect()->back()->with('error', trans('general.feature_disabled'));}- \App\Models\SyncAdapterConfig::query()- ->where('sync_adapter_instance_id', $instance->id)- ->delete();-+ // Config lives on the instance row as a JSON column, so the+ // instance delete takes it with it. No separate purge needed.$instance->delete();return redirect()->route('settings.adapters.index')diff --git a/app/Models/SyncAdapterConfig.php b/app/Models/SyncAdapterConfig.phpindex 8ff40ca8ab..ebd004a6b9 100644--- a/app/Models/SyncAdapterConfig.php+++ b/app/Models/SyncAdapterConfig.php@@ -2,56 +2,82 @@namespace App\Models;-use Illuminate\Database\Eloquent\Model;-/**- * Storage for per-instance sync-adapter configuration. Row-per-- * (sync_adapter_instance_id, config_key) so every adapter instance- * carries its own config bag and no future adapter has to touch the- * schema. Adapters that store secrets (Fleet API tokens, Jamf passwords,+ * Read/write helper for per-instance sync-adapter configuration.+ *+ * Storage is a single JSON blob on the `config` column of+ * SyncAdapterInstance (prior shape was a separate sync_adapter_settings+ * table with one row per key, folded into the parent instance row per+ * review feedback). Adapters call the static methods here to avoid+ * having to json_decode or track dirty state themselves.+ *+ * Value semantics carry over from the old row-per-key design:+ * - put(id, key, value) sets a value (including explicit null).+ * - get(id, key, default) returns the stored value or the default+ * when the key is missing entirely.+ * - has(id, key) is true only when the key exists AND its value is+ * not null.+ * - forget(id, key) removes the key from the blob entirely.+ *+ * Adapters that store secrets (Fleet API tokens, Jamf passwords,* Intune client secrets, etc.) are responsible for Crypt::encrypt-ing* the value before calling put(). This class treats every value as an- * opaque string.+ * opaque scalar and stores it verbatim inside the JSON blob.*/-class SyncAdapterConfig extends Model+class SyncAdapterConfig{- protected $table = 'sync_adapter_settings';-- protected $fillable = ['sync_adapter_instance_id', 'config_key', 'value'];-public static function get(int $instanceId, string $configKey, mixed $default = null): mixed{- $value = self::query()- ->where('sync_adapter_instance_id', $instanceId)- ->where('config_key', $configKey)- ->value('value');+ $instance = SyncAdapterInstance::find($instanceId);+ if ($instance === null) {+ return $default;+ }- return $value ?? $default;+ $config = $instance->config ?? [];++ return array_key_exists($configKey, $config) ? $config[$configKey] : $default;}public static function put(int $instanceId, string $configKey, ?string $value): void{- self::query()->updateOrCreate(- ['sync_adapter_instance_id' => $instanceId, 'config_key' => $configKey],- ['value' => $value],- );+ $instance = SyncAdapterInstance::find($instanceId);+ if ($instance === null) {+ return;+ }++ $config = $instance->config ?? [];+ $config[$configKey] = $value;+ $instance->config = $config;+ $instance->save();}public static function has(int $instanceId, string $configKey): bool{- return self::query()- ->where('sync_adapter_instance_id', $instanceId)- ->where('config_key', $configKey)- ->whereNotNull('value')- ->exists();+ $instance = SyncAdapterInstance::find($instanceId);+ if ($instance === null) {+ return false;+ }++ $config = $instance->config ?? [];++ return array_key_exists($configKey, $config) && $config[$configKey] !== null;}public static function forget(int $instanceId, string $configKey): void{- self::query()- ->where('sync_adapter_instance_id', $instanceId)- ->where('config_key', $configKey)- ->delete();+ $instance = SyncAdapterInstance::find($instanceId);+ if ($instance === null) {+ return;+ }++ $config = $instance->config ?? [];+ if (! array_key_exists($configKey, $config)) {+ return;+ }++ unset($config[$configKey]);+ $instance->config = $config;+ $instance->save();}/**@@ -59,13 +85,10 @@ class SyncAdapterConfig extends Model* by callers that need to enumerate a prefix range of keys* (e.g. `group_mapping.*`) rather than looking one up at a time.*- * @return array<string, ?string>+ * @return array<string, mixed>*/public static function listForInstance(int $instanceId): array{- return self::query()- ->where('sync_adapter_instance_id', $instanceId)- ->pluck('value', 'config_key')- ->all();+ return SyncAdapterInstance::find($instanceId)?->config ?? [];}}diff --git a/app/Models/SyncAdapterInstance.php b/app/Models/SyncAdapterInstance.phpindex ebcc8a56ab..0d3e4f4d03 100644--- a/app/Models/SyncAdapterInstance.php+++ b/app/Models/SyncAdapterInstance.php@@ -18,11 +18,14 @@ use Illuminate\Support\Str;*/class SyncAdapterInstance extends Model{- protected $fillable = ['adapter_type', 'label', 'slug', 'active', 'company_id'];+ protected $fillable = ['adapter_type', 'label', 'slug', 'active', 'company_id', 'config'];protected $casts = ['active' => 'boolean','last_synced_at' => 'datetime',+ // Per-instance settings blob. SyncAdapterConfig::get/put/forget+ // read and write individual keys against this cast array.+ 'config' => 'array',];/**diff --git a/app/SyncAdapters/MappingTargets.php b/app/SyncAdapters/MappingTargets.phpindex dcd7ab58aa..3764f4ee18 100644--- a/app/SyncAdapters/MappingTargets.php+++ b/app/SyncAdapters/MappingTargets.php@@ -13,7 +13,7 @@ use App\Models\CustomField;* custom field, or skip).** Target encoding is `<type>:<id>` so the value stays compact and- * self-describing when stored in sync_adapter_settings:+ * self-describing when stored in the instance config blob:* - skip -> don't write this field* - native:name -> asset.name* - native:serial -> asset.serialdiff --git a/database/migrations/2026_09_16_183827_create_sync_adapter_tables.php b/database/migrations/2026_09_16_183827_create_sync_adapter_tables.phpindex 8ef878a9c8..65c4a4ce9d 100644--- a/database/migrations/2026_09_16_183827_create_sync_adapter_tables.php+++ b/database/migrations/2026_09_16_183827_create_sync_adapter_tables.php@@ -46,27 +46,20 @@ return new class extends Migration$table->unique(['source', 'asset_id'], 'aes_source_asset_id_unique');});- // Per-instance key/value config for each adapter instance- // (URL, encrypted credentials, mapping.{field} targets,- // direction.{field}, group_mapping.{group_id}, cached- // vendor metadata, etc). Every config lookup + write goes- // through SyncAdapterConfig::get/put/forget, which enforces- // the (instance_id, config_key) uniqueness.- Schema::create('sync_adapter_settings', function (Blueprint $table) {- $table->id();- $table->unsignedBigInteger('sync_adapter_instance_id')->index();- $table->string('config_key', 191);- $table->text('value')->nullable();- $table->timestamps();- $table->unique(['sync_adapter_instance_id', 'config_key']);- });-// One row per configured adapter instance. Adapter TYPES live// in the codebase and are discovered at runtime, so nothing// gets pre-seeded here. Admins add instances via the settings// page. The slug is auto-generated from the label on create// and is immutable after (asset_external_sources.source// references it, reassigning slugs would orphan assets).+ //+ // config holds every per-instance setting (URL, encrypted+ // credentials, mapping.{field} targets, direction.{field},+ // group_mapping.{group_id}, cached vendor metadata, etc)+ // as a single JSON blob. The SyncAdapterConfig helper reads+ // and writes individual keys against this column so callers+ // don't have to json_decode themselves. Prior shape was a+ // separate sync_adapter_settings table with one row per key.Schema::create('sync_adapter_instances', function (Blueprint $table) {$table->id();$table->unsignedBigInteger('company_id')->nullable()->index();@@ -74,6 +67,7 @@ return new class extends Migration$table->string('adapter_type', 191)->index();$table->string('label', 191);$table->boolean('active')->default(true);+ $table->longText('config')->nullable();$table->timestamp('last_synced_at')->nullable()->index();$table->text('last_sync_result')->nullable();$table->unsignedBigInteger('created_by')->nullable()->index();@@ -84,7 +78,6 @@ return new class extends Migrationpublic function down(): void{Schema::dropIfExists('sync_adapter_instances');- Schema::dropIfExists('sync_adapter_settings');Schema::dropIfExists('asset_external_sources');}};diff --git a/resources/views/settings/adapters.blade.php b/resources/views/settings/adapters.blade.phpindex 7a10a256c9..e03d97bd9a 100644--- a/resources/views/settings/adapters.blade.php+++ b/resources/views/settings/adapters.blade.php@@ -153,7 +153,9 @@<div class="sync-adapters-empty-state">- <h3>{{ trans('admin/settings/sync_adapters.empty_state_title') }}</h3>+ <h3>+ <x-icon type="tip" class="text-info"/> {{ trans('admin/settings/sync_adapters.empty_state_title') }}+ </h3><p>{{ trans('admin/settings/sync_adapters.empty_state_intro') }}</p><p>{{ trans('admin/settings/sync_adapters.empty_state_supported_intro', ['count' => count($adapterCatalog)]) }}</p><ul class="list-unstyled adapter-catalog" aria-label="{{ trans('admin/settings/sync_adapters.catalog_caption') }}">diff --git a/tests/Feature/SyncAdapters/SyncAdapterDeleteTest.php b/tests/Feature/SyncAdapters/SyncAdapterDeleteTest.phpindex eeab701382..5d577cb06e 100644--- a/tests/Feature/SyncAdapters/SyncAdapterDeleteTest.php+++ b/tests/Feature/SyncAdapters/SyncAdapterDeleteTest.php@@ -80,7 +80,8 @@ class SyncAdapterDeleteTest extends TestCase// page with zero rows. The help tab holds the getting-started// copy + list of supported types + CTA button, and is auto-// selected so the initial view is not just a blank pane.- \App\Models\SyncAdapterConfig::query()->delete();+ // Config now lives on the instance row as a JSON column, so+ // wiping the instances table also wipes their configs.\App\Models\SyncAdapterInstance::query()->delete();$html = $this->actingAs(User::factory()->superuser()->create())@@ -205,29 +206,16 @@ class SyncAdapterDeleteTest extends TestCase$this->assertNotSame($source->id, $clone->id);$this->assertNotSame($source->slug, $clone->slug);- // Every config row from source shows up under the clone's id- // with the same key + value pair. Source rows are untouched.- $this->assertDatabaseHas('sync_adapter_settings', [- 'sync_adapter_instance_id' => $clone->id,- 'config_key' => 'url',- 'value' => 'https://fleet.example',- ]);- $this->assertDatabaseHas('sync_adapter_settings', [- 'sync_adapter_instance_id' => $clone->id,- 'config_key' => 'api_token',- 'value' => 'secret-token',- ]);- $this->assertDatabaseHas('sync_adapter_settings', [- 'sync_adapter_instance_id' => $clone->id,- 'config_key' => 'mapping.hostname',- 'value' => 'name',- ]);+ // Every config key from source shows up under the clone+ // with the same value. Storage is now a JSON blob on the+ // instance row, so we read through SyncAdapterConfig rather+ // than querying a settings table.+ $this->assertSame('https://fleet.example', SyncAdapterConfig::get($clone->id, 'url'));+ $this->assertSame('secret-token', SyncAdapterConfig::get($clone->id, 'api_token'));+ $this->assertSame('name', SyncAdapterConfig::get($clone->id, 'mapping.hostname'));// Source instance's config is untouched.- $this->assertDatabaseHas('sync_adapter_settings', [- 'sync_adapter_instance_id' => $source->id,- 'config_key' => 'url',- ]);+ $this->assertSame('https://fleet.example', SyncAdapterConfig::get($source->id, 'url'));}public function test_clone_rejects_duplicate_label()@@ -260,6 +248,5 @@ class SyncAdapterDeleteTest extends TestCase->assertSessionHas('success');$this->assertDatabaseMissing('sync_adapter_instances', ['id' => $instance->id]);- $this->assertDatabaseMissing('sync_adapter_settings', ['sync_adapter_instance_id' => $instance->id]);}}