Files
sx-fc/app/controllers/concerns/multi_tenancy.rb
2020-01-29 14:20:50 +06:30

40 lines
1.2 KiB
Ruby

module MultiTenancy
extend ActiveSupport::Concern
included do
set_current_tenant_through_filter if respond_to? :set_current_tenant_through_filter
before_action :set_current_tenant_by_subdomain_or_name if respond_to? :before_action
helper_method :current_shop if respond_to? :helper_method
end
private
def set_current_tenant_by_subdomain_or_name
find_tenant_by_subdomain_or_name || not_found
end
def find_tenant_by_subdomain_or_name
if request.subdomains.last && request.subdomains.last != "www"
set_current_tenant(Shop.find_by(subdomain: request.subdomains.last))
elsif ENV["SERVER_MODE"] == "application"
license = License.new(ENV["SX_PROVISION_URL"], request.host)
shop_name = license.read_license_no_decrypt("shop_name")
if Rails.env.production? || shop_name
set_current_tenant(Shop.find_by(name: shop_name))
else
set_current_tenant(Shop.first)
end
end
end
def not_found
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
format.json { head :not_found }
end
end
def current_shop
ActsAsTenant.current_tenant
end
end