Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #162 from UksusoFF/master
Browse files Browse the repository at this point in the history
Add database connection validation before save environment
  • Loading branch information
rashidlaasri authored Aug 12, 2019
2 parents ad297db + a89bd97 commit 01c24c5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/Controllers/EnvironmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace RachidLaasri\LaravelInstaller\Controllers;

use Exception;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\DB;
use RachidLaasri\LaravelInstaller\Helpers\EnvironmentManager;
use RachidLaasri\LaravelInstaller\Events\EnvironmentSaved;
use Validator;
Expand Down Expand Up @@ -92,8 +94,13 @@ public function saveWizard(Request $request, Redirector $redirect)
$validator = Validator::make($request->all(), $rules, $messages);

if ($validator->fails()) {
$errors = $validator->errors();
return view('vendor.installer.environment-wizard', compact('errors', 'envConfig'));
return $redirect->route('LaravelInstaller::environmentWizard')->withInput()->withErrors($validator->errors());
}

if (!$this->checkDatabaseConnection($request)) {
return $redirect->route('LaravelInstaller::environmentWizard')->withInput()->withErrors([
'database_connection' => trans('installer_messages.environment.wizard.form.db_connection_failed'),
]);
}

$results = $this->EnvironmentManager->saveFileWizard($request);
Expand All @@ -103,4 +110,41 @@ public function saveWizard(Request $request, Redirector $redirect)
return $redirect->route('LaravelInstaller::database')
->with(['results' => $results]);
}

/**
* TODO: We can remove this code if PR will be merged: https://github.com/RachidLaasri/LaravelInstaller/pull/162
* Validate database connection with user credentials (Form Wizard).
*
* @param Request $request
* @return boolean
*/
private function checkDatabaseConnection(Request $request)
{
$connection = $request->input('database_connection');

$settings = config("database.connections.$connection");

config([
'database' => [
'default' => $connection,
'connections' => [
$connection => array_merge($settings, [
'driver' => $connection,
'host' => $request->input('database_hostname'),
'port' => $request->input('database_port'),
'database' => $request->input('database_name'),
'username' => $request->input('database_username'),
'password' => $request->input('database_password'),
]),
],
],
]);

try {
DB::connection()->getPdo();
return true;
} catch (Exception $e) {
return false;
}
}
}
1 change: 1 addition & 0 deletions src/Lang/en/installer_messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
'app_log_level_label_emergency' => 'emergency',
'app_url_label' => 'App Url',
'app_url_placeholder' => 'App Url',
'db_connection_failed' => 'Could not connect to the database.',
'db_connection_label' => 'Database Connection',
'db_connection_label_mysql' => 'mysql',
'db_connection_label_sqlite' => 'sqlite',
Expand Down

0 comments on commit 01c24c5

Please sign in to comment.