diff --git a/app/Events/UpdateFinished.php b/app/Events/UpdateFinished.php new file mode 100644 index 000000000..d221c60fd --- /dev/null +++ b/app/Events/UpdateFinished.php @@ -0,0 +1,26 @@ +alias = $alias; + $this->old = $old; + $this->new = $new; + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Install/Updates.php b/app/Http/Controllers/Install/Updates.php index 87c8a9069..9a35c0b09 100644 --- a/app/Http/Controllers/Install/Updates.php +++ b/app/Http/Controllers/Install/Updates.php @@ -3,9 +3,10 @@ namespace App\Http\Controllers\Install; use App\Http\Controllers\Controller; -use App\Models\Module\Module as Model; +use App\Events\UpdateFinished; use App\Utilities\Updater; use App\Utilities\Versions; +use Artisan; use Module; class Updates extends Controller @@ -73,17 +74,50 @@ class Updates extends Controller /** * Update the core or modules. * + * @param $alias + * @param $version * @return Response */ public function update($alias, $version) { set_time_limit(600); // 10 minutes - $status = Updater::update($alias, $version); + if (Updater::update($alias, $version)) { + return redirect('install/updates/post/' . $alias . '/' . version('short') . '/' . $version); + } - // Clear cache in order to check for updates again - Updater::clear(); + flash(trans('updates.error'))->error(); return redirect()->back(); } + + /** + * Final actions post update. + * + * @param $alias + * @param $old + * @param $new + * @return Response + */ + public function post($alias, $old, $new) + { + // Check if the file mirror was successful + if (($alias == 'core') && (version('short') != $new)) { + flash(trans('updates.error'))->error(); + + return redirect('install/updates'); + } + + // Clear cache after update + Artisan::call('cache:clear'); + + // Update database + Artisan::call('migrate', ['--force' => true]); + + event(new UpdateFinished($alias, $old, $new)); + + flash(trans('updates.success'))->success(); + + return redirect('install/updates'); + } } diff --git a/app/Listeners/Auth/Login.php b/app/Listeners/Auth/Login.php index 86f32dab6..84ef6d6a9 100644 --- a/app/Listeners/Auth/Login.php +++ b/app/Listeners/Auth/Login.php @@ -7,20 +7,11 @@ use Illuminate\Auth\Events\Login as ILogin; class Login { - /** - * Create the event listener. - * - * @return void - */ - public function __construct() - { - // - } /** * Handle the event. * - * @param Logout $event + * @param ILogin $event * @return void */ public function handle(ILogin $event) diff --git a/app/Listeners/Auth/Logout.php b/app/Listeners/Auth/Logout.php index 6220ea8ef..e6674112d 100644 --- a/app/Listeners/Auth/Logout.php +++ b/app/Listeners/Auth/Logout.php @@ -7,20 +7,11 @@ use Illuminate\Auth\Events\Logout as ILogout; class Logout { - /** - * Create the event listener. - * - * @return void - */ - public function __construct() - { - // - } /** * Handle the event. * - * @param Logout $event + * @param ILogout $event * @return void */ public function handle(ILogout $event) diff --git a/app/Listeners/Updates/Listener.php b/app/Listeners/Updates/Listener.php new file mode 100644 index 000000000..c377004da --- /dev/null +++ b/app/Listeners/Updates/Listener.php @@ -0,0 +1,31 @@ +alias != static::ALIAS) { + return false; + } + + // Do not apply to the same or newer versions + if ($event->old >= static::VERSION) { + return false; + } + + return true; + } +} diff --git a/app/Listeners/Updates/Version104.php b/app/Listeners/Updates/Version104.php new file mode 100644 index 000000000..cfde224c9 --- /dev/null +++ b/app/Listeners/Updates/Version104.php @@ -0,0 +1,33 @@ +check($event)) { + return; + } + + /*Permission::create([ + 'name' => 'john-doe', + 'display_name' => 'John Doe', + 'description' => 'John Doe', + ]);*/ + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index d113315ab..a83ab6343 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -13,8 +13,8 @@ class EventServiceProvider extends ServiceProvider * @var array */ protected $listen = [ - 'App\Events\Event' => [ - 'App\Listeners\EventListener', + 'App\Events\UpdateFinished' => [ + 'App\Listeners\Updates\Version104', ], 'Illuminate\Auth\Events\Login' => [ 'App\Listeners\Auth\Login', diff --git a/app/Utilities/Updater.php b/app/Utilities/Updater.php index 62a7d09cf..dbb4c3573 100644 --- a/app/Utilities/Updater.php +++ b/app/Utilities/Updater.php @@ -5,7 +5,6 @@ namespace App\Utilities; use App\Models\Module\Module as Model; use App\Models\Module\ModuleHistory as ModelHistory; use App\Traits\SiteApi; -use Artisan; use Cache; use Date; use File; @@ -28,20 +27,21 @@ class Updater // Update public static function update($alias, $version) { + // Download file if (!$data = static::download($alias, $version)) { return false; } + // Create temp directory $path = 'temp-' . md5(mt_rand()); $temp_path = storage_path('app/temp') . '/' . $path; - $file = $temp_path . '/upload.zip'; - - // Create tmp directory if (!File::isDirectory($temp_path)) { File::makeDirectory($temp_path); } + $file = $temp_path . '/upload.zip'; + // Add content to the Zip file $uploaded = is_int(file_put_contents($file, $data)) ? true : false; @@ -58,37 +58,20 @@ class Updater $zip->close(); - // Remove Zip - File::delete($file); - if ($alias == 'core') { - // Move all files/folders from temp path then delete it - File::copyDirectory($temp_path, base_path()); - File::deleteDirectory($temp_path); - - // Clear cache after update - Artisan::call('cache:clear'); - - // Update database - Artisan::call('migrate', ['--force' => true]); - - // Check if the file mirror was successful - /*if (version('short') != $version) { + // Move all files/folders from temp path + if (!File::copyDirectory($temp_path, base_path())) { return false; - }*/ + } } else { + // Get module instance $module = Module::get($alias); $model = Model::where('alias', $alias)->first(); - // Move all files/folders from temp path then delete it - File::copyDirectory($temp_path, module_path($module->get('name'))); - File::deleteDirectory($temp_path); - - // Clear cache after update - Artisan::call('cache:clear'); - - // Update database - Artisan::call('migrate', ['--force' => true]); + // Move all files/folders from temp path + if (!File::copyDirectory($temp_path, module_path($module->get('name')))) { + return false; + } // Add history ModelHistory::create([ @@ -100,6 +83,9 @@ class Updater ]); } + // Delete temp directory + File::deleteDirectory($temp_path); + return true; } diff --git a/resources/lang/en-GB/updates.php b/resources/lang/en-GB/updates.php index bfcdd24e8..e649eb68c 100644 --- a/resources/lang/en-GB/updates.php +++ b/resources/lang/en-GB/updates.php @@ -9,5 +9,7 @@ return [ 'check' => 'Check', 'new_core' => 'An updated version of Akaunting is available.', 'latest_core' => 'Congratulations! You have the latest version of Akaunting. Future security updates will be applied automatically.', + 'success' => 'Update process has been completed successfully.', + 'error' => 'Update process has failed, please, try again.', ]; diff --git a/resources/views/partials/admin/header.blade.php b/resources/views/partials/admin/header.blade.php index 4e7405ce9..cd5b6d6b6 100644 --- a/resources/views/partials/admin/header.blade.php +++ b/resources/views/partials/admin/header.blade.php @@ -138,8 +138,11 @@ @permission('read-install-updates')
  • - + + @if ($updates) + {{ $updates }} + @endif
  • @endpermission diff --git a/routes/web.php b/routes/web.php index f07680271..9f8bdfc2c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -104,7 +104,8 @@ Route::group(['middleware' => ['auth', 'language', 'adminmenu', 'permission:read Route::group(['prefix' => 'install'], function () { Route::get('updates/changelog', 'Install\Updates@changelog'); Route::get('updates/check', 'Install\Updates@check'); - Route::get('updates/update/{id}/{version}', 'Install\Updates@update'); + Route::get('updates/update/{alias}/{version}', 'Install\Updates@update'); + Route::get('updates/post/{alias}/{old}/{new}', 'Install\Updates@post'); Route::resource('updates', 'Install\Updates'); }); });