forked from cconard96/jamf
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmigration.class.php
More file actions
644 lines (584 loc) · 28 KB
/
Copy pathmigration.class.php
File metadata and controls
644 lines (584 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<?php
/**
* -------------------------------------------------------------------------
* JAMF plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of JAMF plugin for GLPI.
*
* JAMF plugin for GLPI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* JAMF plugin for GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JAMF plugin for GLPI. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2024-2025 by Teclib'
* @copyright Copyright (C) 2019-2024 by Curtis Conard
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/jamf
* -------------------------------------------------------------------------
*/
use function Safe\preg_match;
use function Safe\Copy;
use function Safe\mkdir;
/**
* Handles migrating between plugin versions.
*
* @since 2.1.0
*/
final class PluginJamfMigration
{
private const BASE_VERSION = '1.0.0';
/**
* @var Migration
*/
private $glpiMigration;
/**
* @var DBmysql
*/
private $db;
/** @var 'PluginJamfAPI' */
private $api = 'PluginJamfAPI';
/**
* PluginJamfMigration constructor.
* @param string $version
* @global DBmysql $DB
*/
public function __construct($version)
{
/** @var DBmysql $DB */
global $DB;
$this->glpiMigration = new Migration($version);
$this->db = $DB;
}
public function applyMigrations()
{
$rc = new ReflectionClass($this);
$otherMigrationFunctions = array_map(
static fn($rm) => $rm->getShortName(),
array_filter(
$rc->getMethods(),
static fn($m): bool => preg_match('/(?<=^apply_)(.*)(?=_migration$)/', $m->getShortName()) === 1,
),
);
if ($otherMigrationFunctions !== []) {
// Map versions to functions
$versionMap = [];
foreach ($otherMigrationFunctions as $function) {
$ver = str_replace(['apply_', '_migration', '_'], ['', '', '.'], $function);
$versionMap[$ver] = $function;
}
// Sort semantically
uksort($versionMap, version_compare(...));
// Get last known recorded version. If none exists, assume this is 1.0.0 since versions weren't recorded until 2.0.0.
// Migrations should be replayable so nothing should be lost on multiple runs.
$lastKnownVersion = Config::getConfigurationValues('plugin:Jamf')['plugin_version'] ?? self::BASE_VERSION;
// Call each migration in order starting from the last known version
foreach ($versionMap as $version => $func) {
// Last known version is the same or greater than release version
if (version_compare($lastKnownVersion, $version, '<=')) {
$this->$func();
$this->glpiMigration->executeMigration();
if ($version !== self::BASE_VERSION) {
$this->setPluginVersionInDB($version);
$lastKnownVersion = $version;
}
}
}
}
}
public function uninstall()
{
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_imports');
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_mobiledevices');
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_mobiledevicesoftwares');
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_softwares');
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_extensionattributes');
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_items_extensionattributes');
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_extfields');
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_users_jssaccounts');
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_computers');
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_computersoftwares');
PluginJamfDBUtil::dropTableOrDie('glpi_plugin_jamf_devices');
Config::deleteConfigurationValues('plugin:Jamf', [
'config_class',
'jssserver', 'jssuser', 'jsspassword', 'jssignorecert',
'sync_interval', 'sync_general', 'sync_os',
'sync_software', 'sync_financial', 'sync_user', 'sync_components',
'user_sync_mode', 'autoimport', 'default_status',
'default_manufacturer',
'iphone_type',
'ipad_type',
'appletv_type',
'computer_type',
'plugin_version',
]);
CronTask::unregister('Jamf');
if (is_dir(GLPI_PLUGIN_DOC_DIR . "/jamf/")) {
Toolbox::deleteDir(GLPI_PLUGIN_DOC_DIR . "/jamf/");
}
}
private function setPluginVersionInDB($version)
{
$this->db->updateOrInsert(Config::getTable(), [
'value' => $version,
'context' => 'plugin:Jamf',
'name' => 'plugin_version',
], [
'context' => 'plugin:Jamf',
'name' => 'plugin_version',
]);
}
/**
* Apply the migrations for the base plugin version (1.0.0).
*
* @since 2.1.0
*/
public function apply_1_0_0_migration(): void
{
// Check imports table (Used to store newly discovered devices that haven't been imported yet)
if (!$this->db->tableExists('glpi_plugin_jamf_imports')) {
$query = 'CREATE TABLE `glpi_plugin_jamf_imports` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`jamf_items_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`type` varchar(100) NOT NULL,
`udid` varchar(100) NOT NULL,
`date_discover` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`jamf_items_id`,`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
$this->db->doQuery($query);
}
// Check mobile devices table (Extra data for mobile devices)
if (!$this->db->tableExists('glpi_plugin_jamf_mobiledevices')) {
$query = "CREATE TABLE `glpi_plugin_jamf_mobiledevices` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`items_id` int unsigned NOT NULL DEFAULT '0',
`itemtype` varchar(100) NOT NULL,
`udid` varchar(100) NOT NULL,
`last_inventory` datetime NULL,
`entry_date` datetime NULL,
`enroll_date` datetime NULL,
`import_date` datetime NULL,
`sync_date` datetime NULL,
`managed` tinyint(1) NOT NULL DEFAULT '0',
`supervised` tinyint(1) NOT NULL DEFAULT '0',
`shared` varchar(100) NOT NULL DEFAULT '',
`cloud_backup_enabled` tinyint(1) DEFAULT '0',
`activation_lock_enabled` tinyint(1) DEFAULT '0',
`lost_mode_enabled` varchar(255) DEFAULT 'Unknown',
`lost_mode_enforced` tinyint(1) DEFAULT '0',
`lost_mode_enable_date` datetime NULL,
`lost_mode_message` varchar(255) DEFAULT NULL,
`lost_mode_phone` varchar(100) DEFAULT NULL,
`lost_location_latitude` varchar(100) DEFAULT '',
`lost_location_longitude` varchar(100) DEFAULT '',
`lost_location_altitude` varchar(100) DEFAULT '',
`lost_location_speed` varchar(100) DEFAULT '',
`lost_location_date` datetime NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`itemtype`, `items_id`),
KEY `udid` (`udid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$this->db->doQuery($query);
}
// Check software table (Extra data for software). Also check the later name just to avoid useless SQL actions.
if (!$this->db->tableExists('glpi_plugin_jamf_softwares') && !$this->db->tableExists('glpi_plugin_jamf_mobiledevicesoftwares')) {
$query = 'CREATE TABLE `glpi_plugin_jamf_softwares` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`softwares_id` int(11) NOT NULL,
`bundle_id` varchar(255) NOT NULL,
`itunes_store_url` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
$this->db->doQuery($query);
}
$jamfconfig = Config::getConfigurationValues('plugin:Jamf');
if (!count($jamfconfig)) {
$this->glpiMigration->addConfig([
'jssserver' => '',
'jssuser' => '',
'jsspassword' => '',
'sync_interval' => '0',
'sync_general' => '0',
'sync_os' => '0',
'sync_software' => '0',
'sync_financial' => '0',
'sync_user' => '0',
'user_sync_mode' => 'email',
'autoimport' => '0',
], 'plugin:Jamf');
}
// Not originally a part of version 1.0.0 but useful for the migration system added in 2.0.0
if (!isset($jamfconfig['plugin_version'])) {
$this->glpiMigration->addConfig([
'plugin_version' => '1.0.0',
], 'plugin:Jamf');
}
// CronTask already makes sure we don't register duplicates
CronTask::register('PluginJamfSync', 'syncJamf', 300, [
'state' => 1,
'allowmode' => 2,
'logslifetime' => 30,
'comment' => 'Sync devices with Jamf that are already imported',
]);
CronTask::register('PluginJamfSync', 'importJamf', 900, [
'state' => 1,
'allowmode' => 2,
'logslifetime' => 30,
'comment' => 'Import or discover devices in Jamf that are not already imported',
]);
}
private function apply_1_1_0_migration()
{
// Check extension attribute tables
if (!$this->db->tableExists('glpi_plugin_jamf_extensionattributes')) {
$query = 'CREATE TABLE `glpi_plugin_jamf_extensionattributes` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`itemtype` varchar(100) NOT NULL,
`jamf_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`data_type` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
UNIQUE KEY `jamf_id` (`jamf_id`, `itemtype`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
$this->db->doQuery($query);
}
if (!$this->db->tableExists('glpi_plugin_jamf_items_extensionattributes')) {
$query = 'CREATE TABLE `glpi_plugin_jamf_items_extensionattributes` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`itemtype` varchar(100) NOT NULL,
`items_id` int(11) NOT NULL,
`glpi_plugin_jamf_extensionattributes_id` int(11) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `item` (`itemtype`, `items_id`),
UNIQUE `unicity` (`itemtype`, `items_id`, `glpi_plugin_jamf_extensionattributes_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
$this->db->doQuery($query);
}
if (!$this->db->tableExists('glpi_plugin_jamf_extfields')) {
$query = "CREATE TABLE `glpi_plugin_jamf_extfields` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`itemtype` varchar(100) NOT NULL,
`items_id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`value` varchar(255) DEFAULT '',
PRIMARY KEY (`id`),
KEY `item` (`itemtype`, `items_id`),
UNIQUE `unicity` (`itemtype`, `items_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$this->db->doQuery($query);
}
if (!$this->db->tableExists('glpi_plugin_jamf_users_jssaccounts')) {
$query = 'CREATE TABLE `glpi_plugin_jamf_users_jssaccounts` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`users_id` int(11) NOT NULL,
`jssaccounts_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
$this->db->doQuery($query);
}
$this->glpiMigration->addConfig([
'itemtype_iphone' => 'Phone',
'itemtype_ipad' => 'Computer',
'itemtype_appletv' => 'Computer',
], 'plugin:Jamf');
// Fix missing Jamf ID field
if (!$this->db->fieldExists('glpi_plugin_jamf_mobiledevices', 'jamf_items_id', false)) {
$this->glpiMigration->addField('glpi_plugin_jamf_mobiledevices', 'jamf_items_id', 'integer', ['value' => -1]);
$this->glpiMigration->migrationOneTable('glpi_plugin_jamf_mobiledevices');
$mobiledevice = new PluginJamfMobileDevice();
// Find all devices that don't have the jamf id recorded, and retrieve it.
$unassigned = $mobiledevice->find(['jamf_items_id' => -1]);
foreach ($unassigned as $item) {
$jamf_item = $this->api::getMobileDeviceByUDID($item['udid']);
if ($jamf_item !== null && count($jamf_item) === 1) {
$mobiledevice->update([
'id' => $item['id'],
'jamf_items_id' => $jamf_item['mobileDeviceId'],
]);
}
}
}
}
private function apply_1_1_1_migration()
{
$this->glpiMigration->addRight(PluginJamfMobileDevice::$rightname, ALLSTANDARDRIGHT);
$this->glpiMigration->addRight(PluginJamfRuleImport::$rightname, ALLSTANDARDRIGHT);
$this->glpiMigration->addRight(PluginJamfUser_JSSAccount::$rightname, ALLSTANDARDRIGHT);
$this->glpiMigration->addRight(PluginJamfItem_MDMCommand::$rightname, ALLSTANDARDRIGHT);
}
private function apply_1_1_2_migration()
{
$this->db->update('glpi_crontasks', [
'allowmode' => 3,
], [
'itemtype' => 'PluginJamfSync',
]);
}
private function apply_2_0_0_migration()
{
$config = Config::getConfigurationValues('plugin:Jamf');
$coreConfig = Config::getConfigurationValues('core');
// Drop configs for selecting itemtypes. This is now statically enforced since improvements to the GLPI phone type.
if (isset($coreConfig['itemtype_iphone'])) {
$this->db->delete(Config::getTable(), [
'context' => 'core',
'name' => ['itemtype_iphone', 'itemtype_ipad', 'itemtype_appletv'],
]);
}
if (isset($config['itemtype_iphone'])) {
$this->db->delete(Config::getTable(), [
'context' => 'plugin:Jamf',
'name' => ['itemtype_iphone', 'itemtype_ipad', 'itemtype_appletv'],
]);
}
// Add default status config option
if (!isset($config['default_status'])) {
$this->glpiMigration->addConfig(['default_status', null], 'plugin:Jamf');
}
if (!isset($config['sync_components'])) {
$this->glpiMigration->addConfig(['sync_components', 0], 'plugin:Jamf');
}
if (!isset($config['jssignorecert'])) {
$this->glpiMigration->addConfig(['jssignorecert', 0], 'plugin:Jamf');
}
}
public function apply_2_1_0_migration()
{
if (!$this->db->fieldExists('glpi_plugin_jamf_extensionattributes', 'jamf_type', false)) {
$this->glpiMigration->addField('glpi_plugin_jamf_extensionattributes', 'jamf_type', 'string', [
'value' => 'MobileDevice',
'after' => 'id',
]);
$this->glpiMigration->dropKey('glpi_plugin_jamf_extensionattributes', 'jamf_id');
if ($this->db->fieldExists('glpi_plugin_jamf_extensionattributes', 'itemtype')) {
$this->glpiMigration->dropField('glpi_plugin_jamf_extensionattributes', 'itemtype');
}
$this->glpiMigration->addKey('glpi_plugin_jamf_extensionattributes', ['jamf_type', 'jamf_id'], 'unicity', 'UNIQUE');
$this->glpiMigration->migrationOneTable('glpi_plugin_jamf_extensionattributes');
}
if (!$this->db->fieldExists('glpi_plugin_jamf_imports', 'jamf_type', false)) {
$this->glpiMigration->addField('glpi_plugin_jamf_imports', 'jamf_type', 'string', [
'value' => 'MobileDevice',
'after' => 'id',
]);
$this->glpiMigration->migrationOneTable('glpi_plugin_jamf_imports');
}
if (!$this->db->tableExists('glpi_plugin_jamf_devices')) {
$query = "CREATE TABLE `glpi_plugin_jamf_devices` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`jamf_type` varchar(100) DEFAULT 'MobileDevice',
`jamf_items_id` int(11) NOT NULL DEFAULT -1,
`items_id` int(11) NOT NULL,
`itemtype` varchar(100) NOT NULL,
`udid` varchar(100) NOT NULL,
`last_inventory` datetime NULL,
`entry_date` datetime NULL,
`enroll_date` datetime NULL,
`import_date` datetime NULL,
`sync_date` datetime NULL,
`managed` tinyint(1) NOT NULL DEFAULT '0',
`supervised` tinyint(1) NOT NULL DEFAULT '0',
`activation_lock_enabled` tinyint(1) DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`itemtype`, `items_id`),
KEY `udid` (`udid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$this->db->doQuery($query);
$common_fields = ['jamf_items_id', 'items_id', 'itemtype', 'udid', 'last_inventory', 'entry_date', 'enroll_date', 'import_date', 'sync_date', 'managed', 'supervised', 'activation_lock_enabled'];
$all_mobiledevices = getAllDataFromTable('glpi_plugin_jamf_mobiledevices');
$this->glpiMigration->addField('glpi_plugin_jamf_mobiledevices', 'glpi_plugin_jamf_devices_id', 'int');
$this->glpiMigration->migrationOneTable('glpi_plugin_jamf_mobiledevices');
foreach ($all_mobiledevices as $mobiledevice) {
$field_map = [];
foreach ($common_fields as $cf) {
$field_map[$cf] = $mobiledevice[$cf];
}
$this->db->insert('glpi_plugin_jamf_devices', $field_map);
$this->db->update('glpi_plugin_jamf_mobiledevices', ['glpi_plugin_jamf_devices_id' => $this->db->insertId()], ['id' => $mobiledevice['id']]);
}
foreach ($common_fields as $cf) {
$this->glpiMigration->dropField('glpi_plugin_jamf_mobiledevices', $cf);
}
$this->glpiMigration->migrationOneTable('glpi_plugin_jamf_mobiledevices');
}
// Check computers table (Extra data for computers)
if (!$this->db->tableExists('glpi_plugin_jamf_computers')) {
$query = 'CREATE TABLE `glpi_plugin_jamf_computers` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`glpi_plugin_jamf_devices_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `glpi_plugin_jamf_devices_id` (`glpi_plugin_jamf_devices_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
$this->db->doQuery($query);
}
// Convert old software table to mobile device software table
if ($this->db->tableExists('glpi_plugin_jamf_softwares')) {
$this->glpiMigration->renameTable('glpi_plugin_jamf_softwares', 'glpi_plugin_jamf_mobiledevicesoftwares');
}
if (!$this->db->tableExists('glpi_plugin_jamf_computersoftwares')) {
$query = 'CREATE TABLE `glpi_plugin_jamf_computersoftwares` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`softwares_id` int(11) NOT NULL,
`bundle_id` varchar(255) NOT NULL,
`itunes_store_url` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
$this->db->doQuery($query);
}
$old_cron = $this->db->request([
'SELECT' => ['id'],
'FROM' => CronTask::getTable(),
'WHERE' => ['itemtype' => 'PluginJamfSync'],
]);
if ($old_cron->count()) {
$this->db->update(CronTask::getTable(), [
'itemtype' => 'PluginJamfCron',
], [
'itemtype' => 'PluginJamfSync',
]);
}
$this->glpiMigration->addRight(PluginJamfComputer::$rightname, ALLSTANDARDRIGHT);
}
public function apply_2_1_3_migration()
{
if ($this->db->tableExists('glpi_plugin_jamf_mobiledevicesoftwares')) {
$broken_msoftware_links = $this->db->request([
'SELECT' => ['glpi_plugin_jamf_mobiledevicesoftwares.id'],
'FROM' => 'glpi_plugin_jamf_mobiledevicesoftwares',
'LEFT JOIN' => [
'glpi_softwares' => [
'ON' => [
'glpi_plugin_jamf_mobiledevicesoftwares' => 'softwares_id',
'glpi_softwares' => 'id',
],
],
],
'WHERE' => ['glpi_softwares.id' => null],
]);
$m_ids = [];
foreach ($broken_msoftware_links as $data) {
$m_ids[] = $data['id'];
}
if ($m_ids !== []) {
$this->db->delete('glpi_plugin_jamf_mobiledevicesoftwares', [
'id' => $m_ids,
]);
}
}
if ($this->db->tableExists('glpi_plugin_jamf_computersoftwares')) {
$broken_csoftware_links = $this->db->request([
'SELECT' => ['glpi_plugin_jamf_computersoftwares.id'],
'FROM' => 'glpi_plugin_jamf_computersoftwares',
'LEFT JOIN' => [
'glpi_softwares' => [
'ON' => [
'glpi_plugin_jamf_computersoftwares' => 'softwares_id',
'glpi_softwares' => 'id',
],
],
],
'WHERE' => ['glpi_softwares.id' => null],
]);
$c_ids = [];
foreach ($broken_csoftware_links as $data) {
$c_ids[] = $data['id'];
}
if ($c_ids !== []) {
$this->db->delete('glpi_plugin_jamf_computersoftwares', [
'id' => $c_ids,
]);
}
}
}
public function apply_3_0_0_migration(): void
{
if ($this->db->tableExists('glpi_plugin_jamf_devices') && !$this->db->fieldExists('glpi_plugin_jamf_devices', 'model_identifier')) {
// Add model_identifier column (Needed specifically for some MDM commands like ScheduleOSUpdate to know applicable updates)
$this->glpiMigration->addField('glpi_plugin_jamf_devices', 'model_identifier', 'string');
$this->glpiMigration->migrationOneTable('glpi_plugin_jamf_devices');
// Fill model_identifier column for all existing devices (Need to query JSS)
$this->glpiMigration->log('Updating model_identifier for all existing devices. This may take a while...', false);
$devices = $this->db->request([
'SELECT' => ['id', 'jamf_type', 'jamf_items_id'],
'FROM' => 'glpi_plugin_jamf_devices',
]);
$jss_mobiledevices = count($devices) > 0 ? $this->api::getAllMobileDevices() : [];
foreach ($devices as $device) {
if ($device['jamf_type'] === 'MobileDevice') {
// We can get the model identifier directly from the list of mobile devices
foreach ($jss_mobiledevices as $jss_mobile) {
if ((int) $jss_mobile['id'] === $device['jamf_items_id']) {
$this->db->update(
'glpi_plugin_jamf_devices',
[
'model_identifier' => $jss_mobile['modelIdentifier'],
],
[
'id' => $device['id'],
],
);
break;
}
}
} elseif ($device['jamf_type'] === 'Computer') {
// We need to query the JSS for the computer's model identifier
$computer = $this->api::getComputerByID($device['jamf_items_id'], true);
if ($computer !== null) {
$this->db->update(
'glpi_plugin_jamf_devices',
[
'model_identifier' => $computer['hardware']['modelIdentifier'],
],
[
'id' => $device['id'],
],
);
}
} else {
$this->glpiMigration->log('Found device with invalid Jamf type (ID: ' . $device['id'] . '). Ignoring.', true);
}
}
}
//create dir if needed
if (!file_exists(GLPI_PLUGIN_DOC_DIR . '/jamf')) {
mkdir(GLPI_PLUGIN_DOC_DIR . '/jamf');
}
// Copy default pmv from tools dir
$pmv_file_path = GLPI_PLUGIN_DOC_DIR . '/jamf/pmv.json';
if (!file_exists($pmv_file_path)) {
copy(Plugin::getPhpDir('jamf') . '/resources/pmv.json', $pmv_file_path);
}
// Register Cron task to update PMV periodically
CronTask::register(PluginJamfCron::class, 'updatePMV', DAY_TIMESTAMP, [
'comment' => 'Update JSON file containing information about applicable OS versions for different Apple product models',
'state' => CronTask::STATE_WAITING,
]);
}
public function apply_3_0_1_migration(): void
{
// Change udid column in glpi_plugin_jamf_imports to allow NULL values
$this->db->doQuery('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');
}
}