Refactor the upgrade pending check to see if there are settings/permissions yaml changes (#438)

This commit is contained in:
Nabeel S 2019-11-19 10:54:42 -05:00 committed by GitHub
parent 3ec64c989b
commit bffa5ebde2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 127 additions and 19 deletions

View File

@ -117,8 +117,6 @@ class Handler extends ExceptionHandler
*/
protected function renderHttpException(HttpExceptionInterface $e)
{
Flash::error($e->getMessage());
$status = $e->getStatusCode();
view()->replaceNamespace('errors', [
resource_path('views/layouts/'.setting('general.theme', 'default').'/errors'),

View File

@ -1,25 +1,20 @@
<?php
/**
* Determine if an update is pending by checking if there are available migrations
* Redirect to the updater if there are. Done as middlware so it can happen before
* any authentication checks when someone goes to the admin panel
*/
namespace App\Http\Middleware;
use App\Services\Installer\MigrationService;
use App\Services\Installer\InstallerService;
use Closure;
/**
* Determine if an update is pending by checking in with the Installer service
*/
class UpdatePending
{
private $migrationSvc;
private $installerSvc;
/**
* @param MigrationService $migrationSvc
*/
public function __construct(MigrationService $migrationSvc)
public function __construct(InstallerService $installerSvc)
{
$this->migrationSvc = $migrationSvc;
$this->installerSvc = $installerSvc;
}
/**
@ -30,7 +25,7 @@ class UpdatePending
*/
public function handle($request, Closure $next)
{
if (count($this->migrationSvc->migrationsAvailable()) > 0) {
if ($this->installerSvc->isUpgradePending()) {
return redirect('/update/step1');
}

View File

@ -4,7 +4,7 @@
*/
Route::group([
'namespace' => 'Admin', 'prefix' => 'admin', 'as' => 'admin.',
'middleware' => ['update_pending', 'ability:admin,admin-access'],
'middleware' => ['ability:admin,admin-access'],
], static function () {
// CRUD for airlines
Route::resource('airlines', 'AirlinesController');
@ -90,8 +90,8 @@ Route::group([
)->name('users.regen_apikey');
// defaults
Route::get('', ['uses' => 'DashboardController@index']);
Route::get('/', ['uses' => 'DashboardController@index']);
Route::get('', ['uses' => 'DashboardController@index'])->middleware('update_pending');
Route::get('/', ['uses' => 'DashboardController@index'])->middleware('update_pending');
Route::get('dashboard', ['uses' => 'DashboardController@index', 'name' => 'dashboard']);
Route::match(

View File

@ -0,0 +1,39 @@
<?php
namespace App\Services\Installer;
use App\Contracts\Service;
class InstallerService extends Service
{
private $migrationSvc;
private $seederSvc;
/**
* @param $migrationSvc
* @param $seederSvc
*/
public function __construct(MigrationService $migrationSvc, SeederService $seederSvc)
{
$this->migrationSvc = $migrationSvc;
$this->seederSvc = $seederSvc;
}
/**
* Check to see if there is an upgrade pending by checking the migrations or seeds
*
* @return bool
*/
public function isUpgradePending(): bool
{
if (count($this->migrationSvc->migrationsAvailable()) > 0) {
return true;
}
if ($this->seederSvc->seedsPending()) {
return true;
}
return false;
}
}

View File

@ -10,6 +10,7 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\Yaml\Yaml;
use function trim;
class SeederService extends Service
{
@ -26,6 +27,24 @@ class SeederService extends Service
$this->databaseSvc = $databaseSvc;
}
/**
* See if there are any seeds that are out of sync
*
* @return bool
*/
public function seedsPending(): bool
{
if ($this->settingsSeedsPending()) {
return true;
}
if ($this->permissionsSeedsPending()) {
return true;
}
return false;
}
/**
* Syncronize all of the seed files, run this after the migrations
* and on first install.
@ -69,7 +88,7 @@ class SeederService extends Service
$data = file_get_contents(database_path('/seeds/settings.yml'));
$yml = Yaml::parse($data);
foreach ($yml as $setting) {
if (\trim($setting['key']) === '') {
if (trim($setting['key']) === '') {
continue;
}
@ -191,4 +210,61 @@ class SeederService extends Service
return $idx;
}
/**
* See if there are seeds pending for the settings
*
* @return bool
*/
private function settingsSeedsPending(): bool
{
$data = file_get_contents(database_path('/seeds/settings.yml'));
$yml = Yaml::parse($data);
// See if any are missing from the DB
foreach ($yml as $setting) {
if (trim($setting['key']) === '') {
continue;
}
$id = Setting::formatKey($setting['key']);
$row = DB::table('settings')->where('id', $id)->first();
// Doesn't exist in the table, quit early and say there is stuff pending
if (!$row) {
Log::info('Setting '.$id.' missing, update available');
return true;
}
// See if any of the options have changed
if ($row->type === 'select') {
if ($row->options !== $setting['options']) {
Log::info('Options for '.$id.' changed, update available');
return true;
}
}
}
return false;
}
/**
* See if there are seeds pending for the permissions
*
* @return bool
*/
private function permissionsSeedsPending(): bool
{
$data = file_get_contents(database_path('/seeds/permissions.yml'));
$yml = Yaml::parse($data);
foreach ($yml as $perm) {
$count = DB::table('permissions')->where('name', $perm['name'])->count('name');
if ($count === 0) {
return true;
}
}
return false;
}
}