3939 * @since 0.1.0
4040 *
4141 */
42- class PluginFlyvemdmInstall {
42+ class PluginFlyvemdmInstaller {
4343
4444 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 ' ;
4545
4646 const BACKEND_MQTT_USER = 'flyvemdm-backend ' ;
4747
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+
4872 protected static $ currentVersion = null ;
4973
5074 protected $ migration ;
@@ -54,7 +78,7 @@ class PluginFlyvemdmInstall {
5478 * @param string $classname
5579 * @return bool
5680 */
57- public static function autoload ($ classname ) {
81+ public function autoload ($ classname ) {
5882 // useful only for installer GLPI autoloader already handles inc/ folder
5983 $ filename = dirname (__DIR__ ) . '/inc/ ' . strtolower (str_replace ('PluginFlyvemdm ' , '' ,
6084 $ classname )) . '.class.php ' ;
@@ -71,34 +95,52 @@ public static function autoload($classname) {
7195 * @return boolean true (assume success, needs enhancement)
7296 *
7397 */
74- public function install (Migration $ migration ) {
98+ public function install () {
7599 global $ DB ;
76100
77- $ this ->migration = $ migration ;
78101 spl_autoload_register ([__CLASS__ , 'autoload ' ]);
79102
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+ }
92115
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+ }
97124 }
98125
126+ $ this ->migration ->executeMigration ();
127+
99128 if (version_compare (GLPI_VERSION , '9.3.0 ' ) >= 0 ) {
100129 $ this ->migrateToInnodb ();
101130 }
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+
102144 return true ;
103145 }
104146
@@ -156,45 +198,25 @@ public function createDirectories() {
156198 }
157199
158200 // 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 " );
163204 }
164205 }
165206
166207 /**
167208 * @return null|string
168209 */
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 ' ];
194217 }
195218 }
196-
197- return false ;
219+ return self ::$ currentVersion ;
198220 }
199221
200222 protected function createRootEntityConfig () {
@@ -235,8 +257,6 @@ protected function createFirstAccess() {
235257 PluginFlyvemdmInvitation::$ rightname => ALLSTANDARDRIGHT ,
236258 PluginFlyvemdmInvitationLog::$ rightname => READ ,
237259 PluginFlyvemdmTaskstatus::$ rightname => READ ,
238- PluginFlyvemdmFDroidApplication::$ rightname => READ | UPDATE | READNOTE | UPDATENOTE ,
239- PluginFlyvemdmFDroidMarket::$ rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE ,
240260 ];
241261
242262 $ profileRight ->updateProfileRights ($ _SESSION ['glpiactiveprofile ' ]['id ' ], $ newRights );
@@ -475,53 +495,22 @@ public function createNotificationTargetInvitation() {
475495 /**
476496 * Upgrade the plugin to the current code version
477497 *
478- * @param string version to upgrade from
498+ * @param string $fromVersion
479499 */
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 :
499510 }
500- if (! PLUGIN_FLYVEMDM_IS_OFFICIAL_RELEASE ) {
501- $ this ->upgradeOneStep ('develop ' );
511+ if (PluginFlyvemdmCommon:: endsWith ( PLUGIN_FLYVEMDM_VERSION , " -dev " ) ) {
512+ $ this ->upgradeOneStep ('dev ' );
502513 }
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- );
525514 }
526515
527516 /**
@@ -530,19 +519,21 @@ private function installUpgradeCommonTasks() {
530519 * @param string $toVersion
531520 */
532521 protected function upgradeOneStep ($ toVersion ) {
522+
533523 ini_set ("max_execution_time " , "0 " );
534524 ini_set ("memory_limit " , "-1 " );
535525
536526 $ suffix = str_replace ('. ' , '_ ' , $ toVersion );
537- $ includeFile = __DIR__ . "/update_to_ $ suffix.php " ;
527+ $ includeFile = __DIR__ . "/upgrade/ update_to_ $ suffix.php " ;
538528 if (is_readable ($ includeFile ) && is_file ($ includeFile )) {
539529 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+ }
546537 }
547538 }
548539
@@ -552,18 +543,6 @@ protected function createJobs() {
552543 'comment ' => __ ('Parse uploaded applications to collect metadata ' , 'flyvemdm ' ),
553544 'mode ' => CronTask::MODE_EXTERNAL ,
554545 ]);
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- ]);
567546 }
568547
569548 /**
@@ -833,8 +812,6 @@ protected function deleteTables() {
833812 PluginFlyvemdmPolicyCategory::getTable (),
834813 PluginFlyvemdmWellknownpath::getTable (),
835814 PluginFlyvemdmTaskstatus::getTable (),
836- PluginFlyvemdmFDroidApplication::getTable (),
837- PluginFlyvemdmFDroidMarket::getTable (),
838815 ];
839816
840817 foreach ($ tables as $ table ) {
0 commit comments