A free open source IT asset/license management system
Added models
snipe206e15da2307
diff --git a/app/Models/AssetExternalSource.php b/app/Models/AssetExternalSource.phpnew file mode 100644index 0000000000..327233137c--- /dev/null+++ b/app/Models/AssetExternalSource.php@@ -0,0 +1,42 @@+<?php++namespace App\Models;++use Illuminate\Database\Eloquent\Model;+use Illuminate\Database\Eloquent\Relations\BelongsTo;++/**+ * Sync-adapter side row for an asset. Combines the identity mapping+ * (which adapter sourced this asset via source + external_id) with+ * the last-known network / OS inventory the vendor reported+ * (primary_mac / primary_ip / os / os_version / last_seen).+ *+ * One row per asset today. SyncHostFromAdapter upserts on the+ * (source, external_id) unique index and updates the inventory+ * columns on every sync run.+ */+class AssetExternalSource extends Model+{+ protected $table = 'asset_external_sources';++ protected $fillable = [+ 'company_id',+ 'asset_id',+ 'source',+ 'external_id',+ 'primary_mac',+ 'primary_ip',+ 'os',+ 'os_version',+ 'last_seen',+ ];++ protected $casts = [+ 'last_seen' => 'datetime',+ ];++ public function asset(): BelongsTo+ {+ return $this->belongsTo(Asset::class);+ }+}diff --git a/app/Models/SyncAdapterConfig.php b/app/Models/SyncAdapterConfig.phpnew file mode 100644index 0000000000..8ff40ca8ab--- /dev/null+++ b/app/Models/SyncAdapterConfig.php@@ -0,0 +1,71 @@+<?php++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,+ * Intune client secrets, etc.) are responsible for Crypt::encrypt-ing+ * the value before calling put(). This class treats every value as an+ * opaque string.+ */+class SyncAdapterConfig extends Model+{+ 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');++ return $value ?? $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],+ );+ }++ 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();+ }++ public static function forget(int $instanceId, string $configKey): void+ {+ self::query()+ ->where('sync_adapter_instance_id', $instanceId)+ ->where('config_key', $configKey)+ ->delete();+ }++ /**+ * All (config_key => value) pairs stored for this instance. Used+ * 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>+ */+ public static function listForInstance(int $instanceId): array+ {+ return self::query()+ ->where('sync_adapter_instance_id', $instanceId)+ ->pluck('value', 'config_key')+ ->all();+ }+}diff --git a/app/Models/SyncAdapterInstance.php b/app/Models/SyncAdapterInstance.phpnew file mode 100644index 0000000000..6a6db0668f--- /dev/null+++ b/app/Models/SyncAdapterInstance.php@@ -0,0 +1,78 @@+<?php++namespace App\Models;++use App\SyncAdapters\AdapterRegistry;+use App\SyncAdapters\HostInventoryAdapter;+use Illuminate\Database\Eloquent\Model;+use Illuminate\Support\Str;++/**+ * One configured instance of a sync adapter. Multiple instances of the+ * same adapter_type are allowed (e.g., two Fleet installs, one Fleet + one+ * Kandji), each with independent credentials, own tab on the settings+ * page, own row in asset_external_sources where source = the instance slug.+ *+ * Slugs auto-generate from the label at create time and are immutable+ * after, because asset_external_sources.source references them and reassigning+ * slugs later would orphan assets.+ */+class SyncAdapterInstance extends Model+{+ protected $fillable = ['adapter_type', 'label', 'slug', 'active', 'built_in', 'company_id'];++ protected $casts = [+ 'active' => 'boolean',+ 'built_in' => 'boolean',+ 'last_synced_at' => 'datetime',+ ];++ /**+ * Bind route params to slug instead of numeric id so URLs are+ * self-documenting (/admin/adapters/fleet-prod/sync).+ */+ public function getRouteKeyName(): string+ {+ return 'slug';+ }++ protected static function booted(): void+ {+ // Auto-generate slug from label with a uniqueness suffix so+ // "Production Fleet" and "Prod Fleet" don't collide, and+ // "Fleet" + "Fleet" produces fleet + fleet-2.+ static::creating(function (self $instance) {+ if (empty($instance->slug)) {+ $instance->slug = self::generateUniqueSlug($instance->label);+ }+ });+ }++ /**+ * Resolve this instance's adapter_type to the shipped adapter class+ * and return a hydrated, instance-aware adapter. Returns null if the+ * type is not registered (which shouldn't normally happen but can if+ * an adapter class is removed while instances still reference it).+ */+ public function adapter(): ?HostInventoryAdapter+ {+ return AdapterRegistry::hydrate($this);+ }++ private static function generateUniqueSlug(string $label): string+ {+ $base = Str::slug($label);+ if ($base === '') {+ $base = 'adapter';+ }++ $candidate = $base;+ $suffix = 2;+ while (self::query()->where('slug', $candidate)->exists()) {+ $candidate = $base.'-'.$suffix;+ $suffix++;+ }++ return $candidate;+ }+}