From 3d2d304654e86f5a986e2beb0e3b231be5b86086 Mon Sep 17 00:00:00 2001 From: Yan Date: Wed, 21 Feb 2018 16:30:14 +0630 Subject: [PATCH] license update for token --- app/controllers/application_controller.rb | 75 +---------------- app/controllers/base_crm_controller.rb | 2 +- app/controllers/base_inventory_controller.rb | 2 +- app/controllers/base_oqs_controller.rb | 2 +- app/controllers/base_origami_controller.rb | 2 +- app/controllers/base_report_controller.rb | 2 +- app/controllers/base_waiter_controller.rb | 2 +- .../concerns/license_verification.rb | 83 +++++++++++++++++++ .../concerns/login_verification.rb | 9 +- app/controllers/home_controller.rb | 4 +- 10 files changed, 101 insertions(+), 82 deletions(-) create mode 100644 app/controllers/concerns/license_verification.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9eb08f5f..3c061feb 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,11 @@ class ApplicationController < ActionController::Base - include LoginVerification + include LicenseVerification + #before_action :check_installation protect_from_forgery with: :exception # lookup domain for db from provision - before_action :lookup_domain, :set_locale + before_action :set_locale helper_method :current_company,:current_login_employee,:current_user # alias_method :current_user, :current_login_employee,:current_user @@ -22,68 +23,6 @@ class ApplicationController < ActionController::Base { locale: I18n.locale } end - def lookup_domain - if request.subdomain.present? && request.subdomain != "www" - from = request.subdomain.downcase + "." + request.domain.downcase - @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase - if (!@license.nil?) - # logger.info "Location - " + @license.name - ActiveRecord::Base.establish_connection(website_connection(@license)) - authentication - # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema - else - # reconnect_default_db - logger.info 'License is nil' - # redirect_to root_url(:host => request.domain) + "store_error" - render :json => [{ status: false, message: 'Invalid Access!'}] - end - else - # check for license file - # if check_license - # current_license(ENV["SX_PROVISION_URL"]) - # else - # redirect_to activate_path - # end - end - end - - def current_license(url) - @license = License.new(url) - flag = @license.detail_with_local_file() - if (flag == 0) - flash[:notice] = 'Expired or No License!' - elsif (flag == 2) - flash[:notice] = 'Expiring! Please, License extend...' - else - puts "RUN SAY BYAR" - end - end - - def cache_license(url, lookup) - @license = License.new(url, lookup) - - if (@license.detail_with_local_cache(lookup) == true) - return @license - else - return nil - end - end - - def website_connection(license) - default_connection.dup.update(:host => license.dbhost, :database => license.dbschema.to_s.downcase, - :username => license.dbusername, :password => license.dbpassword) - - end - - def reconnect_default_db - ActiveRecord::Base.establish_connection(Rails.env) - end - - # Regular database.yml configuration hash - def default_connection - @default_config ||= ActiveRecord::Base.connection.instance_variable_get("@config").dup - end - rescue_from CanCan::AccessDenied do |exception| flash[:warning] = exception.message redirect_to root_path @@ -112,14 +51,6 @@ class ApplicationController < ActionController::Base @employee = Employee.find_by_token_session(session[:session_token]) end end - - private - def check_license - if License.check_license_file - return true - end - return false - end end diff --git a/app/controllers/base_crm_controller.rb b/app/controllers/base_crm_controller.rb index e66c67b9..ff44bbbf 100755 --- a/app/controllers/base_crm_controller.rb +++ b/app/controllers/base_crm_controller.rb @@ -1,5 +1,5 @@ class BaseCrmController < ActionController::Base - include LoginVerification + include LicenseVerification layout "CRM" #before_action :check_installation diff --git a/app/controllers/base_inventory_controller.rb b/app/controllers/base_inventory_controller.rb index 4059ac25..0ef47129 100755 --- a/app/controllers/base_inventory_controller.rb +++ b/app/controllers/base_inventory_controller.rb @@ -1,5 +1,5 @@ class BaseInventoryController < ActionController::Base - include LoginVerification + include LicenseVerification layout "inventory" #before_action :check_installation diff --git a/app/controllers/base_oqs_controller.rb b/app/controllers/base_oqs_controller.rb index db9f8231..4fc58402 100755 --- a/app/controllers/base_oqs_controller.rb +++ b/app/controllers/base_oqs_controller.rb @@ -1,5 +1,5 @@ class BaseOqsController < ActionController::Base - include LoginVerification + include LicenseVerification layout "OQS" #before_action :check_installation diff --git a/app/controllers/base_origami_controller.rb b/app/controllers/base_origami_controller.rb index ec415bad..318c40f2 100755 --- a/app/controllers/base_origami_controller.rb +++ b/app/controllers/base_origami_controller.rb @@ -1,5 +1,5 @@ class BaseOrigamiController < ActionController::Base - include LoginVerification + include LicenseVerification layout "origami" # before_action :checkin_process diff --git a/app/controllers/base_report_controller.rb b/app/controllers/base_report_controller.rb index af9ec4fc..a164c499 100755 --- a/app/controllers/base_report_controller.rb +++ b/app/controllers/base_report_controller.rb @@ -1,5 +1,5 @@ class BaseReportController < ActionController::Base - include LoginVerification + include LicenseVerification layout "application" #before_action :check_installation diff --git a/app/controllers/base_waiter_controller.rb b/app/controllers/base_waiter_controller.rb index e86433dc..38749af0 100755 --- a/app/controllers/base_waiter_controller.rb +++ b/app/controllers/base_waiter_controller.rb @@ -1,5 +1,5 @@ class BaseWaiterController < ActionController::Base - include LoginVerification + include LicenseVerification layout "waiter" #before_action :check_installation diff --git a/app/controllers/concerns/license_verification.rb b/app/controllers/concerns/license_verification.rb new file mode 100644 index 00000000..bfa40f03 --- /dev/null +++ b/app/controllers/concerns/license_verification.rb @@ -0,0 +1,83 @@ +module LicenseVerification + extend ActiveSupport::Concern + + included do + before_action :lookup_domain + end + + protected + def lookup_domain + if request.subdomain.present? && request.subdomain != "www" + from = request.subdomain.downcase + "." + request.domain.downcase + @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase + if (!@license.nil?) + # logger.info "Location - " + @license.name + ActiveRecord::Base.establish_connection(website_connection(@license)) + authenticate_session_token + # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema + else + # reconnect_default_db + logger.info 'License is nil' + # redirect_to root_url(:host => request.domain) + "store_error" + render :json => [{ status: false, message: 'Invalid Access!'}] + end + else + # check for license file + # if check_license + # current_license(ENV["SX_PROVISION_URL"]) + # else + # redirect_to activate_path + # end + end + end + + def authenticate_session_token + token = session[:session_token] + if (token) + #@current_user = User.find_by(api_key: token) + #Rails.logger.debug "token - " + token.to_s + + @user = Employee.authenticate_by_token(token) + if !@user + flash[:notice] = 'Invalid Access!' + end + end + end + + def current_license(url) + @license = License.new(url) + flag = @license.detail_with_local_file() + if (flag == 0) + flash[:notice] = 'Expired or No License!' + elsif (flag == 2) + flash[:notice] = 'Expiring! Please, License extend...' + else + puts "RUN SAY BYAR" + end + end + + def cache_license(url, lookup) + @license = License.new(url, lookup) + + if (@license.detail_with_local_cache(lookup) == true) + return @license + else + return nil + end + end + + def website_connection(license) + default_connection.dup.update(:host => license.dbhost, :database => license.dbschema.to_s.downcase, + :username => license.dbusername, :password => license.dbpassword) + + end + + def reconnect_default_db + ActiveRecord::Base.establish_connection(Rails.env) + end + + # Regular database.yml configuration hash + def default_connection + @default_config ||= ActiveRecord::Base.connection.instance_variable_get("@config").dup + end +end \ No newline at end of file diff --git a/app/controllers/concerns/login_verification.rb b/app/controllers/concerns/login_verification.rb index 6e9cff8d..7ddf3cec 100755 --- a/app/controllers/concerns/login_verification.rb +++ b/app/controllers/concerns/login_verification.rb @@ -2,9 +2,8 @@ module LoginVerification extend ActiveSupport::Concern included do - # before_action :authenticate + before_action :authenticate helper_method :current_company,:current_login_employee - end @@ -24,6 +23,8 @@ module LoginVerification if @user return true #Maybe log - login? + else + flash[:notice] = 'Invalid Access!' end end end @@ -50,6 +51,10 @@ module LoginVerification end private + def check_license + License.check_license_file + end + def check_installation if current_company.nil? redirect_to install_path diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 25fb0d70..5afd383a 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,6 +1,6 @@ -class HomeController < ApplicationController +class HomeController < ApplicationController # layout "application", except: [:index, :show] - skip_before_action :authenticate, only: [:index, :show, :create, :update, :destroy] + skip_before_action only: [:index, :show, :create, :update, :destroy] def index # @employees = Employee.all_emp_except_waiter.order("name asc")