Module: HasGlobalSession::Rails::ActionControllerInstanceMethods

Defined in:
lib/has_global_session/rails/action_controller_instance_methods.rb

Overview

Module that is mixed into ActionController-derived classes when the class method has_global_session is called.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



17
18
19
20
21
22
# File 'lib/has_global_session/rails/action_controller_instance_methods.rb', line 17

def self.included(base) # :nodoc:
  base.alias_method_chain :session, :global_session
  base.before_filter :global_session_read_cookie
  base.before_filter :global_session_auto_renew
  base.after_filter  :global_session_update_cookie
end

Instance Method Details

#global_sessionObject

Global session reader.

Return

session(GlobalSession)

the global session associated with the current request, nil if none



37
38
39
# File 'lib/has_global_session/rails/action_controller_instance_methods.rb', line 37

def global_session
  @global_session
end

#global_session_auto_renewObject

Before-filter to renew the global session if it will be expiring soon.

Return

true

Always returns true



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/has_global_session/rails/action_controller_instance_methods.rb', line 99

def global_session_auto_renew
  #Auto-renew session if needed
  renew = global_session_config['renew']
  if @global_session &&
     renew &&
     @global_session.directory.local_authority_name &&
     @global_session.expired_at < renew.to_i.minutes.from_now.utc
    @global_session.renew!
  end

  return true
end

#global_session_configObject

Shortcut accessor for global session configuration object. Simply delegates to the class method of the same name defined by ActionController::Base.

Return

config(HasGlobalSession::Configuration)



29
30
31
# File 'lib/has_global_session/rails/action_controller_instance_methods.rb', line 29

def global_session_config
  ActionController::Base.global_session_config
end

Before-filter to read the global session cookie and construct the GlobalSession object for this controller instance.

Return

true

Always returns true



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/has_global_session/rails/action_controller_instance_methods.rb', line 66

def global_session_read_cookie
  directory   = global_session_create_directory
  cookie_name = global_session_config['cookie']['name']
  cookie      = cookies[cookie_name]

  begin
    cached_digest = session_without_global_session[:_session_gbl_valid_sig]

    #unserialize the global session from the cookie, or
    #initialize a new global session if cookie == nil.
    #
    #pass along the cached trusted signature (if any) so the new object
    #can skip the expensive RSA Decrypt operation.
    @global_session = GlobalSession.new(directory, cookie, cached_digest)

    session_without_global_session[:_session_gbl_valid_sig] = @global_session.signature_digest
    return true
  rescue Exception => e
    #silently recover from any error by initializing a new global session
    #and updating the session cookie
    @global_session = GlobalSession.new(directory)
    global_session_update_cookie

    #give the Rails app a chance to handle the exception
    #unless it's an ExpiredSession, which we handle transparently
    raise e
  end
end

After-filter to write any pending changes to the global session cookie.

Return

true

Always returns true



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/has_global_session/rails/action_controller_instance_methods.rb', line 116

def global_session_update_cookie
  name   = global_session_config['cookie']['name']
  domain = global_session_config['cookie']['domain'] || request.env['SERVER_NAME']

  begin
    if @global_session && @global_session.valid?
      value   = @global_session.to_s
      expires = global_session_config['ephemeral'] ? nil : @global_session.expired_at

      unless (cookies[name] == value)
        #Update the cookie only if its value has changed
        cookies[name] = {:value => value, :domain=>domain, :expires=>expires}
      end
    else
      #No valid session? Write an empty cookie.
      cookies[name] = {:value=>nil, :domain=>domain, :expires=>Time.at(0)}
    end
  rescue Exception => e
    #silently recover from any error by wiping the cookie
    cookies[name] = {:value=>nil, :domain=>domain, :expires=>Time.at(0)}
    #give the Rails app a chance to handle the exception
    raise e
  end
end

#log_processingObject

Override for the ActionController method of the same name that logs information about the request. Our version logs the global session ID instead of the local session ID.

Parameters

name(Type)

Description

Return

name(Type)

Description



150
151
152
153
154
155
# File 'lib/has_global_session/rails/action_controller_instance_methods.rb', line 150

def log_processing
  if logger && logger.info?
    log_processing_for_request_id
    log_processing_for_parameters
  end
end

#log_processing_for_parametersObject

:nodoc:



174
175
176
177
178
179
# File 'lib/has_global_session/rails/action_controller_instance_methods.rb', line 174

def log_processing_for_parameters # :nodoc:
  parameters = respond_to?(:filter_parameters) ? filter_parameters(params) : params.dup
  parameters = parameters.except!(:controller, :action, :format, :_method)

  logger.info "  Parameters: #{parameters.inspect}" unless parameters.empty?
end

#log_processing_for_request_idObject

:nodoc:



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/has_global_session/rails/action_controller_instance_methods.rb', line 157

def log_processing_for_request_id # :nodoc:
  if global_session && global_session.id
    session_id = global_session.id + " (#{session[:session_id]})"
  elsif session[:session_id]
    session_id = session[:session_id]
  elsif request.session_options[:id]
    session_id = request.session_options[:id]
  end

  request_id = "\n\nProcessing #{self.class.name}\##{action_name} "
  request_id << "to #{params[:format]} " if params[:format]
  request_id << "(for #{request_origin.split[0]}) [#{request.method.to_s.upcase}]"
  request_id << "\n  Session ID: #{session_id}" if session_id

  logger.info(request_id)
end

#session_with_global_sessionObject

Aliased version of ActionController::Base#session which will return the integrated global-and-local session object (IntegratedSession).

Return

session(IntegratedSession)

the integrated session



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/has_global_session/rails/action_controller_instance_methods.rb', line 46

def session_with_global_session
  if global_session_config['integrated'] && @global_session
    unless @integrated_session &&
           (@integrated_session.local == session_without_global_session) && 
           (@integrated_session.global == @global_session)
      @integrated_session =
        IntegratedSession.new(session_without_global_session, @global_session)
    end
    
    return @integrated_session
  else
    return session_without_global_session
  end
end