2017-12-14 12:28:58 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Installer\Http\Controllers;
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
use Log;
|
2017-12-14 12:28:58 +08:00
|
|
|
use Illuminate\Http\Request;
|
2017-12-31 10:27:12 +08:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2017-12-30 06:56:46 +08:00
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
2017-12-14 12:28:58 +08:00
|
|
|
|
2017-12-31 10:27:12 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Repositories\AirlineRepository;
|
2017-12-30 06:56:46 +08:00
|
|
|
use App\Facades\Utils;
|
|
|
|
use App\Services\UserService;
|
2018-01-12 11:35:03 +08:00
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2017-12-15 06:38:29 +08:00
|
|
|
|
|
|
|
use Modules\Installer\Services\DatabaseService;
|
|
|
|
use Modules\Installer\Services\EnvironmentService;
|
|
|
|
use Modules\Installer\Services\RequirementsService;
|
|
|
|
|
2017-12-18 06:58:53 +08:00
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
|
|
|
|
2018-01-12 11:35:03 +08:00
|
|
|
class InstallerController extends Controller
|
2017-12-14 12:28:58 +08:00
|
|
|
{
|
2017-12-30 06:56:46 +08:00
|
|
|
protected $airlineRepo,
|
|
|
|
$dbService,
|
|
|
|
$envService,
|
|
|
|
$reqService,
|
|
|
|
$userService;
|
2017-12-15 06:38:29 +08:00
|
|
|
|
|
|
|
public function __construct(
|
2017-12-30 06:56:46 +08:00
|
|
|
AirlineRepository $airlineRepo,
|
2017-12-15 06:38:29 +08:00
|
|
|
DatabaseService $dbService,
|
|
|
|
EnvironmentService $envService,
|
2017-12-30 06:56:46 +08:00
|
|
|
RequirementsService $reqService,
|
|
|
|
UserService $userService
|
2017-12-15 06:38:29 +08:00
|
|
|
) {
|
2017-12-30 06:56:46 +08:00
|
|
|
$this->airlineRepo = $airlineRepo;
|
2017-12-15 06:38:29 +08:00
|
|
|
$this->dbService = $dbService;
|
|
|
|
$this->envService = $envService;
|
|
|
|
$this->reqService = $reqService;
|
2017-12-30 06:56:46 +08:00
|
|
|
$this->userService = $userService;
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
2017-12-14 12:28:58 +08:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2017-12-25 02:27:52 +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
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
return view('installer::index-start');
|
|
|
|
}
|
|
|
|
|
2017-12-25 02:10:31 +08:00
|
|
|
protected function testDb(Request $request)
|
|
|
|
{
|
|
|
|
$this->dbService->checkDbConnection(
|
|
|
|
$request->input('db_conn'),
|
|
|
|
$request->input('db_host'),
|
|
|
|
$request->input('db_port'),
|
|
|
|
$request->input('db_name'),
|
|
|
|
$request->input('db_user'),
|
|
|
|
$request->input('db_pass')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
|
|
|
* Check the database connection
|
|
|
|
*/
|
|
|
|
public function dbtest(Request $request)
|
|
|
|
{
|
|
|
|
$status = 'success'; # success|warn|danger
|
|
|
|
$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';
|
|
|
|
$message = 'Failed! ' . $e->getMessage();
|
|
|
|
}
|
|
|
|
|
2017-12-16 01:22:46 +08:00
|
|
|
return view('installer::flash/dbtest', [
|
2017-12-15 06:38:29 +08:00
|
|
|
'status' => $status,
|
|
|
|
'message' => $message,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-16 00:41:52 +08:00
|
|
|
/**
|
|
|
|
* Check if any of the items has been marked as failed
|
|
|
|
* @param array $arr
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function allPassed(array $arr): bool
|
|
|
|
{
|
|
|
|
foreach($arr as $item) {
|
|
|
|
if($item['passed'] === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
|
|
|
* Step 1. Check the modules and permissions
|
|
|
|
*/
|
|
|
|
public function step1(Request $request)
|
|
|
|
{
|
|
|
|
$php_version = $this->reqService->checkPHPVersion();
|
|
|
|
$extensions = $this->reqService->checkExtensions();
|
2017-12-16 00:41:52 +08:00
|
|
|
$directories = $this->reqService->checkPermissions();
|
|
|
|
|
|
|
|
# Only pass if all the items in the ext and dirs are passed
|
|
|
|
$statuses = [
|
|
|
|
$php_version['passed'] === true,
|
|
|
|
$this->allPassed($extensions) === true,
|
|
|
|
$this->allPassed($directories) === true
|
|
|
|
];
|
|
|
|
|
|
|
|
# Make sure there are no false values
|
|
|
|
$passed = ! in_array(false, $statuses, true);
|
2017-12-15 06:38:29 +08:00
|
|
|
|
|
|
|
return view('installer::steps/step1-requirements', [
|
|
|
|
'php' => $php_version,
|
|
|
|
'extensions' => $extensions,
|
2017-12-16 00:41:52 +08:00
|
|
|
'directories' => $directories,
|
2017-12-15 06:38:29 +08:00
|
|
|
'passed' => $passed,
|
|
|
|
]);
|
2017-12-14 12:28:58 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
|
|
|
* Step 2. Database Setup
|
|
|
|
*/
|
|
|
|
public function step2(Request $request)
|
|
|
|
{
|
|
|
|
$db_types = ['mysql' => 'mysql', 'sqlite' => 'sqlite'];
|
|
|
|
return view('installer::steps/step2-db', [
|
|
|
|
'db_types' => $db_types,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-15 22:49:01 +08:00
|
|
|
* Step 2a. Create the .env
|
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
|
|
|
{
|
2017-12-15 22:49:01 +08:00
|
|
|
Log::info('ENV setup', $request->toArray());
|
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) {
|
|
|
|
flash()->error($e->getMessage());
|
|
|
|
return redirect(route('installer.step2'))->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now write out the env file
|
|
|
|
|
2017-12-18 06:58:53 +08:00
|
|
|
try {
|
|
|
|
$this->envService->createEnvFile(
|
|
|
|
$request->input('db_conn'),
|
|
|
|
$request->input('db_host'),
|
|
|
|
$request->input('db_port'),
|
|
|
|
$request->input('db_name'),
|
|
|
|
$request->input('db_user'),
|
|
|
|
$request->input('db_pass')
|
|
|
|
);
|
|
|
|
} catch(FileException $e) {
|
|
|
|
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
|
|
|
|
2017-12-15 22:49:01 +08:00
|
|
|
# Needs to redirect so it can load the new .env
|
|
|
|
Log::info('Redirecting to database setup');
|
|
|
|
return redirect(route('installer.dbsetup'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Step 2b. Setup the database
|
|
|
|
*/
|
|
|
|
public function dbsetup(Request $request)
|
|
|
|
{
|
2017-12-30 06:56:46 +08:00
|
|
|
$console_out = '';
|
|
|
|
|
2017-12-16 00:41:52 +08:00
|
|
|
try {
|
2017-12-30 06:56:46 +08:00
|
|
|
$console_out .= $this->dbService->setupDB();
|
2017-12-16 00:41:52 +08:00
|
|
|
} catch(QueryException $e) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
return view('installer::steps/step2a-db_output', [
|
2017-12-15 11:59:54 +08:00
|
|
|
'console_output' => $console_out
|
|
|
|
]);
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Step 3. Setup the admin user and initial settings
|
|
|
|
*/
|
|
|
|
public function step3(Request $request)
|
|
|
|
{
|
2017-12-30 06:56:46 +08:00
|
|
|
return view('installer::steps/step3-user', []);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Step 3 submit
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
|
|
|
*/
|
|
|
|
public function usersetup(Request $request)
|
|
|
|
{
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
'airline_name' => 'required',
|
|
|
|
'airline_icao' => 'required|unique:airlines,icao',
|
2018-01-01 02:09:23 +08:00
|
|
|
'name' => 'required',
|
2017-12-30 06:56:46 +08:00
|
|
|
'email' => 'required|email|unique:users,email',
|
|
|
|
'password' => 'required|confirmed'
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return redirect('install/step3')
|
|
|
|
->withErrors($validator)
|
|
|
|
->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the first airline
|
|
|
|
*/
|
|
|
|
|
|
|
|
$attrs = [
|
|
|
|
'icao' => $request->get('airline_icao'),
|
|
|
|
'name' => $request->get('airline_name'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$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.
|
|
|
|
*/
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2017-12-30 06:56:46 +08:00
|
|
|
$attrs = [
|
|
|
|
'name' => $request->get('name'),
|
|
|
|
'email' => $request->get('email'),
|
|
|
|
'api_key' => Utils::generateApiKey(),
|
|
|
|
'airline_id' => $airline->id,
|
|
|
|
'home_airport_id' => 'KAUS',
|
|
|
|
'curr_airport_id' => 'KAUS',
|
|
|
|
'password' => Hash::make($request->get('password'))
|
|
|
|
];
|
|
|
|
|
|
|
|
$user = User::create($attrs);
|
|
|
|
$user = $this->userService->createPilot($user, ['admin']);
|
|
|
|
Log::info('User registered: ', $user->toArray());
|
|
|
|
|
2017-12-31 10:27:12 +08:00
|
|
|
# Set the intial admin e-mail address
|
2018-01-01 02:09:23 +08:00
|
|
|
setting('general.admin_email', $user->email);
|
2017-12-31 10:27:12 +08:00
|
|
|
|
2017-12-30 06:56:46 +08:00
|
|
|
return view('installer::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
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
2017-12-15 11:59:54 +08:00
|
|
|
public function complete(Request $request)
|
|
|
|
{
|
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
|
|
|
}
|