partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 48d318362565d774d9ed9dbab17334a5e92e79b0
parent b93aa8c242923d8d8338369baf11e70c4f7bc34f
Author: Felicitus <felicitus@felicitus.org>
Date:   Sun, 11 Oct 2015 16:15:00 +0200

Migrated config files containing passwords to PHP, parse existing legacy config for setup

Diffstat:
M.gitignore | 3++-
Mapp/config/config.yml | 2+-
Mapp/config/config_doctrine.yml | 2+-
Mapp/config/config_setup_test.yml | 2+-
Asrc/PartKeepr/SetupBundle/Controller/ExistingConfigParserController.php | 133+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/PartKeepr/SetupBundle/Controller/SetupController.php | 54+++++++++++++++++++++++++++++-------------------------
Msrc/PartKeepr/SetupBundle/Resources/config/routing.yml | 4++++
Asrc/PartKeepr/SetupBundle/Resources/views/parameters.php.twig | 40++++++++++++++++++++++++++++++++++++++++
Dsrc/PartKeepr/SetupBundle/Resources/views/setup.html.twig | 42------------------------------------------
Asrc/PartKeepr/SetupBundle/Visitor/LegacyConfigVisitor.php | 31+++++++++++++++++++++++++++++++
Mweb/setup/index.html | 2++
Mweb/setup/js/Cards/DatabaseParametersCard.MySQL.js | 21+++++++++++++++++++--
Mweb/setup/js/Cards/DatabaseParametersCard.PostgreSQL.js | 4++--
Mweb/setup/js/Cards/DatabaseParametersCard.js | 1+
Aweb/setup/js/Cards/ExistingConfigParserCard.js | 16++++++++++++++++
Mweb/setup/js/PartKeeprSetup.js | 4++--
Mweb/setup/js/SetupTests/AbstractTest.js | 7+++++++
Aweb/setup/js/SetupTests/ExistingConfigurationTest.js | 18++++++++++++++++++
Mweb/setup/js/SetupWizard.js | 3++-
19 files changed, 311 insertions(+), 78 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -21,8 +21,9 @@ theme/sass/.sass-cache/ /reports /nbproject/ partkeepr.jsfiles -app/config/parameters_setup.yml app/authkey.php +app/config/parameters.php +app/config/parameters_setup.php /web/bundles/ /web/images/* diff --git a/app/config/config.yml b/app/config/config.yml @@ -1,5 +1,5 @@ imports: - - { resource: parameters.yml } + - { resource: parameters.php } - { resource: security.yml } - { resource: partkeepr.yml } - { resource: config_framework.yml } diff --git a/app/config/config_doctrine.yml b/app/config/config_doctrine.yml @@ -10,7 +10,7 @@ doctrine: charset: UTF8 server_version: 5.6 - # if using pdo_sqlite as your database driver, add the path in parameters.yml + # if using pdo_sqlite as your database driver, add the path in parameters.php # e.g. database_path: %kernel.root_dir%/data/data.db3 # path: %database_path% diff --git a/app/config/config_setup_test.yml b/app/config/config_setup_test.yml @@ -1,7 +1,7 @@ imports: - { resource: config_setup.yml } - { resource: security.yml } - - { resource: parameters_setup.yml } + - { resource: parameters_setup.php } - { resource: partkeepr.yml } - { resource: config_framework.yml } - { resource: config_doctrine.yml } diff --git a/src/PartKeepr/SetupBundle/Controller/ExistingConfigParserController.php b/src/PartKeepr/SetupBundle/Controller/ExistingConfigParserController.php @@ -0,0 +1,133 @@ +<?php +namespace PartKeepr\SetupBundle\Controller; + +use PartKeepr\SetupBundle\Visitor\LegacyConfigVisitor; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Yaml\Parser; + +class ExistingConfigParserController extends SetupController +{ + /** + * @Route("/setup/parseExistingConfig") + */ + public function parseExistingConfigAction(Request $request) + { + $data = json_decode($request->getContent(), true); + + $response = $this->handleRequest($request, "/setup/_int_parse_config"); + + return new Response($response->getContent()); + } + + /** + * @Route("/setup/_int_parse_config") + */ + public function intParseExistingConfigAction(Request $request) + { + $response = array( + "success" => true, + "errors" => [], + "message" => "Existing configuration imported successfully", + ); + + try { + $legacyConfig = $this->legacyConfigParser(); + + if (count($legacyConfig) == 0) { + $response["message"] = "No configuration found"; + } else { + $config = array( + "database" => array() + ); + + if (array_key_exists("partkeepr.database.driver", $legacyConfig)) { + $config["database"]["driver"] = $legacyConfig["partkeepr.database.driver"]; + } + + if (array_key_exists("partkeepr.database.host", $legacyConfig)) { + $config["database"]["host"] = $legacyConfig["partkeepr.database.host"]; + } + + if (array_key_exists("partkeepr.database.username", $legacyConfig)) { + $config["database"]["user"] = $legacyConfig["partkeepr.database.username"]; + } + + if (array_key_exists("partkeepr.database.password", $legacyConfig)) { + $config["database"]["password"] = $legacyConfig["partkeepr.database.password"]; + } + + if (array_key_exists("partkeepr.database.dbname", $legacyConfig)) { + $config["database"]["name"] = $legacyConfig["partkeepr.database.dbname"]; + } + + if (array_key_exists("partkeepr.database.port", $legacyConfig)) { + $config["database"]["port"] = $legacyConfig["partkeepr.database.port"]; + } + + $response["config"] = $config; + } + + /*if (file_exists($this->getConfigPath(false))) { + $yaml = new Parser(); + $data = $yaml->parse(file_get_contents($this->getConfigPath(false))); + + if (array_key_exists("parameters", $data)) { + if (array_key_exists("database_driver", $data["parameters"])) { + $config["database"]["driver"] = $this->getParameter("database_driver"); + } + + if (array_key_exists("database_driver", $data["parameters"])) { + $config["database"]["host"] = $this->getParameter("database_host"); + } + + if (array_key_exists("database_driver", $data["parameters"])) { + $config["database"]["port"] = $this->getParameter("database_port"); + } + + if (array_key_exists("database_driver", $data["parameters"])) { + $config["database"]["dbname"] = $this->getParameter("database_name"); + } + + if (array_key_exists("database_driver", $data["parameters"])) { + $config["database"]["username"] = $this->getParameter("database_user"); + } + + if (array_key_exists("database_driver", $data["parameters"])) { + $config["database"]["password"] = $this->getParameter("database_password"); + } + } + }*/ + + + + } catch (\Exception $e) { + $response["success"] = false; + $response["message"] = "Configuration parse error"; + $response["errors"] = [$e->getMessage()]; + } + + return new JsonResponse($response); + } + + protected function legacyConfigParser () { + if (file_exists($this->getLegacyAuthConfigPath())) { + $parser = new \PHPParser_Parser(new \PHPParser_Lexer()); + $traverser = new \PHPParser_NodeTraverser(); + $traverser->addVisitor(new LegacyConfigVisitor()); + $statements = $parser->parse(file_get_contents($this->getLegacyAuthConfigPath())); + $traverser->traverse($statements); + + return LegacyConfigVisitor::getConfigValues(); + } + + return array(); + } + + protected function getLegacyAuthConfigPath () { + return dirname(__FILE__)."/../../../../config.php"; + } + +} diff --git a/src/PartKeepr/SetupBundle/Controller/SetupController.php b/src/PartKeepr/SetupBundle/Controller/SetupController.php @@ -142,38 +142,38 @@ class SetupController extends Controller $parameters = array(); $parameters["database"] = array( "driver" => null, - "host" => "~", - "user" => "~", - "password" => "~", - "name" => "~", + "host" => null, + "user" => null, + "password" => null, + "name" => null, "port" => 3306, ); $parameters["mailer"] = array( - "transport" => "~", - "host" => "~", - "port" => "~", - "encryption" => "~", - "username" => "~", - "password" => "~", - "auth_mode" => "~", + "transport" => null, + "host" => null, + "port" => null, + "encryption" => null, + "username" => null, + "password" => null, + "auth_mode" => null, ); $parameters["ldap"] = array( "enabled" => "false", "host" => "127.0.0.1", - "port" => "~", - "username" => "~", - "password" => "~", - "bindrequiresdn" => "false", + "port" => null, + "username" => null, + "password" => null, + "bindrequiresdn" => false, "basedn" => "", - "ssl" => "~", - "starttls" => "~", + "ssl" => null, + "starttls" => null, "user_basedn" => "dc=blabla,dc=com", - "user_filter" => "~", - "user_username" => "~", - "user_name" => "~", - "user_email" => "~", + "user_filter" => null, + "user_username" => null, + "user_name" => null, + "user_email" => null, ); $secret = ""; @@ -194,7 +194,11 @@ class SetupController extends Controller if (array_key_exists("ldap", $data)) { $parameters["ldap"] = $this->applyIf($parameters["ldap"], $data["ldap"]); } - $contents = $this->container->get('templating')->render('PartKeeprSetupBundle::setup.html.twig', $parameters); + + array_walk_recursive($parameters, function (&$item, $key) { $item = var_export($item, true); }); + + + $contents = $this->container->get('templating')->render('PartKeeprSetupBundle::parameters.php.twig', $parameters); file_put_contents($this->getConfigPath($test), $contents); } @@ -214,11 +218,11 @@ class SetupController extends Controller return dirname(__FILE__)."/../../../../app/authkey.php"; } - private function getConfigPath ($test) { + protected function getConfigPath ($test) { if ($test) { - $filename = "parameters_setup.yml"; + $filename = "parameters_setup.php"; } else { - $filename = "parameters.yml"; + $filename = "parameters.php"; } return dirname(__FILE__)."/../../../../app/config/".$filename; } diff --git a/src/PartKeepr/SetupBundle/Resources/config/routing.yml b/src/PartKeepr/SetupBundle/Resources/config/routing.yml @@ -37,3 +37,7 @@ _setup_units: _setup_manufacturers: resource: "@PartKeeprSetupBundle/Controller/ManufacturerSetupController.php" type: annotation + +_setup_parseconfig: + resource: "@PartKeeprSetupBundle/Controller/ExistingConfigParserController.php" + type: annotation diff --git a/src/PartKeepr/SetupBundle/Resources/views/parameters.php.twig b/src/PartKeepr/SetupBundle/Resources/views/parameters.php.twig @@ -0,0 +1,40 @@ +<?php +$container->setParameter('database_driver', {{ database.driver|raw }}); +$container->setParameter('database_host', {{ database.host|raw }}); +$container->setParameter('database_port', {{ database.port|raw }}); +$container->setParameter('database_name', {{ database.name|raw }}); +$container->setParameter('database_user', {{ database.user|raw }}); +$container->setParameter('database_password', {{ database.password|raw }}); + + +$container->setParameter('mailer_transport', {{ mailer.transport|raw }}); +$container->setParameter('mailer_host', {{ mailer.host|raw }}); +$container->setParameter('mailer_port', {{ mailer.port|raw }}); +$container->setParameter('mailer_encryption', {{ mailer.encryption|raw }}); +$container->setParameter('mailer_user', {{ mailer.username|raw }}); +$container->setParameter('mailer_password', {{ mailer.password|raw }}); +$container->setParameter('mailer_auth_mode', {{ mailer.auth_mode|raw }}); + +$container->setParameter('locale', 'en'); + +$container->setParameter('secret', {{ secret|raw }}); + +$container->setParameter('fr3d_ldap.enabled', {{ ldap.enabled|raw }}); +$container->setParameter('fr3d_ldap.driver.host', {{ ldap.host|raw }}); +$container->setParameter('fr3d_ldap.driver.port', {{ ldap.port|raw }}); +$container->setParameter('fr3d_ldap.driver.username', {{ ldap.username|raw }}); +$container->setParameter('fr3d_ldap.driver.password', {{ ldap.password|raw }}); +$container->setParameter('fr3d_ldap.driver.bindRequiresDn', {{ ldap.bindrequiresdn|raw }}); +$container->setParameter('fr3d_ldap.driver.baseDn', {{ ldap.basedn|raw }}); +$container->setParameter('fr3d_ldap.driver.accountFilterFormat', null); +$container->setParameter('fr3d_ldap.driver.optReferrals', null); +$container->setParameter('fr3d_ldap.driver.useSsl', {{ ldap.ssl|raw }}); +$container->setParameter('fr3d_ldap.driver.useStartTls', {{ ldap.starttls|raw }}); +$container->setParameter('fr3d_ldap.driver.accountCanonicalForm', null); +$container->setParameter('fr3d_ldap.driver.accountDomainName', null); +$container->setParameter('fr3d_ldap.driver.accountDomainNameShort', null); +$container->setParameter('fr3d_ldap.user.baseDn', {{ ldap.user_basedn|raw }}); +$container->setParameter('fr3d_ldap.user.filter', {{ ldap.user_filter|raw }}); +$container->setParameter('fr3d_ldap.user.attribute.username', {{ ldap.user_username|raw }}); +$container->setParameter('fr3d_ldap.user.attribute.name', {{ ldap.user_name|raw }}); +$container->setParameter('fr3d_ldap.user.attribute.email', {{ ldap.user_email|raw }}); diff --git a/src/PartKeepr/SetupBundle/Resources/views/setup.html.twig b/src/PartKeepr/SetupBundle/Resources/views/setup.html.twig @@ -1,42 +0,0 @@ -parameters: - database_driver: {{ database.driver }} - database_host: {{ database.host }} - database_port: {{ database.port }} - database_name: {{ database.name }} - database_user: {{ database.user }} - database_password: {{ database.password }} - # You should uncomment this if you want use pdo_sqlite - # database_path: "%kernel.root_dir%/data.db3" - - mailer_transport: {{ mailer.transport }} - mailer_host: {{ mailer.host }} - mailer_port: {{ mailer.port }} - mailer_encryption: {{ mailer.encryption }} - mailer_user: {{ mailer.username }} - mailer_password: {{ mailer.password }} - mailer_auth_mode: {{ mailer.auth_mode }} - - locale: en - - # A secret key that's used to generate certain security-related tokens - secret: {{ secret }} - - fr3d_ldap.enabled: {{ ldap.enabled }} - fr3d_ldap.driver.host: {{ ldap.host }} - fr3d_ldap.driver.port: {{ ldap.port }} - fr3d_ldap.driver.username: {{ ldap.username }} - fr3d_ldap.driver.password: {{ ldap.password }} - fr3d_ldap.driver.bindRequiresDn: {{ ldap.bindrequiresdn }} - fr3d_ldap.driver.baseDn: {{ ldap.basedn }} - fr3d_ldap.driver.accountFilterFormat: ~ # Optional. sprintf format %s will be the username - fr3d_ldap.driver.optReferrals: ~ # Optional. - fr3d_ldap.driver.useSsl: {{ ldap.ssl }} - fr3d_ldap.driver.useStartTls: {{ ldap.starttls }} - fr3d_ldap.driver.accountCanonicalForm: ~ # Optional. ACCTNAME_FORM_BACKSLASH this is only needed if your users have to login with something like HOST\User - fr3d_ldap.driver.accountDomainName: ~ - fr3d_ldap.driver.accountDomainNameShort: ~ - fr3d_ldap.user.baseDn: {{ ldap.user_basedn }} - fr3d_ldap.user.filter: {{ ldap.user_filter }} - fr3d_ldap.user.attribute.username: {{ ldap.user_username }} - fr3d_ldap.user.attribute.name: {{ ldap.user_name }} - fr3d_ldap.user.attribute.email: {{ ldap.user_email }} diff --git a/src/PartKeepr/SetupBundle/Visitor/LegacyConfigVisitor.php b/src/PartKeepr/SetupBundle/Visitor/LegacyConfigVisitor.php @@ -0,0 +1,31 @@ +<?php +namespace PartKeepr\SetupBundle\Visitor; + + +class LegacyConfigVisitor extends \PHPParser_NodeVisitorAbstract +{ + private static $configValues = array(); + + public function enterNode(\PHPParser_Node $node) { + if ($node instanceof \PHPParser_Node_Expr_StaticCall) { + + if ($node->class->parts[0] == "Configuration" && $node->name == "setOption") { + if (array_key_exists(0, $node->args) && array_key_exists(1, $node->args)) { + + if ($node->args[1]->value instanceof \PHPParser_Node_Scalar_String) { + self::$configValues[$node->args[0]->value->value] = $node->args[1]->value->value; + } elseif ($node->args[1]->value instanceof \PHPParser_Node_Scalar_LNumber) { + self::$configValues[$node->args[0]->value->value] = $node->args[1]->value->value; + } elseif ($node->args[1]->value instanceof \PHPParser_Node_Expr_ConstFetch) { + self::$configValues[$node->args[0]->value->value] = $node->args[1]->value->name->parts[0]; + } + } + } + + } + } + + public static function getConfigValues () { + return self::$configValues; + } +} diff --git a/web/setup/index.html b/web/setup/index.html @@ -30,6 +30,7 @@ <script type="text/javascript" src="js/Cards/DatabaseConnectivityTestCard.js"></script> <script type="text/javascript" src="js/Cards/DatabaseSetupCard.js"></script> <script type="text/javascript" src="js/Cards/AuthKeyCard.js"></script> + <script type="text/javascript" src="js/Cards/ExistingConfigParserCard.js"></script> <script type="text/javascript" src="js/SetupTests/AbstractTest.js"></script> <script type="text/javascript" src="js/SetupTests/GenerateAuthKey.js"></script> @@ -37,6 +38,7 @@ <script type="text/javascript" src="js/SetupTests/PHPPrerequisitesTest.js"></script> <script type="text/javascript" src="js/SetupTests/DatabaseConnectivityTest.js"></script> <script type="text/javascript" src="js/SetupTests/PHPSettingsTest.js"></script> + <script type="text/javascript" src="js/SetupTests/ExistingConfigurationTest.js"></script> <script type="text/javascript" src="js/SetupSteps/ConfigSetup.js"></script> <script type="text/javascript" src="js/SetupSteps/WarmupCacheSetup.js"></script> diff --git a/web/setup/js/Cards/DatabaseParametersCard.MySQL.js b/web/setup/js/Cards/DatabaseParametersCard.MySQL.js @@ -15,6 +15,7 @@ Ext.define('PartKeeprSetup.DatabaseParametersCard.MySQL', { defaults: { labelWidth: 120 }, + initial: false, /** * Initializes the component and creates the various fields @@ -55,7 +56,7 @@ Ext.define('PartKeeprSetup.DatabaseParametersCard.MySQL', { this.databaseName = Ext.create("Ext.form.field.Text", { fieldLabel: 'Database Name', labelWidth: this.defaults.labelWidth, - value: PartKeeprSetup.getApplication().getSetupConfig().database.dbname + value: PartKeeprSetup.getApplication().getSetupConfig().database.name }); this.databaseName.on("change", this.onUpdateParameters, this); @@ -158,7 +159,21 @@ Ext.define('PartKeeprSetup.DatabaseParametersCard.MySQL', { this.callParent(); - this.on("activate", this.onUpdateParameters, this); + this.on("activate", this.onActivate, this); + }, + onActivate: function () { + this.initial = true; + this.hostname.setValue(PartKeeprSetup.getApplication().getSetupConfig().database.host); + this.username.setValue(PartKeeprSetup.getApplication().getSetupConfig().database.user); + this.password.setValue(PartKeeprSetup.getApplication().getSetupConfig().database.password); + this.databaseName.setValue(PartKeeprSetup.getApplication().getSetupConfig().database.name); + + if (PartKeeprSetup.getApplication().getSetupConfig().database.port) { + this.port.setValue(PartKeeprSetup.getApplication().getSetupConfig().database.port); + this.portDefault.setValue(false); + this.port.setDisabled(false); + } + this.initial = false; }, /** * This method gets fired as soon as something in the form was changed. @@ -168,6 +183,8 @@ Ext.define('PartKeeprSetup.DatabaseParametersCard.MySQL', { */ onUpdateParameters: function () { + if (this.initial) { return; } + if (this.showHintCheckbox.checked) { var host; diff --git a/web/setup/js/Cards/DatabaseParametersCard.PostgreSQL.js b/web/setup/js/Cards/DatabaseParametersCard.PostgreSQL.js @@ -192,9 +192,9 @@ Ext.define('PartKeeprSetup.DatabaseParametersCard.PostgreSQL', { database: { driver: 'pdo_pgsql', host: this.hostname.getValue(), - username: this.username.getValue(), + user: this.username.getValue(), password: this.password.getValue(), - dbname: this.databaseName.getValue(), + name: this.databaseName.getValue(), port: this.port.getValue() } }); diff --git a/web/setup/js/Cards/DatabaseParametersCard.js b/web/setup/js/Cards/DatabaseParametersCard.js @@ -107,6 +107,7 @@ Ext.define('PartKeeprSetup.DatabaseParametersCard', { */ onActivate: function () { + this.databaseDropdown.setValue(PartKeeprSetup.getApplication().getSetupConfig().database.driver); this.onDriverSelect(null, this.databaseDropdown.getValue()); // Disable the "next" button, this needs to get enabled by the database cards diff --git a/web/setup/js/Cards/ExistingConfigParserCard.js b/web/setup/js/Cards/ExistingConfigParserCard.js @@ -0,0 +1,16 @@ +/** + * This card runs the prequisites test to make sure basic things like PHP and Doctrine are installed and configured. + */ +Ext.define('PartKeeprSetup.ExistingConfigParserCard', { + extend: 'PartKeeprSetup.AbstractTestCard', + + cardMessage: "Setup now checks if a configuration exists", + breadCrumbTitle: 'Existing Configuration', + /** + * Sets up all tests + */ + setupTests: function () + { + this.tests.push(new PartKeeprSetup.ExistingConfigurationTest()); + } +}); diff --git a/web/setup/js/PartKeeprSetup.js b/web/setup/js/PartKeeprSetup.js @@ -15,8 +15,8 @@ Ext.application({ database: { driver: "", host: "", - dbname: "", - username: "", + name: "", + user: "", password: "", port: null }, diff --git a/web/setup/js/SetupTests/AbstractTest.js b/web/setup/js/SetupTests/AbstractTest.js @@ -140,6 +140,8 @@ Ext.define('PartKeeprSetup.AbstractTest', { if (this.success) { this.fireEvent("complete", this); } + + this.onAfterRunTest(obj); }, /** * Gets called prior test execution. Most tests won't use this, but some tests need to inject parameters. @@ -147,5 +149,10 @@ Ext.define('PartKeeprSetup.AbstractTest', { onBeforeRunTest: function () { this.params = PartKeeprSetup.getApplication().getSetupConfig(); + }, + onAfterRunTest: function (data) + { + } + }); diff --git a/web/setup/js/SetupTests/ExistingConfigurationTest.js b/web/setup/js/SetupTests/ExistingConfigurationTest.js @@ -0,0 +1,18 @@ +/** + * Tests if the most important PHP prerequisites are met (e.g. json_encode). + */ +Ext.define('PartKeeprSetup.ExistingConfigurationTest', { + extend: 'PartKeeprSetup.AbstractTest', + action: 'parseExistingConfig', + name: "PHP", + message: "Retrieving existing configuration", + + onAfterRunTest: function (data) { + var config = PartKeeprSetup.getApplication().getSetupConfig(); + + Ext.apply(config, data.config); + + console.log(PartKeeprSetup.getApplication().getSetupConfig()); + + } +}); diff --git a/web/setup/js/SetupWizard.js b/web/setup/js/SetupWizard.js @@ -65,10 +65,11 @@ Ext.define('PartKeeprSetup.SetupWizard', { var cardNames = [ "PartKeeprSetup.PrerequisitesTestCard", "PartKeeprSetup.AuthKeyCard", + "PartKeeprSetup.ExistingConfigParserCard", "PartKeeprSetup.DatabaseParametersCard", "PartKeeprSetup.DatabaseConnectivityTestCard", "PartKeeprSetup.AdminUserCard", - "PartKeeprSetup.DatabaseSetupCard" + "PartKeeprSetup.DatabaseSetupCard", ]; for (var card = 0; card < cardNames.length; card++) {