module Connect module Controllers module Helpers extend ActiveSupport::Concern def authenticate_request if @mode == "Production" data_present = set_session if !session["valid"] && data_present res = HTTParty.get(Connect.configuration.url + "/api/v1/tools/tasks/#{session["appInstance"]}.json",:basic_auth => auth = {:username => session["user"], :password => session["key"]}) if res.code == 200 session["valid"] = true end end if !session["valid"] redirect_to "/500.html" end set_app_instance(session["appInstance"].to_i) if session["appInstance"] || ( session["appInstance"] && Apartment::Tenant.current != session["appInstance"]) @appinstance.new_session(session["user"], session["key"]) if @appinstance && @appinstance.valid else dev_mode set_app_instance(session["appInstance"].to_i) @appinstance.options = Connect.configuration.dev_mode_options @appinstance.logins = Connect.configuration.dev_mode_logins @appinstance.mode = Connect.configuration.dev_mode_mode end end def set_session if request["data"] values = JSON.parse(Connect::AppInstance.decrypt_response(Base64.urlsafe_decode64(request["data"]))) values["param_data"].each do |k ,v| params[k] = v end session["user"] = values["user"] session["key"] = values["key"] session["appInstance"] = values["appInstance"] session["valid"] = false session["admin"] = values["admin"] ? values["admin"] : false return true end return false end def dev_mode session["appInstance"] = "00000" session["user"] = "test" session["key"] = "test" session["valid"] = true end def set_app_instance(id) if Connect::AppInstance.where(:id => id).size == 0 Connect::AppInstance.create!(:id => id ) Apartment::Tenant.create(id) end @appinstance = Connect::AppInstance.find(id) Apartment::Tenant.switch!(@appinstance.id) end def check_admin redirect_to "/500.html" if session["admin"] != "true" end end end end