From de9c224a92af2973f9e0c5a731470e35a6626e8f Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 7 Apr 2020 17:53:19 +0200 Subject: [PATCH] improved version check - use referer from client when available - save uuid returned by hub - send uuid to hub on version check - check the version on each startup to prevent wrong sync - also: do not save a setting on API update if it has not changed --- CHANGELOG.md | 1 + .../javascripts/controllers/application.js.erb | 5 +++-- app/assets/javascripts/services/version.js | 7 ++++++- app/controllers/api/settings_controller.rb | 2 ++ app/controllers/api/version_controller.rb | 8 ++++++++ app/models/setting.rb | 4 +++- app/workers/version_check_worker.rb | 12 +++++++++--- config/routes.rb | 2 +- env.example | 1 + lib/fab_hub.rb | 5 ++++- lib/version.rb | 2 +- 11 files changed, 39 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8624f006c..38ec69202 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Changelog Fab-manager +- Improved version check - Improved setup script for installations without nginx - Changed some default values for new installations - Updated documentation diff --git a/app/assets/javascripts/controllers/application.js.erb b/app/assets/javascripts/controllers/application.js.erb index 4425f0f76..ea478fb65 100644 --- a/app/assets/javascripts/controllers/application.js.erb +++ b/app/assets/javascripts/controllers/application.js.erb @@ -39,9 +39,10 @@ Application.Controllers.controller('ApplicationController', ['$rootScope', '$sco getNotifications(); // Fab-manager's app-version if (user.role === 'admin') { - return $scope.version = Version.get(); + // get the version + $scope.version = Version.get({origin: window.location.origin}); } else { - return $scope.version = { current: '' }; + $scope.version = { current: '' }; } } }; diff --git a/app/assets/javascripts/services/version.js b/app/assets/javascripts/services/version.js index 3eefe7ef3..cd7f297b8 100644 --- a/app/assets/javascripts/services/version.js +++ b/app/assets/javascripts/services/version.js @@ -1,5 +1,10 @@ 'use strict'; Application.Services.factory('Version', ['$resource', function ($resource) { - return $resource('/api/version'); + return $resource('/api/version/:origin', + { origin: '@origin' }, { + query: { + isArray: false + } + }); }]); diff --git a/app/controllers/api/settings_controller.rb b/app/controllers/api/settings_controller.rb index 9faaf350a..5b1604157 100644 --- a/app/controllers/api/settings_controller.rb +++ b/app/controllers/api/settings_controller.rb @@ -11,6 +11,8 @@ class API::SettingsController < API::ApiController def update authorize Setting @setting = Setting.find_or_initialize_by(name: params[:name]) + render status: :not_modified and return if setting_params[:value] == @setting.value + if @setting.save && @setting.history_values.create(value: setting_params[:value], invoicing_profile: current_user.invoicing_profile) SettingService.new.after_update(@setting) render status: :ok diff --git a/app/controllers/api/version_controller.rb b/app/controllers/api/version_controller.rb index 394efe8d4..41a616fe3 100644 --- a/app/controllers/api/version_controller.rb +++ b/app/controllers/api/version_controller.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'version' # API Controller to get the Fab-manager version @@ -7,6 +8,13 @@ class API::VersionController < API::ApiController def show authorize :version + # save the origin + origin = Setting.find_or_create_by(name: 'origin') + if origin.value != params[:origin] + origin.value = params[:origin] + origin.save! + end + # get the last version update_status = Setting.find_by(name: 'hub_last_version')&.value || '{}' json = JSON.parse(update_status) diff --git a/app/models/setting.rb b/app/models/setting.rb index 7923e21ca..7327f4451 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -73,7 +73,9 @@ class Setting < ApplicationRecord fab_analytics link_name home_content - home_css] } + home_css + origin + uuid] } def value last_value = history_values.order(HistoryValue.arel_table['created_at'].desc).first last_value&.value diff --git a/app/workers/version_check_worker.rb b/app/workers/version_check_worker.rb index b6e5a605e..cce3213ba 100644 --- a/app/workers/version_check_worker.rb +++ b/app/workers/version_check_worker.rb @@ -21,9 +21,15 @@ class VersionCheckWorker setting_ver.save! setting_key = Setting.find_or_initialize_by(name: 'hub_public_key') - return if setting_key.value == res['key'] + if setting_key.value != res['key'] + setting_key.value = res['key'] + setting_key.save! + end - setting_key.value = res['key'] - setting_key.save! + setting_uuid = Setting.find_or_initialize_by(name: 'uuid') + return if setting_uuid.value == res['uuid'] + + setting_uuid.value = res['uuid'] + setting_uuid.save! end end diff --git a/config/routes.rb b/config/routes.rb index 001f3a4d2..1088e4d34 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -159,7 +159,7 @@ Rails.application.routes.draw do end # Fab-manager's version - get 'version' => 'version#show' + get 'version/:origin' => 'version#show' # payments handling post 'payments/confirm_payment' => 'payments/confirm_payment' diff --git a/env.example b/env.example index e16bbf518..739f04dae 100644 --- a/env.example +++ b/env.example @@ -70,6 +70,7 @@ MOMENT_LOCALE=fr SUMMERNOTE_LOCALE=fr-FR ANGULAR_LOCALE=fr-fr FULLCALENDAR_LOCALE=fr +FORCE_VERSION_CHECK=false ELASTICSEARCH_LANGUAGE_ANALYZER=french diff --git a/lib/fab_hub.rb b/lib/fab_hub.rb index 82c33e3b1..68a97595d 100644 --- a/lib/fab_hub.rb +++ b/lib/fab_hub.rb @@ -3,8 +3,11 @@ # Fab-manager central hub (remote host) class FabHub def self.version_check_payload + uuid = Setting.find_by(name: 'uuid').value + origin = Setting.find_by(name: 'origin').value || "#{Rails.application.secrets.default_protocol}://#{Rails.application.secrets.default_host}" { - origin: "#{Rails.application.secrets.default_protocol}://#{Rails.application.secrets.default_host}", + uuid: uuid, + origin: origin, version: Version.current, lang: I18n.default_locale.to_s } diff --git a/lib/version.rb b/lib/version.rb index 1efdff202..f6a0f3736 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -32,6 +32,6 @@ class Version job = Sidekiq::Cron::Job.new(name: job_name, cron: "#{m} #{h} * * #{d}", class: 'VersionCheckWorker') job.save end - job.enque! if !job.last_enqueue_time || job.last_enqueue_time < DateTime.current - 24.hours + job.enque! end end