Work with setup (#3555)

* Remove StartupCheck from Kernel.php

* Real-time database check

* Catch the Exception with DB::getPDO

* Send test email - feature

* Forms

Co-authored-by: David Bomba <turbo124@gmail.com>
This commit is contained in:
Benjamin Beganović 2020-03-28 04:46:50 +01:00 committed by GitHub
parent 9cb7996a2e
commit de9faa9bc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 154 additions and 47 deletions

View File

@ -11,6 +11,7 @@
namespace App\Http\Controllers;
use App\Http\Requests\Setup\CheckDatabaseRequest;
use App\Http\Requests\Setup\StoreSetupRequest;
use App\Jobs\Account\CreateAccount;
use App\Models\Account;
@ -89,7 +90,7 @@ class SetupController extends Controller
$account = CreateAccount::dispatchNow($request->all());
}
return view('index.index');
return redirect('/');
}
/**
@ -97,17 +98,17 @@ class SetupController extends Controller
*
* @return Response
*/
public function checkDB(): Response
public function checkDB(CheckDatabaseRequest $request): Response
{
if (Account::count() == 0) {
if (Account::count() == 0) { /** This may not work, because we don't have 'account's table. */
}
// test db - > /setup/check_db (POST) please send array of DB variables - response 200/success or 400 [message]
// test mail -> /setup/check_mail (POST) please send array of MAIL xvariables - response 200/success or 400 [message]
$status = SystemHealth::dbCheck($request);
$randomStatus = rand(0, 1);
info($status);
if ($randomStatus) {
if (gettype($status) == 'array' && $status['success'] === true) {
return response([], 200);
}
@ -121,18 +122,20 @@ class SetupController extends Controller
*/
public function checkMail(): Response
{
if (Account::count() == 0) {
}
// if (Account::count() == 0) {} /** This may not work, because we don't have 'account's table. */
// test db - > /setup/check_db (POST) please send array of DB variables - response 200/success or 400 [message]
// test mail -> /setup/check_mail (POST) please send array of MAIL variables - response 200/success or 400 [message]
try {
SystemHealth::testMailServer();
$randomStatus = rand(0, 1);
if ($randomStatus) {
return response([], 200);
}
} catch (\Exception $e) {
info(['action' => 'SetupController::checkMail()', 'message' => $e->getMessage(),]);
return response([], 400);
return response([], 400);
}
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Http\Requests\Setup;
use Illuminate\Foundation\Http\FormRequest;
class CheckDatabaseRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true; /** Return something that will check if setup has been completed, like Ninja::hasCompletedSetup() */
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'host' => ['required'],
'database' => ['required'],
'username' => ['required'],
];
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Http\Requests\Setup;
use Illuminate\Foundation\Http\FormRequest;
class CheckMailRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true; /** Return something that will check if setup has been completed, like Ninja::hasCompletedSetup() */
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'driver' => ['required', 'in:smtp,mail,sendmail'],
'from_name' => ['required'],
'from_address' => ['required'],
'username' => ['required'],
'host' => ['required'],
'port' => ['required'],
'encryption' => ['required'],
'password' => ['required'],
];
}
}

View File

