2017-12-14 12:28:58 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Installer\Http\Controllers;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Controller;
|
2017-12-31 10:27:12 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Repositories\AirlineRepository;
|
2018-01-30 03:16:39 +08:00
|
|
|
use App\Services\AnalyticsService;
|
2019-12-13 04:07:35 +08:00
|
|
|
use App\Services\Installer\DatabaseService;
|
|
|
|
use App\Services\Installer\InstallerService;
|
2019-04-08 07:53:24 +08:00
|
|
|
use App\Services\Installer\MigrationService;
|
2019-08-08 00:32:49 +08:00
|
|
|
use App\Services\Installer\SeederService;
|
2017-12-30 06:56:46 +08:00
|
|
|
use App\Services\UserService;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Support\Countries;
|
2020-02-12 01:32:41 +08:00
|
|
|
use App\Support\Utils;
|
2018-03-20 09:50:40 +08:00
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2019-08-08 00:32:49 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-03-20 09:50:40 +08:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2020-02-18 21:23:32 +08:00
|
|
|
use Laracasts\Flash\Flash;
|
2018-01-30 03:16:39 +08:00
|
|
|
use Modules\Installer\Services\ConfigService;
|
2017-12-15 06:38:29 +08:00
|
|
|
use Modules\Installer\Services\RequirementsService;
|
2017-12-18 06:58:53 +08:00
|
|
|
|
2018-01-12 11:35:03 +08:00
|
|
|
class InstallerController extends Controller
|
2017-12-14 12:28:58 +08:00
|
|
|
{
|
2019-08-27 00:32:46 +08:00
|
|
|
private $airlineRepo;
|
|
|
|
private $analyticsSvc;
|
|
|
|
private $dbSvc;
|
|
|
|
private $envSvc;
|
|
|
|
private $migrationSvc;
|
|
|
|
private $reqSvc;
|
|
|
|
private $seederSvc;
|
|
|
|
private $userService;
|
2018-03-20 09:50:40 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* InstallerController constructor.
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
|
|
|
* @param AirlineRepository $airlineRepo
|
|
|
|
* @param AnalyticsService $analyticsSvc
|
|
|
|
* @param DatabaseService $dbService
|
|
|
|
* @param ConfigService $envService
|
|
|
|
* @param MigrationService $migrationSvc
|
2019-08-08 00:32:49 +08:00
|
|
|
* @param RequirementsService $reqSvc
|
2019-08-27 00:32:46 +08:00
|
|
|
* @param SeederService $seederSvc
|
|
|
|
* @param UserService $userService
|
2018-03-20 09:50:40 +08:00
|
|
|
*/
|
2017-12-15 06:38:29 +08:00
|
|
|
public function __construct(
|
2017-12-30 06:56:46 +08:00
|
|
|
AirlineRepository $airlineRepo,
|
2018-01-30 03:16:39 +08:00
|
|
|
AnalyticsService $analyticsSvc,
|
2017-12-15 06:38:29 +08:00
|
|
|
DatabaseService $dbService,
|
2018-01-30 03:16:39 +08:00
|
|
|
ConfigService $envService,
|
2018-02-06 06:16:24 +08:00
|
|
|
MigrationService $migrationSvc,
|
2019-08-08 00:32:49 +08:00
|
|
|
RequirementsService $reqSvc,
|
|
|
|
SeederService $seederSvc,
|
2017-12-30 06:56:46 +08:00
|
|
|
UserService $userService
|
2017-12-15 06:38:29 +08:00
|
|
|
) {
|
2017-12-30 06:56:46 +08:00
|
|
|
$this->airlineRepo = $airlineRepo;
|
2018-01-30 03:16:39 +08:00
|
|
|
$this->analyticsSvc = $analyticsSvc;
|
2019-08-08 00:32:49 +08:00
|
|
|
$this->dbSvc = $dbService;
|
|
|
|
$this->envSvc = $envService;
|
2018-02-06 06:16:24 +08:00
|
|
|
$this->migrationSvc = $migrationSvc;
|
2019-08-08 00:32:49 +08:00
|
|
|
$this->reqSvc = $reqSvc;
|
|
|
|
$this->seederSvc = $seederSvc;
|
2017-12-30 06:56:46 +08:00
|
|
|
$this->userService = $userService;
|
2019-12-12 01:57:18 +08:00
|
|
|
|
2020-02-29 07:00:56 +08:00
|
|
|
Utils::disableDebugToolbar();
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
2019-08-27 00:32:46 +08:00
|
|
|
|
2017-12-14 12:28:58 +08:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2019-08-27 00:32:46 +08:00
|
|
|
if (config('app.key') !== 'base64:zdgcDqu9PM8uGWCtMxd74ZqdGJIrnw812oRMmwDF6KY=') {
|
2017-12-18 01:29:11 +08:00
|
|
|
return view('installer::errors/already-installed');
|
2017-12-25 02:27:52 +08:00
|
|
|
}
|
2017-12-18 01:29:11 +08:00
|
|
|
|
2018-02-06 06:16:24 +08:00
|
|
|
return view('installer::install/index-start');
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
|
|
|
|
2017-12-25 02:10:31 +08:00
|
|
|
protected function testDb(Request $request)
|
|
|
|
{
|
2019-08-08 00:32:49 +08:00
|
|
|
$this->dbSvc->checkDbConnection(
|
2018-01-30 03:16:39 +08:00
|
|
|
$request->post('db_conn'),
|
|
|
|
$request->post('db_host'),
|
|
|
|
$request->post('db_port'),
|
|
|
|
$request->post('db_name'),
|
|
|
|
$request->post('db_user'),
|
|
|
|
$request->post('db_pass')
|
2017-12-25 02:10:31 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
|
|
|
* Check the database connection
|
|
|
|
*/
|
|
|
|
public function dbtest(Request $request)
|
|
|
|
{
|
2019-08-27 00:32:46 +08:00
|
|
|
$status = 'success'; // success|warn|danger
|
2017-12-15 06:38:29 +08:00
|
|
|
$message = 'Database connection looks good!';
|
|
|
|
|
|
|
|
try {
|
2017-12-25 02:10:31 +08:00
|
|
|
$this->testDb($request);
|
2017-12-15 06:38:29 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$status = 'danger';
|
2019-08-27 00:32:46 +08:00
|
|
|
$message = 'Failed! '.$e->getMessage();
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
return view('installer::install/dbtest', [
|
2019-08-27 00:32:46 +08:00
|
|
|
'status' => $status,
|
2017-12-15 06:38:29 +08:00
|
|
|
'message' => $message,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-16 00:41:52 +08:00
|
|
|
/**
|
|
|
|
* Check if any of the items has been marked as failed
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-16 00:41:52 +08:00
|
|
|
* @param array $arr
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-16 00:41:52 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function allPassed(array $arr): bool
|
|
|
|
{
|
2019-08-27 00:32:46 +08:00
|
|
|
foreach ($arr as $item) {
|
|
|
|
if ($item['passed'] === false) {
|
2017-12-16 00:41:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
|
|
|
* Step 1. Check the modules and permissions
|
2019-06-21 04:52:37 +08:00
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2017-12-15 06:38:29 +08:00
|
|
|
*/
|
|
|
|
public function step1(Request $request)
|
|
|
|
{
|
2019-08-08 00:32:49 +08:00
|
|
|
$php_version = $this->reqSvc->checkPHPVersion();
|
|
|
|
$extensions = $this->reqSvc->checkExtensions();
|
|
|
|
$directories = $this->reqSvc->checkPermissions();
|
2017-12-16 00:41:52 +08:00
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
// Only pass if all the items in the ext and dirs are passed
|
2017-12-16 00:41:52 +08:00
|
|
|
$statuses = [
|
|
|
|
$php_version['passed'] === true,
|
|
|
|
$this->allPassed($extensions) === true,
|
2019-08-27 00:32:46 +08:00
|
|
|
$this->allPassed($directories) === true,
|
2017-12-16 00:41:52 +08:00
|
|
|
];
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
// Make sure there are no false values
|
2018-01-19 10:40:25 +08:00
|
|
|
$passed = !\in_array(false, $statuses, true);
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2018-02-06 06:16:24 +08:00
|
|
|
return view('installer::install/steps/step1-requirements', [
|
2019-08-27 00:32:46 +08:00
|
|
|
'php' => $php_version,
|
|
|
|
'extensions' => $extensions,
|
2017-12-16 00:41:52 +08:00
|
|
|
'directories' => $directories,
|
2019-08-27 00:32:46 +08:00
|
|
|
'passed' => $passed,
|
2017-12-15 06:38:29 +08:00
|
|
|
]);
|
2017-12-14 12:28:58 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
|
|
|
* Step 2. Database Setup
|
2019-06-21 04:52:37 +08:00
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2017-12-15 06:38:29 +08:00
|
|
|
*/
|
|
|
|
public function step2(Request $request)
|
|
|
|
{
|
|
|
|
$db_types = ['mysql' => 'mysql', 'sqlite' => 'sqlite'];
|
2018-02-06 06:16:24 +08:00
|
|
|
return view('installer::install/steps/step2-db', [
|
2017-12-15 06:38:29 +08:00
|
|
|
'db_types' => $db_types,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-15 22:49:01 +08:00
|
|
|
* Step 2a. Create the .env
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-19 10:40:25 +08:00
|
|
|
* @param Request $request
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-19 10:40:25 +08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2017-12-15 06:38:29 +08:00
|
|
|
*/
|
2017-12-15 22:49:01 +08:00
|
|
|
public function envsetup(Request $request)
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
2018-02-26 05:13:04 +08:00
|
|
|
$log_str = $request->post();
|
2018-02-26 05:27:20 +08:00
|
|
|
$log_str['db_pass'] = '';
|
|
|
|
|
2018-02-26 05:13:04 +08:00
|
|
|
Log::info('ENV setup', $log_str);
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2017-12-25 02:10:31 +08:00
|
|
|
// Before writing out the env file, test the DB credentials
|
|
|
|
try {
|
|
|
|
$this->testDb($request);
|
|
|
|
} catch (\Exception $e) {
|
2018-02-26 05:27:20 +08:00
|
|
|
Log::error('Testing db before writing configs failed');
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
|
2020-02-18 21:23:32 +08:00
|
|
|
Flash::error($e->getMessage());
|
2017-12-25 02:10:31 +08:00
|
|
|
return redirect(route('installer.step2'))->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now write out the env file
|
2018-01-30 03:16:39 +08:00
|
|
|
$attrs = [
|
|
|
|
'SITE_NAME' => $request->post('site_name'),
|
2019-06-21 04:52:37 +08:00
|
|
|
'SITE_URL' => $request->post('site_url'),
|
|
|
|
'DB_CONN' => $request->post('db_conn'),
|
|
|
|
'DB_HOST' => $request->post('db_host'),
|
|
|
|
'DB_PORT' => $request->post('db_port'),
|
|
|
|
'DB_NAME' => $request->post('db_name'),
|
|
|
|
'DB_USER' => $request->post('db_user'),
|
|
|
|
'DB_PASS' => $request->post('db_pass'),
|
2018-01-30 03:16:39 +08:00
|
|
|
'DB_PREFIX' => $request->post('db_prefix'),
|
|
|
|
];
|
2017-12-25 02:10:31 +08:00
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
/*
|
2018-01-30 03:16:39 +08:00
|
|
|
* Create the config files and then redirect so that the
|
|
|
|
* framework can pickup all those configs, etc, before we
|
|
|
|
* setup the database and stuff
|
|
|
|
*/
|
2017-12-18 06:58:53 +08:00
|
|
|
try {
|
2019-08-08 00:32:49 +08:00
|
|
|
$this->envSvc->createConfigFiles($attrs);
|
2020-01-16 02:57:56 +08:00
|
|
|
} catch (\Exception $e) {
|
2018-02-26 05:27:20 +08:00
|
|
|
Log::error('Config files failed to write');
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
|
2020-02-18 21:23:32 +08:00
|
|
|
Flash::error($e->getMessage());
|
2017-12-25 02:10:31 +08:00
|
|
|
return redirect(route('installer.step2'))->withInput();
|
2017-12-18 06:58:53 +08:00
|
|
|
}
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
// Needs to redirect so it can load the new .env
|
2017-12-15 22:49:01 +08:00
|
|
|
Log::info('Redirecting to database setup');
|
|
|
|
return redirect(route('installer.dbsetup'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Step 2b. Setup the database
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-19 10:40:25 +08:00
|
|
|
* @param Request $request
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2020-02-18 21:23:32 +08:00
|
|
|
* @return mixed
|
2017-12-15 22:49:01 +08:00
|
|
|
*/
|
|
|
|
public function dbsetup(Request $request)
|
|
|
|
{
|
2017-12-30 06:56:46 +08:00
|
|
|
$console_out = '';
|
|
|
|
|
2017-12-16 00:41:52 +08:00
|
|
|
try {
|
2019-08-08 00:32:49 +08:00
|
|
|
$console_out .= $this->dbSvc->setupDB();
|
2018-02-06 06:16:24 +08:00
|
|
|
$console_out .= $this->migrationSvc->runAllMigrations();
|
2019-08-08 00:32:49 +08:00
|
|
|
$this->seederSvc->syncAllSeeds();
|
2019-08-27 00:32:46 +08:00
|
|
|
} catch (QueryException $e) {
|
|
|
|
Log::error('Error on db setup: '.$e->getMessage());
|
2020-02-18 21:23:32 +08:00
|
|
|
dd($e);
|
2019-08-08 00:32:49 +08:00
|
|
|
$this->envSvc->removeConfigFiles();
|
2020-02-18 21:23:32 +08:00
|
|
|
Flash::error($e->getMessage());
|
2017-12-25 02:10:31 +08:00
|
|
|
return redirect(route('installer.step2'))->withInput();
|
2017-12-16 00:41:52 +08:00
|
|
|
}
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2017-12-30 06:56:46 +08:00
|
|
|
$console_out = trim($console_out);
|
|
|
|
|
2018-02-06 06:16:24 +08:00
|
|
|
return view('installer::install/steps/step2a-db_output', [
|
2019-08-27 00:32:46 +08:00
|
|
|
'console_output' => $console_out,
|
2017-12-15 11:59:54 +08:00
|
|
|
]);
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Step 3. Setup the admin user and initial settings
|
|
|
|
*/
|
|
|
|
public function step3(Request $request)
|
|
|
|
{
|
2018-02-13 00:51:04 +08:00
|
|
|
return view('installer::install/steps/step3-user', [
|
|
|
|
'countries' => Countries::getSelectList(),
|
|
|
|
]);
|
2017-12-30 06:56:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Step 3 submit
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-30 06:56:46 +08:00
|
|
|
* @param Request $request
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-19 10:40:25 +08:00
|
|
|
* @throws \RuntimeException
|
2017-12-30 06:56:46 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 02:50:08 +08:00
|
|
|
* @throws \Exception
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2020-02-24 06:21:26 +08:00
|
|
|
* @return mixed
|
2017-12-30 06:56:46 +08:00
|
|
|
*/
|
|
|
|
public function usersetup(Request $request)
|
|
|
|
{
|
|
|
|
$validator = Validator::make($request->all(), [
|
2019-06-21 04:52:37 +08:00
|
|
|
'airline_name' => 'required',
|
2019-08-09 01:42:43 +08:00
|
|
|
'airline_icao' => 'required|size:3|unique:airlines,icao',
|
2018-02-13 00:51:04 +08:00
|
|
|
'airline_country' => 'required',
|
2019-06-21 04:52:37 +08:00
|
|
|
'name' => 'required',
|
|
|
|
'email' => 'required|email|unique:users,email',
|
2019-08-27 00:32:46 +08:00
|
|
|
'password' => 'required|confirmed',
|
2017-12-30 06:56:46 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return redirect('install/step3')
|
|
|
|
->withErrors($validator)
|
|
|
|
->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the first airline
|
|
|
|
*/
|
|
|
|
$attrs = [
|
2019-06-21 04:52:37 +08:00
|
|
|
'icao' => $request->get('airline_icao'),
|
|
|
|
'name' => $request->get('airline_name'),
|
2018-02-13 00:51:04 +08:00
|
|
|
'country' => $request->get('airline_country'),
|
2017-12-30 06:56:46 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
$airline = $this->airlineRepo->create($attrs);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the user, and associate to the airline
|
|
|
|
* Ensure the seed data at least has one airport
|
|
|
|
* KAUS, for giggles, though.
|
|
|
|
*/
|
|
|
|
$attrs = [
|
2019-08-27 00:32:46 +08:00
|
|
|
'name' => $request->get('name'),
|
|
|
|
'email' => $request->get('email'),
|
|
|
|
'api_key' => Utils::generateApiKey(),
|
|
|
|
'airline_id' => $airline->id,
|
|
|
|
'password' => Hash::make($request->get('password')),
|
2017-12-30 06:56:46 +08:00
|
|
|
];
|
|
|
|
|
2020-02-24 06:21:26 +08:00
|
|
|
$user = $this->userService->createUser($attrs, ['admin']);
|
2017-12-30 06:56:46 +08:00
|
|
|
Log::info('User registered: ', $user->toArray());
|
|
|
|
|
2019-10-30 01:07:53 +08:00
|
|
|
// Set the initial admin e-mail address
|
|
|
|
setting_save('general.admin_email', $user->email);
|
2017-12-31 10:27:12 +08:00
|
|
|
|
2019-10-30 01:07:53 +08:00
|
|
|
// Save telemetry setting
|
|
|
|
setting_save('general.telemetry', get_truth_state($request->get('telemetry')));
|
|
|
|
|
|
|
|
// Try sending telemetry info
|
2018-01-30 03:16:39 +08:00
|
|
|
$this->analyticsSvc->sendInstall();
|
2018-01-19 10:40:25 +08:00
|
|
|
|
2018-02-06 06:16:24 +08:00
|
|
|
return view('installer::install/steps/step3a-completed', []);
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
2017-12-15 11:59:54 +08:00
|
|
|
|
2017-12-30 06:56:46 +08:00
|
|
|
/**
|
|
|
|
* Final step
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-30 06:56:46 +08:00
|
|
|
* @param Request $request
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-30 06:56:46 +08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
2017-12-15 11:59:54 +08:00
|
|
|
public function complete(Request $request)
|
|
|
|
{
|
2019-12-13 04:07:35 +08:00
|
|
|
$installerSvc = app(InstallerService::class);
|
|
|
|
$installerSvc->disableInstallerModules();
|
|
|
|
|
2017-12-30 06:56:46 +08:00
|
|
|
return redirect('/login');
|
2017-12-15 11:59:54 +08:00
|
|
|
}
|
2017-12-14 12:28:58 +08:00
|
|
|
}
|