From 87ad49a0696781fd5734ca0f8e258badf8740e91 Mon Sep 17 00:00:00 2001 From: Thein Lin Kyaw Date: Thu, 16 Jan 2020 17:04:31 +0630 Subject: [PATCH] update multi tenancy --- .../api/authenticate_controller.rb | 1 - app/controllers/api/customers_controller.rb | 2 +- app/controllers/api/memberships_controller.rb | 3 --- app/controllers/api/move_controller.rb | 2 -- app/controllers/api/shops_controller.rb | 1 + app/controllers/concerns/multi_tenancy.rb | 15 +++++++++--- config/initializers/action_controller.rb | 23 +++++++++++-------- 7 files changed, 28 insertions(+), 19 deletions(-) diff --git a/app/controllers/api/authenticate_controller.rb b/app/controllers/api/authenticate_controller.rb index 69cac0c1..545735d9 100755 --- a/app/controllers/api/authenticate_controller.rb +++ b/app/controllers/api/authenticate_controller.rb @@ -1,5 +1,4 @@ class Api::AuthenticateController < Api::ApiController - skip_before_action :authenticate def create emp_id = params[:emp_id] diff --git a/app/controllers/api/customers_controller.rb b/app/controllers/api/customers_controller.rb index e20a2d68..9e6682ae 100755 --- a/app/controllers/api/customers_controller.rb +++ b/app/controllers/api/customers_controller.rb @@ -1,5 +1,5 @@ class Api::CustomersController < Api::ApiController - skip_before_action :authenticate + #List all active customers by name def index @customers = Customer.order("name asc") diff --git a/app/controllers/api/memberships_controller.rb b/app/controllers/api/memberships_controller.rb index ea5b5279..3468b8db 100755 --- a/app/controllers/api/memberships_controller.rb +++ b/app/controllers/api/memberships_controller.rb @@ -1,14 +1,11 @@ class Api::MembershipsController < ActionController::API #before :authenticate_token - #Add Membership to invoice def create end - - private def set_sale_params diff --git a/app/controllers/api/move_controller.rb b/app/controllers/api/move_controller.rb index 29913e06..123ea9fa 100755 --- a/app/controllers/api/move_controller.rb +++ b/app/controllers/api/move_controller.rb @@ -4,8 +4,6 @@ class Api::Restaurant::MoveController < Api::ApiController def update end - - private # Use callbacks to share common setup or constraints between actions. def move_params diff --git a/app/controllers/api/shops_controller.rb b/app/controllers/api/shops_controller.rb index b455784f..73d329a9 100644 --- a/app/controllers/api/shops_controller.rb +++ b/app/controllers/api/shops_controller.rb @@ -1,4 +1,5 @@ class Api::ShopsController < Api::ApiController + skip_before_action :set_current_tenant_by_subdomain_or_first def index @shops = Shop.select('id,logo,name,shop_code,address,phone_no').all diff --git a/app/controllers/concerns/multi_tenancy.rb b/app/controllers/concerns/multi_tenancy.rb index a88cc9e0..7fa85117 100644 --- a/app/controllers/concerns/multi_tenancy.rb +++ b/app/controllers/concerns/multi_tenancy.rb @@ -3,16 +3,25 @@ module MultiTenancy included do set_current_tenant_through_filter if respond_to? :set_current_tenant_through_filter - before_action :set_current_tenant_by_subdomain_or_frist if respond_to? :before_action + before_action :set_current_tenant_by_subdomain_or_first if respond_to? :before_action helper_method :current_shop if respond_to? :helper_method end private - def set_current_tenant_by_subdomain_or_frist + def set_current_tenant_by_subdomain_or_first + find_tenant_by_subdomain_or_frist || not_found + end + + def find_tenant_by_subdomain_or_frist if request.subdomains.last && request.subdomains.last != 'www' set_current_tenant(Shop.find_by(subdomain: request.subdomains.last)) + elsif ENV["SERVER_MODE"] == "application" + set_current_tenant(Shop.first) end - set_current_tenant(Shop.first) if current_tenant.nil? + end + + def not_found + head :not_found end def current_shop diff --git a/config/initializers/action_controller.rb b/config/initializers/action_controller.rb index 3a83e26a..d560c347 100644 --- a/config/initializers/action_controller.rb +++ b/config/initializers/action_controller.rb @@ -4,7 +4,7 @@ class ActionController::Base private def lookup_domain - if request.subdomains.last && request.subdomains.last != "www" && ENV["SERVER_CODE"] = "cloud" + if request.subdomain.present? && request.subdomain != "www" && ENV["SERVER_MODE"] == "cloud" from = request.host @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase if (!@license.nil?) @@ -18,15 +18,20 @@ class ActionController::Base # redirect_to root_url(:host => request.domain) + "store_error" render :json => [{ status: false, message: 'Invalid Access!'}] end - else + elsif request.subdomain.present? && request.subdomain != "www" || ENV["SERVER_MODE"] == "application" # check for license file - # if check_license - # current_license(ENV["SX_PROVISION_URL"]) - # else - # redirect_to activate_path - # end + if check_license + current_license(ENV["SX_PROVISION_URL"]) + else + redirect_to activate_path + end + else + render_not_found end + end + def render_not_found + render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found end def current_license(url) @@ -94,8 +99,8 @@ class ActionController::API private def lookup_domain - if request.subdomain.present? && request.subdomain != "www" - from = request.subdomain.downcase + "." + request.domain.downcase + if request.subdomain.present? && request.subdomain != "www" && ENV["SERVER_CODE"] == "cloud" + from = request.host @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase if (!@license.nil?) logger.info "Location - " + @license.dbschema