A free open source IT asset/license management system
Fixed #19877 - split AxM mac addresses for ones with more than one in AxM api response
snipe8b5d2b9d1261
diff --git a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.phpindex 32006bdd07..565d6e75f8 100644--- a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php+++ b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php@@ -244,7 +244,12 @@ class AppleBusinessManagerAdapter extends SyncAdapter'abm_device_capacity' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_device_capacity'],'abm_wifi_mac' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_wifi_mac'],'abm_bluetooth_mac' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_bluetooth_mac'],- 'abm_ethernet_mac' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_ethernet_mac'],+ // Desktop Macs with two Ethernet NICs (Mac Pro, iMac Pro)+ // return ethernetMacAddress as a two-element array. Split+ // positionally so admins can map each physical port to its+ // own Snipe-IT custom field. #19877.+ 'abm_ethernet_mac_1' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_ethernet_mac_1'],+ 'abm_ethernet_mac_2' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_ethernet_mac_2'],// Apple returns IMEIs as an array (dual-SIM iPhones carry// two, older devices one). Positional split so admins can// map each to its own custom field. #19754.@@ -782,7 +787,10 @@ class AppleBusinessManagerAdapter extends SyncAdapterreturn $wifi;}- $ethernet = self::normalizeMacAddress(Arr::get($attrs, 'ethernetMacAddress'));+ // ethernetMacAddress can be a two-element array on Macs with two+ // physical ports. Prefer the first slot so the primaryMac field+ // on a non-wifi Mac still populates something sensible.+ $ethernet = self::extractMacAtIndex(Arr::get($attrs, 'ethernetMacAddress'), 0);if ($ethernet !== null) {return $ethernet;}@@ -829,18 +837,30 @@ class AppleBusinessManagerAdapter extends SyncAdapter$extra['abm_mdm_storage_total'] = Arr::get($attrs, 'storageTotalCapacity');$extra['abm_mdm_storage_free'] = Arr::get($attrs, 'storageFreeCapacity');- $macMap = [+ // Wi-Fi and Bluetooth are single-radio on every current Apple+ // SKU so they stay scalar. Ethernet can be a two-element array+ // on dual-NIC Macs, so split positionally like IMEI.+ $singleMacMap = ['wifiMacAddress' => 'abm_wifi_mac','bluetoothMacAddress' => 'abm_bluetooth_mac',- 'ethernetMacAddress' => 'abm_ethernet_mac',];- foreach ($macMap as $mdmKey => $extraKey) {+ foreach ($singleMacMap as $mdmKey => $extraKey) {$val = self::normalizeMacAddress(Arr::get($attrs, $mdmKey));if ($val !== null) {$extra[$extraKey] = $val;}}+ $mdmEthernet = Arr::get($attrs, 'ethernetMacAddress');+ $ethernet1 = self::extractMacAtIndex($mdmEthernet, 0);+ if ($ethernet1 !== null) {+ $extra['abm_ethernet_mac_1'] = $ethernet1;+ }+ $ethernet2 = self::extractMacAtIndex($mdmEthernet, 1);+ if ($ethernet2 !== null) {+ $extra['abm_ethernet_mac_2'] = $ethernet2;+ }+// IMEI is an array in both orgDevices and MDM-detail shapes.// Overwrite each positional slot only when MDM has a value so// the orgDevices value stays as the fallback.@@ -895,6 +915,27 @@ class AppleBusinessManagerAdapter extends SyncAdapter* Returns null when the slot is absent, when the value isn't a* non-empty string, or when a scalar is requested for slot 1.*/+ /**+ * Pick a single MAC out of Apple's `ethernetMacAddress` field,+ * which is a two-element array on Macs with two physical NICs and+ * a bare string on everything else. Shape semantics mirror+ * extractImeiAtIndex: scalar fills slot 0 only, array indexes+ * positionally, anything else returns null. The returned value+ * passes through normalizeMacAddress so the colon-separated upper-+ * case shape custom-field validators expect round-trips cleanly.+ */+ private static function extractMacAtIndex(mixed $value, int $index): ?string+ {+ if (is_string($value)) {+ return $index === 0 ? self::normalizeMacAddress($value) : null;+ }+ if (! is_array($value)) {+ return null;+ }++ return self::normalizeMacAddress($value[$index] ?? null);+ }+private static function extractImeiAtIndex(mixed $value, int $index): ?string{if (is_string($value)) {@@ -1076,10 +1117,14 @@ class AppleBusinessManagerAdapter extends SyncAdapter'abm_device_capacity' => Arr::get($attrs, 'deviceCapacity'),'abm_wifi_mac' => self::normalizeMacAddress(Arr::get($attrs, 'wifiMacAddress')),'abm_bluetooth_mac' => self::normalizeMacAddress(Arr::get($attrs, 'bluetoothMacAddress')),- 'abm_ethernet_mac' => self::normalizeMacAddress(Arr::get($attrs, 'ethernetMacAddress')),+ // ethernetMacAddress is a two-element array on dual-NIC+ // Macs. Positional split so admins can map each port to+ // its own custom field. See #19877.+ 'abm_ethernet_mac_1' => self::extractMacAtIndex(Arr::get($attrs, 'ethernetMacAddress'), 0),+ 'abm_ethernet_mac_2' => self::extractMacAtIndex(Arr::get($attrs, 'ethernetMacAddress'), 1),// Cellular-only identifiers. Populated for iPhones and// cellular iPads, null on Macs / non-cellular devices.- // IMEI is an array in Apple's shape; _1/_2 split by+ // IMEI is an array in Apple's shape, _1/_2 split by// index so admins can map each to its own custom field.'abm_imei_1' => self::extractImeiAtIndex(Arr::get($attrs, 'imei'), 0),'abm_imei_2' => self::extractImeiAtIndex(Arr::get($attrs, 'imei'), 1),diff --git a/resources/lang/en-US/admin/settings/sync_adapters.php b/resources/lang/en-US/admin/settings/sync_adapters.phpindex 773e55e58b..49c028de4d 100644--- a/resources/lang/en-US/admin/settings/sync_adapters.php+++ b/resources/lang/en-US/admin/settings/sync_adapters.php@@ -573,7 +573,8 @@ return ['abm_extra_device_capacity' => 'AxM Storage Capacity','abm_extra_wifi_mac' => 'AxM Wi-Fi MAC','abm_extra_bluetooth_mac' => 'AxM Bluetooth MAC',- 'abm_extra_ethernet_mac' => 'AxM Ethernet MAC',+ 'abm_extra_ethernet_mac_1' => 'AxM Ethernet MAC 1',+ 'abm_extra_ethernet_mac_2' => 'AxM Ethernet MAC 2','abm_extra_imei_1' => 'AxM IMEI 1','abm_extra_imei_2' => 'AxM IMEI 2','abm_extra_meid' => 'AxM MEID',diff --git a/tests/Feature/SyncAdapters/AppleBusinessManager/ActivationLockEnrichmentTest.php b/tests/Feature/SyncAdapters/AppleBusinessManager/ActivationLockEnrichmentTest.phpindex a100532719..885c8b7c20 100644--- a/tests/Feature/SyncAdapters/AppleBusinessManager/ActivationLockEnrichmentTest.php+++ b/tests/Feature/SyncAdapters/AppleBusinessManager/ActivationLockEnrichmentTest.php@@ -141,7 +141,8 @@ class ActivationLockEnrichmentTest extends TestCase$this->assertSame('256GB', $record->extra['abm_device_capacity']);$this->assertSame('AA:BB:CC:00:11:22', $record->extra['abm_wifi_mac']);$this->assertSame('AA:BB:CC:00:11:23', $record->extra['abm_bluetooth_mac']);- $this->assertNull($record->extra['abm_ethernet_mac']);+ $this->assertNull($record->extra['abm_ethernet_mac_1']);+ $this->assertNull($record->extra['abm_ethernet_mac_2']);$this->assertSame('351234567890123', $record->extra['abm_imei_1']);$this->assertNull($record->extra['abm_imei_2']);$this->assertSame('35123456789012', $record->extra['abm_meid']);diff --git a/tests/Feature/SyncAdapters/AppleBusinessManager/MacAndImeiNormalizationTest.php b/tests/Feature/SyncAdapters/AppleBusinessManager/MacAndImeiNormalizationTest.phpindex f6199819c0..cd3bef107e 100644--- a/tests/Feature/SyncAdapters/AppleBusinessManager/MacAndImeiNormalizationTest.php+++ b/tests/Feature/SyncAdapters/AppleBusinessManager/MacAndImeiNormalizationTest.php@@ -60,7 +60,8 @@ class MacAndImeiNormalizationTest extends TestCase$this->assertSame('01:23:45:67:89:AB', $record->primaryMac);$this->assertSame('01:23:45:67:89:AB', $record->extra['abm_wifi_mac']);$this->assertSame('AA:BB:CC:DD:EE:FF', $record->extra['abm_bluetooth_mac']);- $this->assertSame('11:22:33:44:55:66', $record->extra['abm_ethernet_mac']);+ $this->assertSame('11:22:33:44:55:66', $record->extra['abm_ethernet_mac_1']);+ $this->assertNull($record->extra['abm_ethernet_mac_2']);}public function test_already_colon_separated_mac_round_trips_unchanged()@@ -195,6 +196,65 @@ class MacAndImeiNormalizationTest extends TestCase$this->assertNull($record->extra['abm_imei_2']);}+ public function test_dual_nic_mac_populates_both_ethernet_mac_slots()+ {+ // Regression for #19877. Mac Pro, iMac Pro, and other dual-+ // Ethernet Macs return ethernetMacAddress as a two-element+ // array. Before the fix normalizeMacAddress saw the array,+ // failed its is_string gate, and the extra silently dropped+ // to null.+ $adapter = $this->configuredAdapter();++ Http::fake([+ 'account.apple.com/*' => Http::response(['access_token' => 'stub-bearer']),+ 'api-business.apple.com/v1/orgDevices*' => Http::response([+ 'data' => [[+ 'id' => 'abm-dual-nic-mac',+ 'attributes' => [+ 'serialNumber' => 'SN-MAC-PRO',+ 'partNumber' => 'Z175LL/A',+ 'productFamily' => 'Mac',+ 'ethernetMacAddress' => ['008A76F01E74', '008A76E815A2'],+ ],+ ]],+ ]),+ ]);++ $record = iterator_to_array($adapter->pull())[0];++ $this->assertSame('00:8A:76:F0:1E:74', $record->extra['abm_ethernet_mac_1']);+ $this->assertSame('00:8A:76:E8:15:A2', $record->extra['abm_ethernet_mac_2']);+ }++ public function test_single_ethernet_mac_scalar_populates_only_slot_one()+ {+ // iMacs and MacBook Pros with a single NIC (or an Apple USB-C+ // Ethernet adapter) still return a scalar. That path keeps+ // working after the fix via the extractMacAtIndex scalar+ // branch.+ $adapter = $this->configuredAdapter();++ Http::fake([+ 'account.apple.com/*' => Http::response(['access_token' => 'stub-bearer']),+ 'api-business.apple.com/v1/orgDevices*' => Http::response([+ 'data' => [[+ 'id' => 'abm-single-nic-mac',+ 'attributes' => [+ 'serialNumber' => 'SN-IMAC',+ 'partNumber' => 'MKFH3LL/A',+ 'productFamily' => 'Mac',+ 'ethernetMacAddress' => '008A76F01E74',+ ],+ ]],+ ]),+ ]);++ $record = iterator_to_array($adapter->pull())[0];++ $this->assertSame('00:8A:76:F0:1E:74', $record->extra['abm_ethernet_mac_1']);+ $this->assertNull($record->extra['abm_ethernet_mac_2']);+ }+public function test_non_cellular_device_leaves_both_imei_slots_null(){// Macs and non-cellular iPads don't carry an IMEI at all.