From bffa5ebde221b08aede33f5c9118130ba9139187 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Tue, 19 Nov 2019 10:54:42 -0500 Subject: [PATCH] Refactor the upgrade pending check to see if there are settings/permissions yaml changes (#438) --- app/Exceptions/Handler.php | 2 - app/Http/Middleware/UpdatePending.php | 21 +++--- app/Http/Routes/admin.php | 6 +- app/Services/Installer/InstallerService.php | 39 +++++++++++ app/Services/Installer/SeederService.php | 78 ++++++++++++++++++++- 5 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 app/Services/Installer/InstallerService.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 5d00553b..cfff05e4 100755 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -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'), diff --git a/app/Http/Middleware/UpdatePending.php b/app/Http/Middleware/UpdatePending.php index 4d29f9db..18d3f5ec 100644 --- a/app/Http/Middleware/UpdatePending.php +++ b/app/Http/Middleware/UpdatePending.php @@ -1,25 +1,20 @@ 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'); } diff --git a/app/Http/Routes/admin.php b/app/Http/Routes/admin.php index a736e996..f0f83c75 100644 --- a/app/Http/Routes/admin.php +++ b/app/Http/Routes/admin.php @@ -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( diff --git a/app/Services/Installer/InstallerService.php b/app/Services/Installer/InstallerService.php new file mode 100644 index 00000000..9cfdca31 --- /dev/null +++ b/app/Services/Installer/InstallerService.php @@ -0,0 +1,39 @@ +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; + } +} diff --git a/app/Services/Installer/SeederService.php b/app/Services/Installer/SeederService.php index 05b76433..2ae2fed9 100644 --- a/app/Services/Installer/SeederService.php +++ b/app/Services/Installer/SeederService.php @@ -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; + } }