Skip to content

A free open source IT asset/license management system

Adds externalSource method to asset to connect the asset_external_sources to assets

snipebdd943400ba3
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index eb98907628..2ef107007f 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -1205,6 +1205,18 @@ class Asset extends Depreciable
return $this->hasMany(LicenseSeat::class, 'asset_id');
}
+ /**
+ * Sync-adapter side row: identity (source + external_id) plus
+ * last-known network / OS inventory (primary MAC / IP / OS /
+ * OS version / last seen). Nullable relation: only assets that
+ * have been synced from an adapter have a row. Detail view +
+ * assets table render these fields when the relation is present.
+ */
+ public function externalSource()
+ {
+ return $this->hasOne(AssetExternalSource::class);
+ }
+
/**
* Establishes the asset -> aupplier relationship
*
@@ -2187,6 +2199,32 @@ class Asset extends Depreciable
return $query->leftJoin('companies as company_sort', 'assets.company_id', '=', 'company_sort.id')->orderBy('company_sort.name', $order);
}
+ /**
+ * Sort by a sync-adapter external-source column (primary_mac,
+ * primary_ip, os, os_version, last_seen). LeftJoin so unsynced
+ * assets sort as nulls rather than dropping out. Join is on the
+ * unique asset_id index in asset_external_sources, so cost is
+ * an index lookup per row.
+ *
+ * Column argument is whitelisted by the caller (AssetsController's
+ * sort switch) so it's never user-controlled at this layer, but
+ * we still validate against the known column set as defense-in-
+ * depth against a caller regression.
+ */
+ public function scopeOrderExternalSource($query, string $order, string $column)
+ {
+ if (! in_array($column, ['primary_mac', 'primary_ip', 'os', 'os_version', 'last_seen'], true)) {
+ return $query;
+ }
+
+ return $query->leftJoin(
+ 'asset_external_sources as ext_src_sort',
+ 'assets.id',
+ '=',
+ 'ext_src_sort.asset_id',
+ )->orderBy('ext_src_sort.'.$column, $order);
+ }
+
/**
* Query builder scope to return results of a category
*