diff --git a/app/models/license.rb b/app/models/license.rb index 3a755030..f7662455 100755 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -2,6 +2,7 @@ require 'yaml' class License include HTTParty + include ActionDispatch::Http base_uri "https://l.doemal.app//api" @@ -93,6 +94,7 @@ class License # License Activation def license_activate (shop, license_key, db_host, db_schema, db_user, db_password) + aes = MyAesCrypt.new aes_key, aes_iv = aes.export_key(license_key) @@ -101,26 +103,26 @@ class License @activate = response.parsed_response if (@activate["status"]) + ##Check from local redis - if available load local otherwise get from remote cache_key = "shop:#{@activate["shop_name"]}" - cache_license = nil - ##Get redis connection from connection pool + # redis = Redis.new + # cache_license = redis.get(cache_key) + + Rails.logger.info "Cache key - " + cache_key.to_s + + # if cache_license.nil? + cache = {"shop" => @activate["shop_name"], "key" => aes_key, "iv" => @activate["iv_key"], "renewable_date" => @activate["renewable_date"] } redis = Redis.new - cache_license = redis.get(cache_key) - - Rails.logger.info "Cache key - " + cache_key.to_s - - if cache_license.nil? - cache = {"shop" => @activate["shop_name"], "key" => aes_key, "iv" => @activate["iv_key"], "renewable_date" => @activate["renewable_date"] } - redis = Redis.new - redis.set(cache_key, Marshal.dump(cache)) - end + redis.set(cache_key, Marshal.dump(cache)) + # end Rails.logger.info "License - " + response.parsed_response.to_s response = create_license_file(@activate) + if(response[:status]) #sym_path = "/home/user/symmetric/" sym_path = File.expand_path("~/symmetric/") @@ -197,7 +199,21 @@ class License def self.check_license_file(lookup) return unless File.exist?("config/license.yml") if license = YAML.load_file("config/license.yml") - license[lookup].present? + if license[lookup].nil? + if ENV["SERVER_MODE"] == "application" + license[lookup] = license.values.first + else + tld_length = Rails.application.config.action_dispatch.tld_length + subdomains = URL.extract_subdomains(lookup, tld_length) + if key = license.keys.find { |k| URL.extract_subdomains(k, tld_length).last == subdomains.last} + license[lookup] = license[key] + end + end + if license[lookup] + File.open("config/license.yml", "w") { |file| file.write license.to_yaml } + end + end + return license[lookup] end end @@ -238,7 +254,7 @@ class License license[lookup][content] = encode_str(crypted_str) # To write changes to the file, use: - File.open("config/license.yml", "w") {|file| file.puts new_file_str } + 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) @@ -286,8 +302,6 @@ class License ##Check from local redis - if available load local otherwise get from remote cache_key = "shop:#{shop.chomp}" - cache_shop = nil - ##Get redis connection from connection pool redis = Redis.new cache_shop = redis.get(cache_key) @@ -310,45 +324,36 @@ class License # License File Creation def create_license_file(response_data) - if File.exist?("config/license.yml") - delete_license_file - end - begin # Licese File Creation - # File.open("config/license.yml", "w") do |f| - # f.puts("iv_key: #{response_data['iv_key']}") - # f.puts("shop_name: #{response_data['shop_name']}") - # f.puts("email: #{response_data['email']}") - # f.puts("telephone: #{response_data['telephone']}") - # f.puts("fax: #{response_data['fax']}") - # f.puts("address: #{response_data['address']}") - # f.puts("dbhost: #{response_data['dbhost']}") - # f.puts("dbschema: #{response_data['dbschema']}") - # f.puts("dbusername: #{response_data['dbusername']}") - # f.puts("dbpassword: #{response_data['dbpassword']}") - # f.puts("api_token: #{response_data['api_token']}") - # f.puts("app_token: #{response_data['app_token']}") - # f.puts("plan_sku: #{response_data['plan_sku']}") - # f.puts("renewable_date: #{response_data['renewable_date']}") - # f.puts("plan_name: #{response_data['plan_name']}") - # end - license = {lookup => {"iv_key" => response_data['iv_key'], - "shop_name" => response_data['shop_name'], - "email" => response_data['email'], - "telephone" => response_data['telephone'], - "fax" => response_data['fax'], - "address" => response_data['address'], - "dbhost" => response_data['dbhost'], - "dbschema" => response_data['dbschema'], - "dbusername" => response_data['dbusername'], - "dbpassword" => response_data['dbpassword'], - "api_token" => response_data['api_token'], - "app_token" => response_data['app_token'], - "plan_sku" => response_data['plan_sku'], - "renewable_date" => response_data['renewable_date'], - "plan_name" => response_data['plan_name']} - } + config = { "iv_key" => response_data['iv_key'], + "shop_name" => response_data['shop_name'], + "email" => response_data['email'], + "telephone" => response_data['telephone'], + "fax" => response_data['fax'], + "address" => response_data['address'], + "dbhost" => response_data['dbhost'], + "dbschema" => response_data['dbschema'], + "dbusername" => response_data['dbusername'], + "dbpassword" => response_data['dbpassword'], + "api_token" => response_data['api_token'], + "app_token" => response_data['app_token'], + "plan_sku" => response_data['plan_sku'], + "renewable_date" => response_data['renewable_date'], + "plan_name" => response_data['plan_name'] } + + if File.exist?("config/license.yml") + license = YAML.load_file("config/license.yml") + end + + if license + license.each do |k,v| + license[k] = config if v["shop_name"] == config["shop_name"] + end + license[lookup] = config + end + + license ||= {lookup => config} File.open("config/license.yml", "w"){ |file| file.write(license.to_yaml) } rescue IOError @@ -407,10 +412,10 @@ class License # f.write("initial.load.use.extract.job.enabled=true\n") f.close rescue IOError - response = { "status": false, "message": "Activation is success but something is wrong. \n Please contact code2lab call center!"} + return { "status": false, "message": "Activation is success but something is wrong. \n Please contact code2lab call center!"} end - response = { "status": true, "message": "Success Activation. License also created."} end + { "status": true, "message": "Success Activation. License also created." } end # Run Symmetric diff --git a/config/initializers/action_controller.rb b/config/initializers/action_controller.rb index 25655a91..dfcf041d 100644 --- a/config/initializers/action_controller.rb +++ b/config/initializers/action_controller.rb @@ -4,9 +4,8 @@ class ActionController::Base private def lookup_domain - 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 request.subdomains.last && request.subdomains.last != "www" && ENV["SERVER_MODE"] == "cloud" + @license = cache_license(ENV["SX_PROVISION_URL"], request.host) # request.subdomain.downcase if (!@license.nil?) logger.info "Location - " + @license.dbschema ActiveRecord::Base.establish_connection(website_connection(@license)) @@ -18,7 +17,7 @@ class ActionController::Base # redirect_to root_url(:host => request.domain) + "store_error" render :json => [{ status: false, message: 'Invalid Access!'}] end - elsif request.subdomain.present? && request.subdomain != "www" || ENV["SERVER_MODE"] == "application" + elsif request.subdomains.last && request.subdomains.last != "www" || ENV["SERVER_MODE"] == "application" # check for license file if check_license(request.host) current_license(ENV["SX_PROVISION_URL"], request.host) @@ -102,9 +101,8 @@ class ActionController::API private def lookup_domain - 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 request.subdomains.last && request.subdomains.last != "www" && ENV["SERVER_CODE"] == "cloud" + @license = cache_license(ENV["SX_PROVISION_URL"], request.host) # request.subdomain.downcase if (!@license.nil?) logger.info "Location - " + @license.dbschema ActiveRecord::Base.establish_connection(website_connection(@license))