Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
fix discovered devices unicity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Apr 15, 2024
1 parent b0a03da commit 1f6f4da
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions inc/computersync.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,14 +540,15 @@ public static function discover(): bool
$imported[] = $data['jamf_items_id'];
}
$pending_iterator = $DB->request([
'SELECT' => ['jamf_items_id'],
'FROM' => 'glpi_plugin_jamf_imports',
'WHERE' => [
'jamf_type' => 'Computer'
]
]);
$pending_import = [];
foreach ($pending_iterator as $data) {
$pending_import[$data['jamf_items_id']] = $data;
$pending_import[] = $data['jamf_items_id'];
}

$config = Config::getConfigurationValues('plugin:Jamf');
Expand All @@ -564,7 +565,7 @@ public static function discover(): bool
// Some other error
}
} else {
if (!array_key_exists($jamf_device['id'], $pending_import)) {
if (!in_array((int) $jamf_device['id'], $pending_import, true)) {
// Just discovered and cannot auto-import. Save to imports table instead.
$DB->insert('glpi_plugin_jamf_imports', [
'jamf_type' => 'Computer',
Expand Down
8 changes: 8 additions & 0 deletions inc/migration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,4 +605,12 @@ public function apply_3_0_1_migration(): void
// Change udid column in glpi_plugin_jamf_imports to allow NULL values
$this->db->queryOrDie('ALTER TABLE `glpi_plugin_jamf_imports` MODIFY `udid` VARCHAR(100) NULL DEFAULT NULL');
}

public function apply_3_1_1_migration(): void
{
// Fix unicity constraint on imports table
$this->glpiMigration->dropKey('glpi_plugin_jamf_imports', 'unicity');
// Unicity should be on jamf_type (The type in Jamf: Computer/MobileDevice) and jamf_items_id, not jamf_items_id and type (The type of asset in GLPI)
$this->glpiMigration->addKey('glpi_plugin_jamf_imports', ['jamf_type', 'jamf_items_id'], 'unicity', 'UNIQUE');
}
}
7 changes: 4 additions & 3 deletions inc/mobilesync.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,19 +659,20 @@ public static function discover(): bool
$imported[] = $data['udid'];
}
$pending_iterator = $DB->request([
'SELECT' => ['jamf_items_id'],
'FROM' => 'glpi_plugin_jamf_imports',
'WHERE' => [
'jamf_type' => 'MobileDevice'
]
]);
$pending_import = [];
foreach ($pending_iterator as $data) {
$pending_import[$data['udid']] = $data;
$pending_import[] = $data['jamf_items_id'];
}

$config = Config::getConfigurationValues('plugin:Jamf');
foreach ($jamf_devices as $jamf_device) {
if (!in_array($jamf_device['udid'], $imported, true)) {
if (!in_array($jamf_device['id'], $imported, true)) {
$itemtype = strpos($jamf_device['model_identifier'], 'iPhone') !== false ? 'Phone' : 'Computer';
if (isset($config['autoimport']) && $config['autoimport']) {
try {
Expand All @@ -683,7 +684,7 @@ public static function discover(): bool
// Some other error
}
} else {
if (!array_key_exists($jamf_device['udid'], $pending_import)) {
if (!in_array((int) $jamf_device['id'], $pending_import, true)) {
$DB->insert('glpi_plugin_jamf_imports', [
'jamf_type' => 'MobileDevice',
'jamf_items_id' => $jamf_device['id'],
Expand Down

0 comments on commit 1f6f4da

Please sign in to comment.