@ -32,8 +32,6 @@ class StoreSetupRequest extends Request
return [
/*System*/
'url' => 'required',
'debug' => 'required',
'https' => 'required',
/*Database*/
'host' => 'required',
'database' => 'required',

View File

@ -11,6 +11,8 @@
namespace App\Utils;
use App\Http\Requests\Setup\CheckDatabaseRequest;
use App\Http\Requests\Setup\CheckMailRequest;
use App\Libraries\MultiDB;
use App\Mail\TestMailServer;
use Illuminate\Support\Facades\DB;
@ -72,28 +74,40 @@ class SystemHealth
return $loaded_extensions;
}
private static function dbCheck() :array
public static function dbCheck($request = null): array
{
$result = [];
$result = ['success' => false];
if ($request && $request instanceof CheckDatabaseRequest) {
config(['database.connections.db-ninja-01.host'=> $request->input('host')]);
config(['database.connections.db-ninja-01.database'=> $request->input('database')]);
config(['database.connections.db-ninja-01.username'=> $request->input('username')]);
config(['database.connections.db-ninja-01.password'=> $request->input('password')]);
config(['database.default' => 'db-ninja-01']);
DB::purge('db-ninja-01');
}
if (! config('ninja.db.multi_db_enabled')) {
$pdo = DB::connection()->getPdo();
if ($pdo) {
try {
$pdo = DB::connection()->getPdo();
$result[] = [ DB::connection()->getDatabaseName() => true ];
} else {
$result['success'] = true;
} catch (\Exception $e) {
$result[] = [ config('database.connections.' . config('database.default') . '.database') => false ];
$result['success'] = false;
}
} else {
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
$pdo = DB::connection()->getPdo();
if ($pdo) {
try {
$pdo = DB::connection()->getPdo();
$result[] = [ DB::connection()->getDatabaseName() => true ];
} else {
$result['success'] = true;
} catch (\Exception $e) {
$result[] = [ config('database.connections.' . config('database.default') . '.database') => false ];
$result['success'] = false;
}
}
}
@ -106,8 +120,19 @@ class SystemHealth
return DB::connection()->getPdo();
}
private static function testMailServer()
public static function testMailServer($request = null)
{
if ($request && $request instanceof CheckMailRequest) {
config(['mail.driver' => $request->input('driver')]);
config(['mail.host' => $request->input('host')]);
config(['mail.port' => $request->input('port')]);
config(['mail.from.address' => $request->input('from_address')]);
config(['mail.from.name' => $request->input('from_name')]);
config(['mail.encryption' => $request->input('encryption')]);
config(['mail.username' => $request->input('username')]);
config(['mail.password' => $request->input('password')]);
}
try {
Mail::to(config('mail.from.address'))
->send(new TestMailServer('Email Server Works!', config('mail.from.address')));

View File

@ -14,7 +14,7 @@
{{ ctrans('texts.first_name') }}
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" class="input" name="first_name">
<input type="text" class="input" name="first_name" value="{{ old('first_name') }}">
</dd>
</div>
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
@ -22,7 +22,7 @@
{{ ctrans('texts.last_name') }}
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" class="input" name="last_name">
<input type="text" class="input" name="last_name" value="{{ old('last_name') }}">
</dd>
</div>
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
@ -30,7 +30,7 @@
{{ ctrans('texts.email') }}
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="email" class="input" name="email">
<input type="email" class="input" name="email" value="{{ old('email') }}">
</dd>
</div>
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">

View File

@ -14,7 +14,7 @@
{{ ctrans('texts.url') }}*
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" class="input" name="url" required>
<input type="text" class="input" name="url" required value="{{ old('url') }}">
</dd>
</div>
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
@ -22,7 +22,7 @@
{{ ctrans('texts.https') }}
</dt>
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="checkbox" class="form-checkbox mr-1" name="https">
<input type="checkbox" class="form-checkbox mr-1" name="https" {{ old('https') ? 'checked': '' }}>
<span>{{ ctrans('texts.require') }}</span>
</dd>
</div>
@ -31,7 +31,7 @@
{{ ctrans('texts.debug') }}
</dt>
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="checkbox" class="form-checkbox mr-1" name="debug">
<input type="checkbox" class="form-checkbox mr-1" name="debug" {{ old('debug') ? 'checked': '' }}>
<span>{{ ctrans('texts.enable') }}</span>
</dd>
</div>
@ -40,7 +40,7 @@
{{ ctrans('texts.reports') }}
</dt>
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="checkbox" class="form-checkbox mr-1" name="send_logs">
<input type="checkbox" class="form-checkbox mr-1" name="send_logs" {{ old('send_logs' ? 'checked': '') }}>
<span>{{ ctrans('texts.send_fail_logs_to_our_server') }}</span>
</dd>
</div>

View File

@ -39,7 +39,7 @@ FLUSH PRIVILEGES;
{{ ctrans('texts.host') }}*
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" class="input" name="host" required>
<input type="text" class="input" name="host" required value="{{ old('host') }}">
</dd>
</div>
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
@ -47,15 +47,15 @@ FLUSH PRIVILEGES;
{{ ctrans('texts.database') }}*
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" class="input" name="database" required>
<input type="text" class="input" name="database" required value="{{ old('database') }}">
</dd>
</div>
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
<dt class="text-sm leading-5 font-medium text-gray-500">
<dt class="text-sm leading-5 font-medium text-gray-500" value="{{ old('username') }}">
{{ ctrans('texts.username') }}*
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" class="input" name="db_username" required>
<input type="text" class="input" name="db_username" required value="{{ old('db_username') }}">
</dd>
</div>
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
@ -63,7 +63,7 @@ FLUSH PRIVILEGES;
{{ ctrans('texts.password') }}
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="password" class="input" name="db_password">
<input type="password" class="input" name="db_password" value="{{ old('db_password') }}">
</dd>
</div>
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">

View File

@ -15,7 +15,9 @@
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<select name="mail_driver" class="input form-select">
<option value="1">SMTP</option>
<option value="smtp">SMTP</option>
<option value="mail">Mail</option>
<option value="sendmail">Sendmail</option>
</select>
</dd>
</div>
@ -24,7 +26,7 @@
{{ ctrans('texts.from_name') }}
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" class="input" name="mail_name">
<input type="text" class="input" name="mail_name" value="{{ old('mail_name') }}">
</dd>
</div>
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
@ -32,7 +34,7 @@
{{ ctrans('texts.from_address') }}
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="email" class="input" name="mail_address">
<input type="email" class="input" name="mail_address" value="{{ old('mail_address') }}">
</dd>
</div>
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
@ -40,7 +42,7 @@
{{ ctrans('texts.username') }}
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" class="input" name="mail_username">
<input type="text" class="input" name="mail_username" value="{{ old('mail_username') }}">
</dd>
</div>
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
@ -48,7 +50,7 @@
{{ ctrans('texts.host') }}
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" class="input" name="mail_host">
<input type="text" class="input" name="mail_host" value="{{ old('mail_host') }}">
</dd>
</div>
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
@ -56,7 +58,7 @@
{{ ctrans('texts.port') }}
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" class="input" name="mail_port">
<input type="text" class="input" name="mail_port" value="{{ old('mail_port') }}">
</dd>
</div>
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
@ -65,8 +67,8 @@
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<select name="encryption" class="input form-select">
<option value="1">TLS</option>
<option value="2">SSL</option>
<option value="tls">TLS</option>
<option value="ssl">SSL</option>
</select>
</dd>
</div>
@ -81,7 +83,7 @@
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 sm:flex sm:items-center">
<dt class="text-sm leading-5 font-medium text-gray-500">
<button type="button" class="button button-primary py-2 px-3 text-xs" id="test-smtp-connection">
{{ ctrans('texts.test_connection') }}
{{ ctrans('texts.send_test_email') }}
</button>
</dt>
<dd class="text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">

View File

@ -19,6 +19,16 @@
or email us at <a href="mailto:contact@invoiceninja.com" class="button-link">contact@invoiceninja.com</a>.
</p>
@if($errors->any())
<div class="alert alert-failure">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@include('setup._application')
@include('setup._database')
@include('setup._mail')