diff options
author | Jamis Buck <jamis@37signals.com> | 2005-07-22 10:37:09 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2005-07-22 10:37:09 +0000 |
commit | 06843f8794ad8c0368ece9ab57e340052f758025 (patch) | |
tree | 8c79aba7c8267733b4f66e20a6a57ff1c0b1110b /actionpack/lib/action_controller | |
parent | 06160fd3e85e99374ac09bb941aa499eba7a98c5 (diff) | |
download | rails-06843f8794ad8c0368ece9ab57e340052f758025.tar.gz rails-06843f8794ad8c0368ece9ab57e340052f758025.tar.bz2 rails-06843f8794ad8c0368ece9ab57e340052f758025.zip |
Added support for per-action session management #1763
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1886 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller')
-rw-r--r-- | actionpack/lib/action_controller/cgi_process.rb | 4 | ||||
-rw-r--r-- | actionpack/lib/action_controller/session_management.rb | 77 | ||||
-rw-r--r-- | actionpack/lib/action_controller/test_process.rb | 2 |
3 files changed, 80 insertions, 3 deletions
diff --git a/actionpack/lib/action_controller/cgi_process.rb b/actionpack/lib/action_controller/cgi_process.rb index a8ac127ac7..1b36274dd9 100644 --- a/actionpack/lib/action_controller/cgi_process.rb +++ b/actionpack/lib/action_controller/cgi_process.rb @@ -37,7 +37,7 @@ module ActionController #:nodoc: end class CgiRequest < AbstractRequest #:nodoc: - attr_accessor :cgi + attr_accessor :cgi, :session_options DEFAULT_SESSION_OPTIONS = { :database_manager => CGI::Session::PStore, @@ -115,7 +115,7 @@ module ActionController #:nodoc: end def reset_session - @session.delete + @session.delete if CGI::Session === @session @session = (@session_options == false ? {} : new_session) end diff --git a/actionpack/lib/action_controller/session_management.rb b/actionpack/lib/action_controller/session_management.rb new file mode 100644 index 0000000000..8341575f64 --- /dev/null +++ b/actionpack/lib/action_controller/session_management.rb @@ -0,0 +1,77 @@ +module ActionController #:nodoc: + module SessionManagement #:nodoc: + def self.append_features(base) + super + base.extend(ClassMethods) + base.class_eval do + alias_method :process_without_session_management_support, :process + alias_method :process, :process_with_session_management_support + end + end + + module ClassMethods + # Specify how sessions ought to be managed for a subset of the actions on + # the controller. Like filters, you can specify <tt>:only</tt> and + # <tt>:except</tt> clauses to restrict the subset, otherwise options + # apply to all actions on this controller. + # + # The session options are inheritable, as well, so if you specify them in + # a parent controller, they apply to controllers that extend the parent. + # + # Usage: + # + # # turn off session management for all actions. + # session :off + # + # # turn off session management for all actions _except_ foo and bar. + # session :off, :except => %w(foo bar) + # + # # turn off session management for only the foo and bar actions. + # session :off, :only => %w(foo bar) + # + # # the session will only work over HTTPS, but only for the foo action + # session :only => :foo, :session_secure => true + # + # All session options described for ActionController::Base.process_cgi + # are valid arguments. + def session(*args) + options = Hash === args.last ? args.pop : {} + + options[:disabled] = true if !args.empty? + options[:only] = [*options[:only]].map { |o| o.to_s } if options[:only] + options[:except] = [*options[:except]].map { |o| o.to_s } if options[:except] + if options[:only] && options[:except] + raise ArgumentError, "only one of either :only or :except are allowed" + end + + write_inheritable_array("session_options", [options]) + end + + def session_options_for(action) #:nodoc: + options = {} + + action = action.to_s + (read_inheritable_attribute("session_options") || []).each do |opts| + if opts[:only] && opts[:only].include?(action) + options.merge!(opts) + elsif opts[:except] && !opts[:except].include?(action) + options.merge!(opts) + elsif !opts[:only] && !opts[:except] + options.merge!(opts) + end + end + + options.delete :only + options.delete :except + + options[:disabled] ? false : options + end + end + + def process_with_session_management_support(request, response, method = :perform_action, *arguments) #:nodoc: + action = request.parameters["action"] || "index" + request.session_options = self.class.session_options_for(action) + process_without_session_management_support(request, response, method, *arguments) + end + end +end diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index 8e6c81b375..17953240c8 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -14,7 +14,7 @@ module ActionController #:nodoc: end class TestRequest < AbstractRequest #:nodoc: - attr_accessor :cookies + attr_accessor :cookies, :session_options attr_accessor :query_parameters, :request_parameters, :path, :session, :env attr_accessor :host |