From aa68332cf69575c7b7fde5a4c58508487efd02ab Mon Sep 17 00:00:00 2001 From: Darko Gjorgjijoski Date: Sun, 4 Feb 2024 19:37:09 +0100 Subject: [PATCH] Improve installed database detection --- app/Console/Kernel.php | 3 +- .../V1/Installation/FinishController.php | 3 +- .../OnboardingWizardController.php | 5 +- app/Http/Middleware/ConfigMiddleware.php | 3 +- .../Middleware/InstallationMiddleware.php | 13 ++- app/Http/Middleware/RedirectIfInstalled.php | 3 +- app/Providers/AppServiceProvider.php | 4 +- app/Providers/ViewServiceProvider.php | 13 ++- app/Space/InstallUtils.php | 86 +++++++++++++++++++ app/Space/helpers.php | 32 ++++--- .../2024_02_04_005632_update_version_100.php | 23 +++++ database/seeders/DemoSeeder.php | 3 +- .../admin/views/settings/UpdateAppSetting.vue | 19 ++-- 13 files changed, 161 insertions(+), 49 deletions(-) create mode 100644 app/Space/InstallUtils.php create mode 100644 database/migrations/2024_02_04_005632_update_version_100.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 07a9065e..55030f06 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -6,6 +6,7 @@ use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use InvoiceShelf\Models\CompanySetting; use InvoiceShelf\Models\RecurringInvoice; +use InvoiceShelf\Space\InstallUtils; class Kernel extends ConsoleKernel { @@ -28,7 +29,7 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - if (\Storage::disk('local')->has('database_created')) { + if (InstallUtils::isDbCreated()) { $schedule->command('check:invoices:status') ->daily(); diff --git a/app/Http/Controllers/V1/Installation/FinishController.php b/app/Http/Controllers/V1/Installation/FinishController.php index 050bbd48..3a053b26 100644 --- a/app/Http/Controllers/V1/Installation/FinishController.php +++ b/app/Http/Controllers/V1/Installation/FinishController.php @@ -4,6 +4,7 @@ namespace InvoiceShelf\Http\Controllers\V1\Installation; use Illuminate\Http\Request; use InvoiceShelf\Http\Controllers\Controller; +use InvoiceShelf\Space\InstallUtils; class FinishController extends Controller { @@ -14,7 +15,7 @@ class FinishController extends Controller */ public function __invoke(Request $request) { - \Storage::disk('local')->put('database_created', 'database_created'); + InstallUtils::createDbMarker(); return response()->json(['success' => true]); } diff --git a/app/Http/Controllers/V1/Installation/OnboardingWizardController.php b/app/Http/Controllers/V1/Installation/OnboardingWizardController.php index cb7acd29..32c732ad 100644 --- a/app/Http/Controllers/V1/Installation/OnboardingWizardController.php +++ b/app/Http/Controllers/V1/Installation/OnboardingWizardController.php @@ -5,17 +5,18 @@ namespace InvoiceShelf\Http\Controllers\V1\Installation; use Illuminate\Http\Request; use InvoiceShelf\Http\Controllers\Controller; use InvoiceShelf\Models\Setting; +use InvoiceShelf\Space\InstallUtils; class OnboardingWizardController extends Controller { /** * Handle the incoming request. * - * @return \Illuminate\Http\Response + * @return \Illuminate\Http\JsonResponse */ public function getStep(Request $request) { - if (! \Storage::disk('local')->has('database_created')) { + if (! InstallUtils::dbMarkerExists()) { return response()->json([ 'profile_complete' => 0, ]); diff --git a/app/Http/Middleware/ConfigMiddleware.php b/app/Http/Middleware/ConfigMiddleware.php index 63b2b5ac..b3b6567c 100644 --- a/app/Http/Middleware/ConfigMiddleware.php +++ b/app/Http/Middleware/ConfigMiddleware.php @@ -4,6 +4,7 @@ namespace InvoiceShelf\Http\Middleware; use Closure; use InvoiceShelf\Models\FileDisk; +use InvoiceShelf\Space\InstallUtils; class ConfigMiddleware { @@ -15,7 +16,7 @@ class ConfigMiddleware */ public function handle($request, Closure $next) { - if (\Storage::disk('local')->has('database_created')) { + if (InstallUtils::isDbCreated()) { if ($request->has('file_disk_id')) { $file_disk = FileDisk::find($request->file_disk_id); } else { diff --git a/app/Http/Middleware/InstallationMiddleware.php b/app/Http/Middleware/InstallationMiddleware.php index 0eb8b876..f27a1fbb 100644 --- a/app/Http/Middleware/InstallationMiddleware.php +++ b/app/Http/Middleware/InstallationMiddleware.php @@ -4,6 +4,7 @@ namespace InvoiceShelf\Http\Middleware; use Closure; use InvoiceShelf\Models\Setting; +use InvoiceShelf\Space\InstallUtils; class InstallationMiddleware { @@ -15,14 +16,12 @@ class InstallationMiddleware */ public function handle($request, Closure $next) { - if (! \Storage::disk('local')->has('database_created')) { - return redirect('/installation'); - } - - if (\Storage::disk('local')->has('database_created')) { - if (Setting::getSetting('profile_complete') !== 'COMPLETED') { - return redirect('/installation'); + if (! InstallUtils::isDbCreated() || Setting::getSetting('profile_complete') !== 'COMPLETED') { + if (InstallUtils::dbMarkerExists()) { + InstallUtils::deleteDbMarker(); } + + return redirect('/installation'); } return $next($request); diff --git a/app/Http/Middleware/RedirectIfInstalled.php b/app/Http/Middleware/RedirectIfInstalled.php index 65144c57..cf509553 100644 --- a/app/Http/Middleware/RedirectIfInstalled.php +++ b/app/Http/Middleware/RedirectIfInstalled.php @@ -4,6 +4,7 @@ namespace InvoiceShelf\Http\Middleware; use Closure; use InvoiceShelf\Models\Setting; +use InvoiceShelf\Space\InstallUtils; class RedirectIfInstalled { @@ -15,7 +16,7 @@ class RedirectIfInstalled */ public function handle($request, Closure $next) { - if (\Storage::disk('local')->has('database_created')) { + if (InstallUtils::dbMarkerExists()) { if (Setting::getSetting('profile_complete') === 'COMPLETED') { return redirect('login'); } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 98633c97..94b4adb1 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,8 +3,8 @@ namespace InvoiceShelf\Providers; use Illuminate\Pagination\Paginator; -use Illuminate\Support\Facades\Schema; use Illuminate\Support\ServiceProvider; +use InvoiceShelf\Space\InstallUtils; class AppServiceProvider extends ServiceProvider { @@ -18,7 +18,7 @@ class AppServiceProvider extends ServiceProvider Paginator::useBootstrapThree(); $this->loadJsonTranslationsFrom(resource_path('scripts/locales')); - if (\Storage::disk('local')->has('database_created') && Schema::hasTable('abilities')) { + if (InstallUtils::isDbCreated()) { $this->addMenus(); } } diff --git a/app/Providers/ViewServiceProvider.php b/app/Providers/ViewServiceProvider.php index ce2df97e..16b868a4 100644 --- a/app/Providers/ViewServiceProvider.php +++ b/app/Providers/ViewServiceProvider.php @@ -4,7 +4,6 @@ namespace InvoiceShelf\Providers; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; -use Schema; class ViewServiceProvider extends ServiceProvider { @@ -25,12 +24,10 @@ class ViewServiceProvider extends ServiceProvider */ public function boot() { - if (\Storage::disk('local')->has('database_created') && Schema::hasTable('settings')) { - View::share('login_page_logo', get_app_setting('login_page_logo')); - View::share('login_page_heading', get_app_setting('login_page_heading')); - View::share('login_page_description', get_app_setting('login_page_description')); - View::share('admin_page_title', get_app_setting('admin_page_title')); - View::share('copyright_text', get_app_setting('copyright_text')); - } + View::share('login_page_logo', get_app_setting('login_page_logo')); + View::share('login_page_heading', get_app_setting('login_page_heading')); + View::share('login_page_description', get_app_setting('login_page_description')); + View::share('admin_page_title', get_app_setting('admin_page_title')); + View::share('copyright_text', get_app_setting('copyright_text')); } } diff --git a/app/Space/InstallUtils.php b/app/Space/InstallUtils.php new file mode 100644 index 00000000..88d0fe8f --- /dev/null +++ b/app/Space/InstallUtils.php @@ -0,0 +1,86 @@ +has('database_created'); + } catch (FilesystemException $e) { + $flag = false; + } + + return $flag; + } + + /** + * Creates the database marker + * + * @return void + */ + public static function createDbMarker() + { + \Storage::disk('local')->put('database_created', 'database_created'); + } + + /** + * Deletes the database marker + * + * @return void + */ + public static function deleteDbMarker() + { + try { + \Storage::disk('local')->delete('database_created'); + } catch (\Exception $e) { + } + } +} diff --git a/app/Space/helpers.php b/app/Space/helpers.php index e4db5827..4e05deb4 100644 --- a/app/Space/helpers.php +++ b/app/Space/helpers.php @@ -5,6 +5,7 @@ use InvoiceShelf\Models\CompanySetting; use InvoiceShelf\Models\Currency; use InvoiceShelf\Models\CustomField; use InvoiceShelf\Models\Setting; +use InvoiceShelf\Space\InstallUtils; /** * Get company setting @@ -13,9 +14,11 @@ use InvoiceShelf\Models\Setting; */ function get_company_setting($key, $company_id) { - if (\Storage::disk('local')->has('database_created')) { - return CompanySetting::getSetting($key, $company_id); + if (! InstallUtils::isDbCreated()) { + return null; } + + return CompanySetting::getSetting($key, $company_id); } /** @@ -26,9 +29,11 @@ function get_company_setting($key, $company_id) */ function get_app_setting($key) { - if (\Storage::disk('local')->has('database_created')) { - return Setting::getSetting($key); + if (! InstallUtils::isDbCreated()) { + return null; } + + return Setting::getSetting($key); } /** @@ -38,22 +43,23 @@ function get_app_setting($key) */ function get_page_title($company_id) { + if (! InstallUtils::isDbCreated()) { + return null; + } + $routeName = Route::currentRouteName(); - $pageTitle = null; $defaultPageTitle = 'InvoiceShelf - Self Hosted Invoicing Platform'; - if (\Storage::disk('local')->has('database_created')) { - if ($routeName === 'customer.dashboard') { - $pageTitle = CompanySetting::getSetting('customer_portal_page_title', $company_id); - - return $pageTitle ? $pageTitle : $defaultPageTitle; - } - - $pageTitle = Setting::getSetting('admin_page_title'); + if ($routeName === 'customer.dashboard') { + $pageTitle = CompanySetting::getSetting('customer_portal_page_title', $company_id); return $pageTitle ? $pageTitle : $defaultPageTitle; } + + $pageTitle = Setting::getSetting('admin_page_title'); + + return $pageTitle ? $pageTitle : $defaultPageTitle; } /** diff --git a/database/migrations/2024_02_04_005632_update_version_100.php b/database/migrations/2024_02_04_005632_update_version_100.php new file mode 100644 index 00000000..fbdbbbea --- /dev/null +++ b/database/migrations/2024_02_04_005632_update_version_100.php @@ -0,0 +1,23 @@ +put('database_created', 'database_created'); + InstallUtils::createDbMarker(); } } diff --git a/resources/scripts/admin/views/settings/UpdateAppSetting.vue b/resources/scripts/admin/views/settings/UpdateAppSetting.vue index 53a6c99c..3f068c6e 100644 --- a/resources/scripts/admin/views/settings/UpdateAppSetting.vue +++ b/resources/scripts/admin/views/settings/UpdateAppSetting.vue @@ -308,23 +308,22 @@ async function checkUpdate() { isCheckingforUpdate.value = true let response = await axios.get('/api/v1/check/update') isCheckingforUpdate.value = false - if (!response.data.version) { + if (!response.data.release) { notificationStore.showNotification({ title: 'Info!', type: 'info', message: t('settings.update_app.latest_message'), }) - return + return; } if (response.data) { updateData.isMinor = response.data.is_minor - updateData.version = response.data.version.version - description.value = response.data.version.description - requiredExtentions.value = response.data.version.extensions + updateData.version = response.data.release.version + description.value = response.data.release.description + requiredExtentions.value = response.data.release.extensions isUpdateAvailable.value = true - minPhpVesrion.value = response.data.version.minimum_php_version - deletedFiles.value = response.data.version.deleted_files + minPhpVesrion.value = response.data.release.min_php_version } } catch (e) { isUpdateAvailable.value = false @@ -363,7 +362,6 @@ function onUpdateApp() { let updateParams = { version: updateData.version, installed: currentVersion.value, - deleted_files: deletedFiles.value, path: path || null, } @@ -377,10 +375,7 @@ function onUpdateApp() { } // on finish - if ( - currentStep.translationKey == - 'settings.update_app.finishing_update' - ) { + if (currentStep.translationKey == 'settings.update_app.finishing_update') { isUpdating.value = false notificationStore.showNotification({ type: 'success',