39
39
* @since 0.1.0
40
40
*
41
41
*/
42
- class PluginFlyvemdmInstall {
42
+ class PluginFlyvemdmInstaller {
43
43
44
44
const DEFAULT_CIPHERS_LIST = 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:AES128:AES256:RC4-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK ' ;
45
45
46
46
const BACKEND_MQTT_USER = 'flyvemdm-backend ' ;
47
47
48
+ // Order of this array is mandatory due tu dependancies on install and uninstall
49
+ protected static $ itemtypesToInstall = [
50
+ 'mqttuser ' ,
51
+ // Must be before config because config creates a mqtt user for the plugin
52
+ 'mqttacl ' ,
53
+ // Must be before config because config creates a mqtt ACL for the plugin
54
+ 'config ' ,
55
+ 'entityconfig ' ,
56
+ 'mqttlog ' ,
57
+ 'agent ' ,
58
+ 'package ' ,
59
+ 'file ' ,
60
+ 'fleet ' ,
61
+ 'profile ' ,
62
+ 'notificationtargetinvitation ' ,
63
+ 'geolocation ' ,
64
+ 'policy ' ,
65
+ 'policycategory ' ,
66
+ 'fleet_policy ' ,
67
+ 'wellknownpath ' ,
68
+ 'invitation ' ,
69
+ 'invitationlog ' ,
70
+ ];
71
+
48
72
protected static $ currentVersion = null ;
49
73
50
74
protected $ migration ;
@@ -54,7 +78,7 @@ class PluginFlyvemdmInstall {
54
78
* @param string $classname
55
79
* @return bool
56
80
*/
57
- public static function autoload ($ classname ) {
81
+ public function autoload ($ classname ) {
58
82
// useful only for installer GLPI autoloader already handles inc/ folder
59
83
$ filename = dirname (__DIR__ ) . '/inc/ ' . strtolower (str_replace ('PluginFlyvemdm ' , '' ,
60
84
$ classname )) . '.class.php ' ;
@@ -71,34 +95,52 @@ public static function autoload($classname) {
71
95
* @return boolean true (assume success, needs enhancement)
72
96
*
73
97
*/
74
- public function install (Migration $ migration ) {
98
+ public function install () {
75
99
global $ DB ;
76
100
77
- $ this ->migration = $ migration ;
78
101
spl_autoload_register ([__CLASS__ , 'autoload ' ]);
79
102
80
- $ this ->installSchema ( );
81
- $ this ->createInitialConfig ( );
82
- $ this -> migration -> executeMigration ();
83
- $ this -> installUpgradeCommonTasks ();
84
-
85
- return true ;
86
- }
87
-
88
- protected function installSchema ( ) {
89
- global $ DB ;
90
-
91
- $ this -> migration -> displayMessage ( " create database schema " );
103
+ $ this ->migration = new Migration ( PLUGIN_FLYVEMDM_VERSION );
104
+ $ this ->migration -> setVersion ( PLUGIN_FLYVEMDM_VERSION );
105
+
106
+ // adding DB model from sql file
107
+ // TODO : migrate in-code DB model setup here
108
+ if ( self :: getCurrentVersion () == '' ) {
109
+ // Setup DB model
110
+ $ dbFile = PLUGIN_FLYVEMDM_ROOT . " /install/mysql/plugin_flyvemdm_empty.sql " ;
111
+ if (! $ DB -> runFile ( $ dbFile ) ) {
112
+ $ this -> migration -> displayWarning ( " Error creating tables : " . $ DB -> error (), true ) ;
113
+ return false ;
114
+ }
92
115
93
- $ dbFile = __DIR__ . '/mysql/plugin_flyvemdm_empty.sql ' ;
94
- if (!$ DB ->runFile ($ dbFile )) {
95
- $ this ->migration ->displayWarning ("Error creating tables : " . $ DB ->error (), true );
96
- return false ;
116
+ $ this ->createInitialConfig ();
117
+ } else {
118
+ if (PluginFlyvemdmCommon::endsWith (PLUGIN_FLYVEMDM_VERSION ,
119
+ "-dev " ) || (version_compare (self ::getCurrentVersion (),
120
+ PLUGIN_FLYVEMDM_VERSION ) != 0 )) {
121
+ // TODO : Upgrade (or downgrade)
122
+ $ this ->upgrade (self ::getCurrentVersion ());
123
+ }
97
124
}
98
125
126
+ $ this ->migration ->executeMigration ();
127
+
99
128
if (version_compare (GLPI_VERSION , '9.3.0 ' ) >= 0 ) {
100
129
$ this ->migrateToInnodb ();
101
130
}
131
+ $ this ->createDirectories ();
132
+ $ this ->createFirstAccess ();
133
+ $ this ->createGuestProfileAccess ();
134
+ $ this ->createAgentProfileAccess ();
135
+ $ this ->createDefaultFleet ();
136
+ $ this ->createPolicies ();
137
+ $ this ->createNotificationTargetInvitation ();
138
+ $ this ->createJobs ();
139
+ $ this ->createRootEntityConfig ();
140
+ $ this ->createDisplayPreferences ();
141
+
142
+ Config::setConfigurationValues ('flyvemdm ' , ['version ' => PLUGIN_FLYVEMDM_VERSION ]);
143
+
102
144
return true ;
103
145
}
104
146
@@ -156,45 +198,25 @@ public function createDirectories() {
156
198
}
157
199
158
200
// Create cache directory for the template engine
159
- if (!file_exists (FLYVEMDM_TEMPLATE_CACHE_PATH )) {
160
- if (!mkdir (FLYVEMDM_TEMPLATE_CACHE_PATH , 0770 , true )) {
161
- $ this ->migration ->displayWarning ("Cannot create " . FLYVEMDM_TEMPLATE_CACHE_PATH . " directory " );
162
- }
201
+ PluginFlyvemdmCommon::recursiveRmdir (FLYVEMDM_TEMPLATE_CACHE_PATH );
202
+ if (!mkdir (FLYVEMDM_TEMPLATE_CACHE_PATH , 0770 , true )) {
203
+ $ this ->migration ->displayWarning ("Cannot create " . FLYVEMDM_TEMPLATE_CACHE_PATH . " directory " );
163
204
}
164
205
}
165
206
166
207
/**
167
208
* @return null|string
168
209
*/
169
- public function getSchemaVersion () {
170
- if ($ this ->isPluginInstalled ()) {
171
- $ config = Config::getConfigurationValues ('flyvemdm ' , ['schema_version ' ]);
172
- if (!isset ($ config ['schema_version ' ])) {
173
- return '0.0 ' ;
174
- }
175
- return $ config ['schema_version ' ];
176
- }
177
-
178
- return null ;
179
- }
180
-
181
- /**
182
- * is the plugin already installed ?
183
- *
184
- * @return boolean
185
- */
186
- public function isPluginInstalled () {
187
- global $ DB ;
188
-
189
- // Check tables of the plugin between 1.1 and 2.0 releases
190
- $ result = $ DB ->query ("SHOW TABLES LIKE 'glpi_plugin_flyvemdm_%' " );
191
- if ($ result ) {
192
- if ($ DB ->numrows ($ result ) > 0 ) {
193
- return true ;
210
+ public static function getCurrentVersion () {
211
+ if (self ::$ currentVersion === null ) {
212
+ $ config = \Config::getConfigurationValues ('flyvemdm ' , ['version ' ]);
213
+ if (!isset ($ config ['version ' ])) {
214
+ self ::$ currentVersion = '' ;
215
+ } else {
216
+ self ::$ currentVersion = $ config ['version ' ];
194
217
}
195
218
}
196
-
197
- return false ;
219
+ return self ::$ currentVersion ;
198
220
}
199
221
200
222
protected function createRootEntityConfig () {
@@ -235,8 +257,6 @@ protected function createFirstAccess() {
235
257
PluginFlyvemdmInvitation::$ rightname => ALLSTANDARDRIGHT ,
236
258
PluginFlyvemdmInvitationLog::$ rightname => READ ,
237
259
PluginFlyvemdmTaskstatus::$ rightname => READ ,
238
- PluginFlyvemdmFDroidApplication::$ rightname => READ | UPDATE | READNOTE | UPDATENOTE ,
239
- PluginFlyvemdmFDroidMarket::$ rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE ,
240
260
];
241
261
242
262
$ profileRight ->updateProfileRights ($ _SESSION ['glpiactiveprofile ' ]['id ' ], $ newRights );
@@ -475,53 +495,22 @@ public function createNotificationTargetInvitation() {
475
495
/**
476
496
* Upgrade the plugin to the current code version
477
497
*
478
- * @param string version to upgrade from
498
+ * @param string $fromVersion
479
499
*/
480
- public function upgrade (Migration $ migration ) {
481
- spl_autoload_register ([__CLASS__ , 'autoload ' ]);
482
-
483
- $ this ->migration = $ migration ;
484
- $ fromSchemaVersion = $ this ->getSchemaVersion ();
485
-
486
- switch ($ fromSchemaVersion ) {
487
- case '0.0 ' :
488
- // Upgrade to 2.0
489
- $ this ->upgradeOneStep ('2.0 ' );
490
-
491
- case '2.0 ' :
492
- // Example : upgrade to version 2.1
493
- // $this->upgradeOneStep('2.1');
494
-
495
- case '3.0 ' :
496
- // Example : upgrade to version 3.0
497
- // $this->upgradeOneStep('3.0');
498
-
500
+ protected function upgrade ($ fromVersion ) {
501
+ switch ($ fromVersion ) {
502
+ case '2.0.0 ' :
503
+ // Example : upgrade to version 3.0.0
504
+ // $this->upgradeOneStep('3.0.0');
505
+ case '3.0.0 ' :
506
+ // Example : upgrade to version 4.0.0
507
+ // $this->upgradeOneStep('4.0.0');
508
+
509
+ default :
499
510
}
500
- if (! PLUGIN_FLYVEMDM_IS_OFFICIAL_RELEASE ) {
501
- $ this ->upgradeOneStep ('develop ' );
511
+ if (PluginFlyvemdmCommon:: endsWith ( PLUGIN_FLYVEMDM_VERSION , " -dev " ) ) {
512
+ $ this ->upgradeOneStep ('dev ' );
502
513
}
503
- $ this ->installUpgradeCommonTasks ();
504
- return true ;
505
- }
506
-
507
- private function installUpgradeCommonTasks () {
508
- $ this ->createDirectories ();
509
- $ this ->createFirstAccess ();
510
- $ this ->createGuestProfileAccess ();
511
- $ this ->createAgentProfileAccess ();
512
- $ this ->createDefaultFleet ();
513
- $ this ->createPolicies ();
514
- $ this ->createNotificationTargetInvitation ();
515
- $ this ->createJobs ();
516
- $ this ->createRootEntityConfig ();
517
- $ this ->createDisplayPreferences ();
518
-
519
- Config::setConfigurationValues (
520
- 'flyvemdm ' , [
521
- 'version ' => PLUGIN_FLYVEMDM_VERSION ,
522
- 'schema_version ' => PLUGIN_FLYVEMDM_SCHEMA_VERSION ,
523
- ]
524
- );
525
514
}
526
515
527
516
/**
@@ -530,19 +519,21 @@ private function installUpgradeCommonTasks() {
530
519
* @param string $toVersion
531
520
*/
532
521
protected function upgradeOneStep ($ toVersion ) {
522
+
533
523
ini_set ("max_execution_time " , "0 " );
534
524
ini_set ("memory_limit " , "-1 " );
535
525
536
526
$ suffix = str_replace ('. ' , '_ ' , $ toVersion );
537
- $ includeFile = __DIR__ . "/update_to_ $ suffix.php " ;
527
+ $ includeFile = __DIR__ . "/upgrade/ update_to_ $ suffix.php " ;
538
528
if (is_readable ($ includeFile ) && is_file ($ includeFile )) {
539
529
include_once $ includeFile ;
540
- $ updateClass = "PluginFlyvemdmUpgradeTo $ suffix " ;
541
- $ this ->migration ->addNewMessageArea ("Upgrade to $ toVersion " );
542
- $ upgradeStep = new $ updateClass ();
543
- $ upgradeStep ->upgrade ($ this ->migration );
544
- $ this ->migration ->executeMigration ();
545
- $ this ->migration ->displayMessage ('Done ' );
530
+ $ updateFunction = "plugin_flyvemdm_update_to_ $ suffix " ;
531
+ if (function_exists ($ updateFunction )) {
532
+ $ this ->migration ->addNewMessageArea ("Upgrade to $ toVersion " );
533
+ $ updateFunction ($ this ->migration );
534
+ $ this ->migration ->executeMigration ();
535
+ $ this ->migration ->displayMessage ('Done ' );
536
+ }
546
537
}
547
538
}
548
539
@@ -552,18 +543,6 @@ protected function createJobs() {
552
543
'comment ' => __ ('Parse uploaded applications to collect metadata ' , 'flyvemdm ' ),
553
544
'mode ' => CronTask::MODE_EXTERNAL ,
554
545
]);
555
-
556
- CronTask::Register (PluginFlyvemdmFDroidMarket::class, 'UpdateRepositories ' , DAY_TIMESTAMP ,
557
- [
558
- 'comment ' => __ ('Update the list of applications available from F-Droid like repositories ' , 'flyvemdm ' ),
559
- 'mode ' => CronTask::MODE_EXTERNAL
560
- ]);
561
-
562
- CronTask::Register (PluginFlyvemdmFDroidApplication::class, 'DownloadApplications ' , DAY_TIMESTAMP ,
563
- [
564
- 'comment ' => __ ('Imports applications for deployment ' , 'flyvemdm ' ),
565
- 'mode ' => CronTask::MODE_EXTERNAL
566
- ]);
567
546
}
568
547
569
548
/**
@@ -833,8 +812,6 @@ protected function deleteTables() {
833
812
PluginFlyvemdmPolicyCategory::getTable (),
834
813
PluginFlyvemdmWellknownpath::getTable (),
835
814
PluginFlyvemdmTaskstatus::getTable (),
836
- PluginFlyvemdmFDroidApplication::getTable (),
837
- PluginFlyvemdmFDroidMarket::getTable (),
838
815
];
839
816
840
817
foreach ($ tables as $ table ) {
0 commit comments