Module: ZuoraConnect::Controllers::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/zuora_connect/controllers/helpers.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_app_api_requestObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zuora_connect/controllers/helpers.rb', line 7

def authenticate_app_api_request
  start_time = Time.now
  if !request.headers["API-Token"].blank?
    @appinstance = ZuoraConnect::AppInstance.where(:api_token => request.headers["API-Token"]).first
    Rails.logger.debug("[#{@appinstance.id}] API REQUEST - API token") if !@appinstance.blank?
    check_instance
  else
    authenticate_or_request_with_http_basic do |username, password|
      @appinstance = ZuoraConnect::AppInstance.where(:token => password).first
      @appinstance ||= ZuoraConnect::AppInstance.where(:api_token => password).first
      Rails.logger.debug("[#{@appinstance.id}] API REQUEST - Basic Auth") if !@appinstance.blank?
      check_instance
    end
  end
  Rails.logger.info("[#{@appinstance.blank? ? "N/A" : @appinstance.id}] Authenticate App API Request Completed In - #{(Time.now - start_time).round(2)}s")
end

#authenticate_connect_app_requestObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zuora_connect/controllers/helpers.rb', line 24

def authenticate_connect_app_request
  start_time = Time.now
  if ZuoraConnect.configuration.mode == "Production"
    if request["data"]
      setup_instance_via_data
    else
      setup_instance_via_session
    end
  else
    setup_instance_via_dev_mode
  end
  #Call .data_lookup with the current session to retrieve session. In some cases session may be stored/cache in redis 
  #so data lookup provides a model method that can be overriden per app.
  @appinstance.new_session(:session => @appinstance.data_lookup(:session => session))
  PaperTrail.whodunnit =  session["#{@appinstance.id}::user::email"] if defined?(PaperTrail) && session["#{@appinstance.id}::user::email"].present? 
  begin
    I18n.locale = session["#{@appinstance.id}::user::locale"] ?  session["#{@appinstance.id}::user::locale"] : @appinstance.locale
  rescue I18n::InvalidLocale => ex
    Rails.logger.error("Invalid Locale: #{ex.message}")
  end
  Time.zone = session["#{@appinstance.id}::user::timezone"] ? session["#{@appinstance.id}::user::timezone"] : @appinstance.timezone
  Rails.logger.info("[#{@appinstance.blank? ? "N/A" : @appinstance.id}] Authenticate App Request Completed In - #{(Time.now - start_time).round(2)}s")
end

#check_connect_adminObject



62
63
64
# File 'lib/zuora_connect/controllers/helpers.rb', line 62

def check_connect_admin
  return session["#{@appinstance.id}::admin"]
end

#check_connect_admin!Object



58
59
60
# File 'lib/zuora_connect/controllers/helpers.rb', line 58

def check_connect_admin!
  raise ZuoraConnect::Exceptions::AccessDenied.new("User is not an authorized admin for this application") if !session["#{@appinstance.id}::admin"]
end

#persist_connect_app_sessionObject



48
49
50
51
52
53
54
55
56
# File 'lib/zuora_connect/controllers/helpers.rb', line 48

def persist_connect_app_session
  if @appinstance.present?
    if defined?(Redis.current) && Rails.application.config.session_store.to_s == "RedisSessionStore"
      @appinstance.cache_app_instance
    else
      session.merge!(@appinstance.save_data)
    end
  end
end