/vendor/rails/actionpack/lib/action_controller/session_management.rb

https://bitbucket.org/javierbuilder/uncontabilidad · Ruby · 162 lines · 91 code · 17 blank · 54 comment · 23 complexity · 5d69b1f71425f56e2529cf543a73e8b8 MD5 · raw file

  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'
  6. end
  7. module ActionController #:nodoc:
  8. module SessionManagement #:nodoc:
  9. def self.included(base)
  10. base.class_eval do
  11. extend ClassMethods
  12. alias_method_chain :process, :session_management_support
  13. alias_method_chain :process_cleanup, :session_management_support
  14. end
  15. end
  16. module ClassMethods
  17. # Set the session store to be used for keeping the session data between requests.
  18. # By default, sessions are stored in browser cookies (<tt>:cookie_store</tt>),
  19. # but you can also specify one of the other included stores (<tt>:active_record_store</tt>,
  20. # <tt>:p_store</tt>, <tt>:drb_store</tt>, <tt>:mem_cache_store</tt>, or
  21. # <tt>:memory_store</tt>) 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
  25. end
  26. # Returns the session store class currently used.
  27. def session_store
  28. ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager]
  29. end
  30. # Returns the hash used to configure the session. Example use:
  31. #
  32. # ActionController::Base.session_options[:session_secure] = true # session only available over HTTPS
  33. def session_options
  34. ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
  35. end
  36. # Specify how sessions ought to be managed for a subset of the actions on
  37. # the controller. Like filters, you can specify <tt>:only</tt> and
  38. # <tt>:except</tt> clauses to restrict the subset, otherwise options
  39. # apply to all actions on this controller.
  40. #
  41. # The session options are inheritable, as well, so if you specify them in
  42. # a parent controller, they apply to controllers that extend the parent.
  43. #
  44. # Usage:
  45. #
  46. # # turn off session management for all actions.
  47. # session :off
  48. #
  49. # # turn off session management for all actions _except_ foo and bar.
  50. # session :off, :except => %w(foo bar)
  51. #
  52. # # turn off session management for only the foo and bar actions.
  53. # session :off, :only => %w(foo bar)
  54. #
  55. # # the session will only work over HTTPS, but only for the foo action
  56. # session :only => :foo, :session_secure => true
  57. #
  58. # # the session by default uses HttpOnly sessions for security reasons.
  59. # # this can be switched off.
  60. # session :only => :foo, :session_http_only => false
  61. #
  62. # # the session will only be disabled for 'foo', and only if it is
  63. # # requested as a web service
  64. # session :off, :only => :foo,
  65. # :if => Proc.new { |req| req.parameters[:ws] }
  66. #
  67. # # the session will be disabled for non html/ajax requests
  68. # session :off,
  69. # :if => Proc.new { |req| !(req.format.html? || req.format.js?) }
  70. #
  71. # # turn the session back on, useful when it was turned off in the
  72. # # application controller, and you need it on in another controller
  73. # session :on
  74. #
  75. # All session options described for ActionController::Base.process_cgi
  76. # are valid arguments.
  77. def session(*args)
  78. options = args.extract_options!
  79. options[:disabled] = false if args.delete(:on)
  80. options[:disabled] = true if !args.empty?
  81. options[:only] = [*options[:only]].map { |o| o.to_s } if options[:only]
  82. options[:except] = [*options[:except]].map { |o| o.to_s } if options[:except]
  83. if options[:only] && options[:except]
  84. raise ArgumentError, "only one of either :only or :except are allowed"
  85. end
  86. write_inheritable_array(:session_options, [options])
  87. end
  88. # So we can declare session options in the Rails initializer.
  89. alias_method :session=, :session
  90. def cached_session_options #:nodoc:
  91. @session_options ||= read_inheritable_attribute(:session_options) || []
  92. end
  93. def session_options_for(request, action) #:nodoc:
  94. if (session_options = cached_session_options).empty?
  95. {}
  96. else
  97. options = {}
  98. action = action.to_s
  99. session_options.each do |opts|
  100. next if opts[:if] && !opts[:if].call(request)
  101. if opts[:only] && opts[:only].include?(action)
  102. options.merge!(opts)
  103. elsif opts[:except] && !opts[:except].include?(action)
  104. options.merge!(opts)
  105. elsif !opts[:only] && !opts[:except]
  106. options.merge!(opts)
  107. end
  108. end
  109. if options.empty? then options
  110. else
  111. options.delete :only
  112. options.delete :except
  113. options.delete :if
  114. options[:disabled] ? false : options
  115. end
  116. end
  117. end
  118. end
  119. def process_with_session_management_support(request, response, method = :perform_action, *arguments) #:nodoc:
  120. set_session_options(request)
  121. process_without_session_management_support(request, response, method, *arguments)
  122. end
  123. private
  124. def set_session_options(request)
  125. request.session_options = self.class.session_options_for(request, request.parameters["action"] || "index")
  126. end
  127. def process_cleanup_with_session_management_support
  128. clear_persistent_model_associations
  129. process_cleanup_without_session_management_support
  130. end
  131. # Clear cached associations in session data so they don't overflow
  132. # the database field. Only applies to ActiveRecordStore since there
  133. # is not a standard way to iterate over session data.
  134. def clear_persistent_model_associations #:doc:
  135. if defined?(@_session) && @_session.respond_to?(:data)
  136. session_data = @_session.data
  137. if session_data && session_data.respond_to?(:each_value)
  138. session_data.each_value do |obj|
  139. obj.clear_association_cache if obj.respond_to?(:clear_association_cache)
  140. end
  141. end
  142. end
  143. end
  144. end
  145. end