update multi_tenancy/license

This commit is contained in:
Thein Lin Kyaw
2020-01-29 14:20:50 +06:30
parent 0ea212bf65
commit 11ffaa738f
7 changed files with 37 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
class Api::ShopsController < Api::ApiController class Api::ShopsController < Api::ApiController
skip_before_action :set_current_tenant_by_subdomain_or_first, only: [:index, :show] skip_before_action :set_current_tenant_by_subdomain_or_name, only: [:index, :show]
def index def index
@shops = Shop.select('id,logo,name,shop_code,address,phone_no').all @shops = Shop.select('id,logo,name,shop_code,address,phone_no').all

View File

@@ -3,22 +3,28 @@ module MultiTenancy
included do included do
set_current_tenant_through_filter if respond_to? :set_current_tenant_through_filter set_current_tenant_through_filter if respond_to? :set_current_tenant_through_filter
before_action :set_current_tenant_by_subdomain_or_first if respond_to? :before_action before_action :set_current_tenant_by_subdomain_or_name if respond_to? :before_action
helper_method :current_shop if respond_to? :helper_method helper_method :current_shop if respond_to? :helper_method
end end
private private
def set_current_tenant_by_subdomain_or_first def set_current_tenant_by_subdomain_or_name
find_tenant_by_subdomain_or_frist || not_found find_tenant_by_subdomain_or_name || not_found
end end
def find_tenant_by_subdomain_or_frist def find_tenant_by_subdomain_or_name
if request.subdomains.last && request.subdomains.last != "www" if request.subdomains.last && request.subdomains.last != "www"
set_current_tenant(Shop.find_by(subdomain: request.subdomains.last)) set_current_tenant(Shop.find_by(subdomain: request.subdomains.last))
elsif ENV["SERVER_MODE"] == "application" 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) set_current_tenant(Shop.first)
end end
end end
end
def not_found def not_found
respond_to do |format| respond_to do |format|

View File

@@ -1,5 +1,5 @@
class InstallController < BaseController class InstallController < BaseController
skip_before_action :set_current_tenant_by_subdomain_or_first skip_before_action :set_current_tenant_by_subdomain_or_name
skip_before_action :verify_authenticity_token skip_before_action :verify_authenticity_token
def index def index

View File

@@ -219,26 +219,22 @@ class License
# read line by key for license file # read line by key for license file
def read_license(key_name) def read_license(key_name)
decrypted_line = ""
key, iv = get_redis_key() key, iv = get_redis_key()
if File.exist?("config/license.yml") if File.exist?("config/license.yml")
if license = YAML.load(File.read("config/license.yml")) if license = YAML.load(File.read("config/license.yml"))
decrypted_line = AESCrypt.decrypt_data(decode_str(license[lookup][key_name]), decode_str(key), decode_str(iv), ENV['CIPHER_TYPE']) AESCrypt.decrypt_data(decode_str(license[lookup][key_name]), decode_str(key), decode_str(iv), ENV['CIPHER_TYPE'])
end end
end end
return decrypted_line
end end
# read line by key for license file without decrypt # read line by key for license file without decrypt
def read_license_no_decrypt(key) def read_license_no_decrypt(key)
decrypted_line = ""
if File.exist?("config/license.yml") if File.exist?("config/license.yml")
if license = YAML.load_file("config/license.yml") if license = YAML.load_file("config/license.yml")
decrypted_line = license[lookup][key] decrypted_line = license[lookup][key]
end end
end end
return decrypted_line
end end
# Update license file for line # Update license file for line
@@ -255,11 +251,6 @@ class License
# To write changes to the file, use: # To write changes to the file, use:
File.open("config/license.yml", "w") {|file| file.write(license.to_yaml) } File.open("config/license.yml", "w") {|file| file.write(license.to_yaml) }
# File.open("config/license.yml").each do |line|
# new_file_str = line.gsub(content, crypted_str)
# f.put
# end
end end
end end
end end
@@ -306,6 +297,7 @@ class License
redis = Redis.new redis = Redis.new
cache_shop = redis.get(cache_key) cache_shop = redis.get(cache_key)
puts Marshal.load(cache_shop)
if !cache_shop.nil? if !cache_shop.nil?
@shop = Marshal.load(cache_shop) @shop = Marshal.load(cache_shop)
key = @shop["key"] key = @shop["key"]

View File

@@ -1,5 +1,6 @@
class ActionController::Base class ActionController::Base
before_action :lookup_domain, :set_locale before_action :lookup_domain if Rails.env.production?
before_action :set_locale
private private
@@ -96,7 +97,7 @@ class ActionController::Base
end end
class ActionController::API class ActionController::API
before_action :lookup_domain before_action :lookup_domain if Rails.env.production?
private private

View File

@@ -188,7 +188,7 @@ namespace :shop do
#Create Adminstrator employee #Create Adminstrator employee
ActiveRecord::Base.connection.execute("INSERT INTO employees(name,role,password_digest,emp_id,created_by,shop_code,created_at,updated_at,app_id,app_token) VALUES ('Administrator','administrator','$2a$10$OKFA4b4yh0xCJNmzjMMijusnO712LUSEL4/y56p2XmdKBRPWgXwDa','999','SYSTEM DEFAULT','#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),NULL,NULL),('Cashier','cashier','$2a$10$rXWBQro.YqUu5klH9oxqA.vcdq7webHm75FeXylY4uoJlek82luOq','333','SYSTEM DEFAULT','#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),NULL,NULL),('Manager','manager','$2a$10$JmzePPoejam8alpR6hrjr.8jIe/pm36IhErtoP4WwK788x2JfHUL2','777','SYSTEM DEFAULT','#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),NULL,NULL),('Application','application','$2a$12$Kzr5OIFb7B7KU52iKNoeSu5LnXtfJx0YX3s3d.HHJzxeodCv31Ehm','444','SYSTEM DEFAULT','#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(), '#{SecureRandom.urlsafe_base64(nil, false)}', '#{SecureRandom.hex(10)}');") ActiveRecord::Base.connection.execute("INSERT INTO employees(name,role,password_digest,emp_id,created_by,shop_code,created_at,updated_at,app_id,app_token) VALUES ('Administrator','administrator','$2a$10$OKFA4b4yh0xCJNmzjMMijusnO712LUSEL4/y56p2XmdKBRPWgXwDa','999','SYSTEM DEFAULT','#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),NULL,NULL),('Cashier','cashier','$2a$10$rXWBQro.YqUu5klH9oxqA.vcdq7webHm75FeXylY4uoJlek82luOq','333','SYSTEM DEFAULT','#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),NULL,NULL),('Manager','manager','$2a$10$JmzePPoejam8alpR6hrjr.8jIe/pm36IhErtoP4WwK788x2JfHUL2','777','SYSTEM DEFAULT','#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),NULL,NULL);")
Rails.logger.info " Finished Create Adminstrator employee Set Up Data" Rails.logger.info " Finished Create Adminstrator employee Set Up Data"
#PrintSetting #PrintSetting