[BUGFIX] Harden database select step 67/47967/4
authorHelmut Hummel <info@helhum.io>
Fri, 29 Apr 2016 10:50:38 +0000 (12:50 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Sat, 7 May 2016 09:36:18 +0000 (11:36 +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/47967
Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org>
Tested-by: Helmut Hummel <helmut.hummel@typo3.org>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/install/Classes/Controller/Action/Step/DatabaseSelect.php

index 43665b5..723b342 100644 (file)
@@ -71,10 +71,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 = $this->objectManager->get(\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 = $this->objectManager->get(\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/database'] = $postValues['existing'];
             }
         } else {