1 require 'action_controller/session/cookie_store'
2 require 'action_controller/session/drb_store'
3 require 'action_controller/session/mem_cache_store'
4 if Object.const_defined?(:ActiveRecord)
5 require 'action_controller/session/active_record_store'
8 module ActionController #:nodoc:
9 module SessionManagement #:nodoc:
10 def self.included(base)
13 alias_method_chain :process, :session_management_support
14 alias_method_chain :process_cleanup, :session_management_support
19 # Set the session store to be used for keeping the session data between requests. By default, sessions are stored
20 # in browser cookies (:cookie_store), but you can also specify one of the other included stores
21 # (:active_record_store, :p_store, drb_store, :mem_cache_store, or :memory_store) or your own custom class.
22 def session_store=(store)
23 ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager] =
24 store.is_a?(Symbol) ? CGI::Session.const_get(store == :drb_store ? "DRbStore" : store.to_s.camelize) : store
27 # Returns the session store class currently used.
29 ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager]
32 # Returns the hash used to configure the session. Example use:
34 # ActionController::Base.session_options[:session_secure] = true # session only available over HTTPS
36 ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
39 # Specify how sessions ought to be managed for a subset of the actions on
40 # the controller. Like filters, you can specify <tt>:only</tt> and
41 # <tt>:except</tt> clauses to restrict the subset, otherwise options
42 # apply to all actions on this controller.
44 # The session options are inheritable, as well, so if you specify them in
45 # a parent controller, they apply to controllers that extend the parent.
49 # # turn off session management for all actions.
52 # # turn off session management for all actions _except_ foo and bar.
53 # session :off, :except => %w(foo bar)
55 # # turn off session management for only the foo and bar actions.
56 # session :off, :only => %w(foo bar)
58 # # the session will only work over HTTPS, but only for the foo action
59 # session :only => :foo, :session_secure => true
61 # # the session will only be disabled for 'foo', and only if it is
62 # # requested as a web service
63 # session :off, :only => :foo,
64 # :if => Proc.new { |req| req.parameters[:ws] }
66 # # the session will be disabled for non html/ajax requests
68 # :if => Proc.new { |req| !(req.format.html? || req.format.js?) }
70 # All session options described for ActionController::Base.process_cgi
71 # are valid arguments.
73 options = args.extract_options!
75 options[:disabled] = true if !args.empty?
76 options[:only] = [*options[:only]].map { |o| o.to_s } if options[:only]
77 options[:except] = [*options[:except]].map { |o| o.to_s } if options[:except]
78 if options[:only] && options[:except]
79 raise ArgumentError, "only one of either :only or :except are allowed"
82 write_inheritable_array("session_options", [options])
85 # So we can declare session options in the Rails initializer.
86 alias_method :session=, :session
88 def cached_session_options #:nodoc:
89 @session_options ||= read_inheritable_attribute("session_options") || []
92 def session_options_for(request, action) #:nodoc:
93 if (session_options = cached_session_options).empty?
99 session_options.each do |opts|
100 next if opts[:if] && !opts[:if].call(request)
101 if opts[:only] && opts[:only].include?(action)
103 elsif opts[:except] && !opts[:except].include?(action)
105 elsif !opts[:only] && !opts[:except]
110 if options.empty? then options
113 options.delete :except
115 options[:disabled] ? false : options
121 def process_with_session_management_support(request, response, method = :perform_action, *arguments) #:nodoc:
122 set_session_options(request)
123 process_without_session_management_support(request, response, method, *arguments)
127 def set_session_options(request)
128 request.session_options = self.class.session_options_for(request, request.parameters["action"] || "index")
131 def process_cleanup_with_session_management_support
132 clear_persistent_model_associations
133 process_cleanup_without_session_management_support
136 # Clear cached associations in session data so they don't overflow
137 # the database field. Only applies to ActiveRecordStore since there
138 # is not a standard way to iterate over session data.
139 def clear_persistent_model_associations #:doc:
140 if defined?(@_session) && @_session.respond_to?(:data)
141 session_data = @_session.data
143 if session_data && session_data.respond_to?(:each_value)
144 session_data.each_value do |obj|
145 obj.clear_association_cache if obj.respond_to?(:clear_association_cache)