diff options
author | Simon Rettberg | 2014-12-12 18:28:38 +0100 |
---|---|---|
committer | Simon Rettberg | 2014-12-12 18:28:38 +0100 |
commit | 97a0f7dcfdcf4a5263c1cc6c19160a0868abb5f2 (patch) | |
tree | cf735055cfb785ac64e60ab477e215f5f50ad767 /inc | |
parent | [news] fix xml tags of news api (diff) | |
download | slx-admin-97a0f7dcfdcf4a5263c1cc6c19160a0868abb5f2.tar.gz slx-admin-97a0f7dcfdcf4a5263c1cc6c19160a0868abb5f2.tar.xz slx-admin-97a0f7dcfdcf4a5263c1cc6c19160a0868abb5f2.zip |
Rework config module class structure. Still some TODOs though....
Diffstat (limited to 'inc')
-rw-r--r-- | inc/configmodule/adauth.inc.php (renamed from inc/configmodule.inc.php) | 89 | ||||
-rw-r--r-- | inc/configmodule/branding.inc.php | 44 | ||||
-rw-r--r-- | inc/configmodule/customodule.inc.php | 16 | ||||
-rw-r--r-- | inc/configmodules.inc.php | 94 | ||||
-rw-r--r-- | inc/database.inc.php | 6 | ||||
-rw-r--r-- | inc/trigger.inc.php | 32 |
6 files changed, 218 insertions, 63 deletions
diff --git a/inc/configmodule.inc.php b/inc/configmodule/adauth.inc.php index 1788a53a..c0d4860c 100644 --- a/inc/configmodule.inc.php +++ b/inc/configmodule/adauth.inc.php @@ -1,17 +1,28 @@ <?php -class ConfigModule +ConfigModules::registerModule( + ConfigModule_AdAuth::MODID, // ID + Dictionary::translate('config-module', 'adAuth_title'), // Title + Dictionary::translate('config-module', 'adAuth_description'), // Description + Dictionary::translate('config-module', 'group_authentication'), // Group + true // Only one per config? +); + +class ConfigModule_AdAuth extends ConfigModule { - - public static function insertAdConfig($title, $server, $searchbase, $binddn, $bindpw, $home) + const MODID = 'AdAuth'; + + public static function insert($title, $server, $searchbase, $binddn, $bindpw, $home) { Database::exec("LOCK TABLE configtgz_module WRITE"); Database::exec("INSERT INTO configtgz_module (title, moduletype, filepath, contents) " - . " VALUES (:title, 'AD_AUTH', '', '')", array('title' => $title)); + . " VALUES (:title, :modid, '', '')", array('title' => $title, 'modid' => self::MODID)); $id = Database::lastInsertId(); if (!is_numeric($id)) Util::traceError('Inserting new AD config to DB did not yield a numeric insert id'); // Entry created, now try to get a free port for the proxy - $res = Database::simpleQuery("SELECT moduleid, contents FROM configtgz_module WHERE moduletype = 'AD_AUTH'"); + $res = Database::simpleQuery("SELECT moduleid, contents FROM configtgz_module WHERE moduletype = :modid", array( + 'modid' => self::MODID + )); $ports = array(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { if ($row['moduleid'] == $id) { @@ -48,6 +59,36 @@ class ConfigModule $ownEntry['filename'] = $moduleTgz; return $ownEntry; } + + /** + * To be called if the server ip changes, as it's embedded in the AD module configs. + * This will then recreate all AD tgz modules. + */ + private static function rebuildAll($parent = NULL) + { + // Stop all running instances of ldadp + $task = Taskmanager::submit('LdadpLauncher', array( + 'parentTask' => $parent, + 'failOnParentFail' => false, + 'ids' => array() + )); + $ads = self::getAll(); + if (empty($ads)) // Nothing to do + return false; + + if (isset($task['id'])) + $parent = $task['id']; + foreach ($ads as $ad) { + $ad['parentTask'] = $parent; + $ad['failOnParentFail'] = false; + $ad['proxyip'] = Property::getServerIp(); + $task = Taskmanager::submit('CreateAdConfig', $ad); + if (isset($task['id'])) + $parent = $task['id']; + } + Trigger::ldadp($parent); + return $parent; + } /** * Get all existing AD proxy configs. @@ -55,9 +96,11 @@ class ConfigModule * @return array array of ad configs in DB with fields: * moduleid, filename, server, searchbase, binddn, bindpw, home, proxyport */ - public static function getAdConfigs() + public static function getAll() { - $res = Database::simpleQuery("SELECT moduleid, filepath, contents FROM configtgz_module WHERE moduletype = 'AD_AUTH'"); + $res = Database::simpleQuery("SELECT moduleid, filepath, contents FROM configtgz_module WHERE moduletype = :modid", array( + 'modid' => self::MODID + )); $mods = array(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { $data = json_decode($row['contents'], true); @@ -68,32 +111,14 @@ class ConfigModule return $mods; } - public static function insertBrandingModule($title, $archive) + // ############## Callbacks ############################# + + /** + * Server IP changed - rebuild all AD modules. + */ + public function event_serverIpChanged() { - Database::exec("INSERT INTO configtgz_module (title, moduletype, filepath, contents) " - . " VALUES (:title, 'BRANDING', '', '')", array('title' => $title)); - $id = Database::lastInsertId(); - if (!is_numeric($id)) Util::traceError('Inserting new Branding Module into DB did not yield a numeric insert id'); - // Move tgz - $moduleTgz = CONFIG_TGZ_LIST_DIR . '/modules/BRANDING_id_' . $id . '.' . mt_rand() . '.tgz'; - $task = Taskmanager::submit('MoveFile', array( - 'source' => $archive, - 'destination' => $moduleTgz - )); - $task = Taskmanager::waitComplete($task, 3000); - if (Taskmanager::isFailed($task) || $task['statusCode'] !== TASK_FINISHED) { - Taskmanager::addErrorMessage($task); - Database::exec("DELETE FROM configtgz_module WHERE moduleid = :moduleid LIMIT 1", array( - 'moduleid' => $id - )); - return false; - } - // Update with path - Database::exec("UPDATE configtgz_module SET filepath = :filename WHERE moduleid = :id LIMIT 1", array( - 'id' => $id, - 'filename' => $moduleTgz - )); - return true; + self::rebuildAll(); } } diff --git a/inc/configmodule/branding.inc.php b/inc/configmodule/branding.inc.php new file mode 100644 index 00000000..f293fda6 --- /dev/null +++ b/inc/configmodule/branding.inc.php @@ -0,0 +1,44 @@ +<?php + +ConfigModules::registerModule( + ConfigModule_Branding::MODID, // ID + Dictionary::translate('config-module', 'branding_title'), // Title + Dictionary::translate('config-module', 'branding_description'), // Description + Dictionary::translate('config-module', 'group_branding'), // Group + true // Only one per config? +); + +class ConfigModule_Branding extends ConfigModule +{ + const MODID = 'Branding'; + + public static function insert($title, $archive) + { + Database::exec("INSERT INTO configtgz_module (title, moduletype, filepath, contents) " + . " VALUES (:title, :modid, '', '')", array('title' => $title, 'modid' => self::MODID)); + $id = Database::lastInsertId(); + if (!is_numeric($id)) + Util::traceError('Inserting new Branding Module into DB did not yield a numeric insert id'); + // Move tgz + $moduleTgz = CONFIG_TGZ_LIST_DIR . '/modules/BRANDING_id_' . $id . '.' . mt_rand() . '.tgz'; + $task = Taskmanager::submit('MoveFile', array( + 'source' => $archive, + 'destination' => $moduleTgz + )); + $task = Taskmanager::waitComplete($task, 3000); + if (Taskmanager::isFailed($task) || $task['statusCode'] !== TASK_FINISHED) { + Taskmanager::addErrorMessage($task); + Database::exec("DELETE FROM configtgz_module WHERE moduleid = :moduleid LIMIT 1", array( + 'moduleid' => $id + )); + return false; + } + // Update with path + Database::exec("UPDATE configtgz_module SET filepath = :filename WHERE moduleid = :id LIMIT 1", array( + 'id' => $id, + 'filename' => $moduleTgz + )); + return true; + } + +} diff --git a/inc/configmodule/customodule.inc.php b/inc/configmodule/customodule.inc.php new file mode 100644 index 00000000..89f0aca6 --- /dev/null +++ b/inc/configmodule/customodule.inc.php @@ -0,0 +1,16 @@ +<?php + +ConfigModules::registerModule( + ConfigModule_CustomModule::MODID, // ID + Dictionary::translate('config-module', 'custom_title'), // Title + Dictionary::translate('config-module', 'custom_description'), // Description + Dictionary::translate('config-module', 'group_generic'), // Group + false, // Only one per config? + 100 // Sort order +); + +class ConfigModule_CustomModule extends ConfigModule +{ + const MODID = 'CustomModule'; + +} diff --git a/inc/configmodules.inc.php b/inc/configmodules.inc.php new file mode 100644 index 00000000..bf870e4f --- /dev/null +++ b/inc/configmodules.inc.php @@ -0,0 +1,94 @@ +<?php + +class ConfigModules +{ + + private static $moduleTypes = false; + + /** + * Load all known config module types. This is done + * by including *.inc.php from inc/configmodule/. The + * files there should in turn call ConfigModule::registerModule() + * to register themselves. + */ + public static function loadDb() + { + if (self::$moduleTypes !== false) + return; + self::$moduleTypes = array(); + foreach (glob('inc/configmodule/*.inc.php') as $file) { + require_once $file; + } + } + + /** + * Get all known config modules. + * + * @return array list of modules + */ + public static function getList() + { + self::loadDb(); + return self::$moduleTypes; + } + + /** + * Add a known configuration module. Every inc/configmodule/*.inc.php should call this. + * + * @param string $id Identifier for the module. + * The module class must be called ConfigModule_{$id}, the wizard start class {$id}_Start. + * The wizard's classes should be located in modules/sysconfig/addmodule_{$id_lowercase}.inc.php + * @param string $title Title of this module type + * @param string $description Description for this module type + * @param string $group Title for group this module type belongs to + * @param bool $unique Can only one such module be added to a config? + * @param int $sortOrder Lower comes first, alphabetical ordering otherwiese + */ + public static function registerModule($id, $title, $description, $group, $unique, $sortOrder = 0) + { + if (isset(self::$moduleTypes[$id])) + Util::traceError("Config Module $id already registered!"); + $moduleClass = 'ConfigModule_' . $id; + $wizardClass = $id . '_Start'; + if (!class_exists($moduleClass)) + Util::traceError("Class $moduleClass does not exist!"); + if (get_parent_class($moduleClass) !== 'ConfigModule') + Util::traceError("$moduleClass does not have ConfigModule as its parent!"); + self::$moduleTypes[$id] = array( + 'title' => $title, + 'description' => $description, + 'group' => $group, + 'unique' => $unique, + 'sortOrder' => $sortOrder, + 'moduleClass' => $moduleClass, + 'wizardClass' => $wizardClass + ); + } + + /** + * Will be called if the server's IP address changes. The event will be propagated + * to all config module classes so action can be taken if appropriate. + */ + public static function serverIpChanged() + { + self::loadDb(); + foreach (self::$moduleTypes as $module) { + $instance = new $module['moduleClass']; + $instance->event_serverIpChanged(); + } + } + +} + +/** + * Base class for config modules + */ +abstract class ConfigModule +{ + + public function event_serverIpChanged() + { + + } + +} diff --git a/inc/database.inc.php b/inc/database.inc.php index 85bee4b1..2c535d04 100644 --- a/inc/database.inc.php +++ b/inc/database.inc.php @@ -7,6 +7,10 @@ class Database { + /** + * + * @var \PDO Database handle + */ private static $dbh = false; private static $statements = array(); @@ -16,7 +20,7 @@ class Database */ public static function getExpectedSchemaVersion() { - return 7; + return 8; } public static function needSchemaUpdate() diff --git a/inc/trigger.inc.php b/inc/trigger.inc.php index a5a149c8..38b25540 100644 --- a/inc/trigger.inc.php +++ b/inc/trigger.inc.php @@ -83,10 +83,11 @@ class Trigger */ public static function ldadp($parent = NULL) { + // TODO: Fetch list from ConfigModule_AdAuth (call loadDb first) $res = Database::simpleQuery("SELECT moduleid, configtgz.filepath FROM configtgz_module" . " INNER JOIN configtgz_x_module USING (moduleid)" . " INNER JOIN configtgz USING (configid)" - . " WHERE moduletype = 'AD_AUTH'"); + . " WHERE moduletype = 'AdAuth'"); // TODO: Multiconfig support $id = array(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { @@ -106,35 +107,6 @@ class Trigger } /** - * To be called if the server ip changes, as it's embedded in the AD module configs. - * This will then recreate all AD tgz modules. - */ - public static function rebuildAdModules($parent = NULL) - { - $task = Taskmanager::submit('LdadpLauncher', array( - 'parentTask' => $parent, - 'failOnParentFail' => false, - 'ids' => array() - )); // Stop all running instances - $ads = ConfigModule::getAdConfigs(); - if (empty($ads)) - return false; - - if (isset($task['id'])) - $parent = $task['id']; - foreach ($ads as $ad) { - $ad['parentTask'] = $parent; - $ad['failOnParentFail'] = false; - $ad['proxyip'] = Property::getServerIp(); - $task = Taskmanager::submit('CreateAdConfig', $ad); - if (isset($task['id'])) - $parent = $task['id']; - } - Trigger::ldadp($parent); - return $parent; - } - - /** * Mount the VM store into the server. * * @return array task status of mount procedure, or false on error |