diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 691c6648..6f8eec9a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,18 +3,60 @@ class ApplicationController < ActionController::Base #before_action :check_installation protect_from_forgery with: :exception + # lookup domain for db from provision + before_action :lookup_domain + helper_method :current_company,:current_login_employee,:current_user # alias_method :current_user, :current_login_employee,:current_user #this is base api base controller to need to inherit. #all token authentication must be done here #response format must be set to JSON + def lookup_domain + # if request.subdomain.present? && request.subdomain != "www" + @license = current_license(ENV["SX_PROVISION_URL"], request.subdomain.downcase) + if (!@license.nil?) + # logger.info "Location - " + @license.name + ActiveRecord::Base.establish_connection(website_connection(@license)) + # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema + else + logger.info 'License is nil' + # redirect_to root_url(:host => request.domain) + "store_error" + render :json => [{ status: false, message: 'Invalid Access!'}] + end + # end + end + + def current_license(url, key) + @license = License.new(url, key) + + ##creating md5 hash + md5_hostname = Digest::MD5.new + md5key = md5_hostname.update(request.host) + if (@license.detail_with_local_cache(key, md5key.to_s) == true) + #if (@license.detail == 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 + + # 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 - end - + end def current_user @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] @@ -42,3 +84,6 @@ class ApplicationController < ActionController::Base end end end + + +