[BUGFIX] Harden database select step 50/47950/2
authorHelmut Hummel <info@helhum.io>
Thu, 28 Apr 2016 10:36:54 +0000 (12:36 +0200)
committerJigal van Hemert <jigal.van.hemert@typo3.org>
Fri, 29 Apr 2016 09:47:43 +0000 (11:47 +0200)
Currently this step does not do precautions, when
the selected database does not exist. Additionally the step
silently skips when the chosen database already has tables.

Although the UI currently does not allow to select a non
empty database, there are situations imaginable, where the database
is empty during generation of the input form but is filled directly after that,
or a database was empty but then got deleted,
which previously ended up with a fatal error.

This change now takes care of both cases and outputs a nice error message accordingly.

Resolves: #75964
Releases: 7.6, master
Change-Id: Ibc7083aa120f420d6201e512f3f41699f55c5f7f
Reviewed-on: https://review.typo3.org/47950
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
typo3/sysext/install/Classes/Controller/Action/Step/DatabaseSelect.php

index 3995d39..ba19393 100644 (file)
@@ -74,10 +74,29 @@ class DatabaseSelect extends AbstractStepAction
         } elseif ($postValues['type'] === 'existing' && !empty($postValues['existing'])) {
             // Only store database information when it's empty
             $this->databaseConnection->setDatabaseName($postValues['existing']);
-            $this->databaseConnection->sql_select_db();
-            $existingTables = $this->databaseConnection->admin_get_tables();
+            try {
+                $this->databaseConnection->sql_select_db();
+                $existingTables = $this->databaseConnection->admin_get_tables();
+                if (!empty($existingTables)) {
+                    $errorStatus = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Status\ErrorStatus::class);
+                    $errorStatus->setTitle('Selected database is not empty!');
+                    $errorStatus->setMessage(
+                        sprintf('Cannot use database "%s"', $postValues['existing'])
+                        . ', because it has tables already. Please select a different database or choose to create one!'
+                    );
+                    $result[] = $errorStatus;
+                }
+            } catch (\RuntimeException $e) {
+                $errorStatus = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Status\ErrorStatus::class);
+                $errorStatus->setTitle('Could not connect to selected database!');
+                $errorStatus->setMessage(
+                    sprintf('Could not connect to database "%s"', $postValues['existing'])
+                    . '! Make sure it really exists and your database user has the permissions to select it!'
+                );
+                $result[] = $errorStatus;
+            }
             $isInitialInstallation = $configurationManager->getConfigurationValueByPath('SYS/isInitialInstallationInProgress');
-            if (!$isInitialInstallation || empty($existingTables)) {
+            if (!$isInitialInstallation || empty($result)) {
                 $localConfigurationPathValuePairs['DB/Connections/Default/dbname'] = $postValues['existing'];
             }
             // check if database charset is utf-8