| 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 |
|
|---|
| 8 |
module ActionController |
|---|
| 9 |
module SessionManagement |
|---|
| 10 |
def self.included(base) |
|---|
| 11 |
base.class_eval do |
|---|
| 12 |
extend ClassMethods |
|---|
| 13 |
alias_method_chain :process, :session_management_support |
|---|
| 14 |
alias_method_chain :process_cleanup, :session_management_support |
|---|
| 15 |
end |
|---|
| 16 |
end |
|---|
| 17 |
|
|---|
| 18 |
module ClassMethods |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 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 |
|
|---|
| 27 |
|
|---|
| 28 |
def session_store |
|---|
| 29 |
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager] |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
def session_options |
|---|
| 36 |
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS |
|---|
| 37 |
end |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
def session(*args) |
|---|
| 73 |
options = args.extract_options! |
|---|
| 74 |
|
|---|
| 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" |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
write_inheritable_array("session_options", [options]) |
|---|
| 83 |
end |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
alias_method :session=, :session |
|---|
| 87 |
|
|---|
| 88 |
def cached_session_options |
|---|
| 89 |
@session_options ||= read_inheritable_attribute("session_options") || [] |
|---|
| 90 |
end |
|---|
| 91 |
|
|---|
| 92 |
def session_options_for(request, action) |
|---|
| 93 |
if (session_options = cached_session_options).empty? |
|---|
| 94 |
{} |
|---|
| 95 |
else |
|---|
| 96 |
options = {} |
|---|
| 97 |
|
|---|
| 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 |
|
|---|
| 110 |
if options.empty? then options |
|---|
| 111 |
else |
|---|
| 112 |
options.delete :only |
|---|
| 113 |
options.delete :except |
|---|
| 114 |
options.delete :if |
|---|
| 115 |
options[:disabled] ? false : options |
|---|
| 116 |
end |
|---|
| 117 |
end |
|---|
| 118 |
end |
|---|
| 119 |
end |
|---|
| 120 |
|
|---|
| 121 |
def process_with_session_management_support(request, response, method = :perform_action, *arguments) |
|---|
| 122 |
set_session_options(request) |
|---|
| 123 |
process_without_session_management_support(request, response, method, *arguments) |
|---|
| 124 |
end |
|---|
| 125 |
|
|---|
| 126 |
private |
|---|
| 127 |
def set_session_options(request) |
|---|
| 128 |
request.session_options = self.class.session_options_for(request, request.parameters["action"] || "index") |
|---|
| 129 |
end |
|---|
| 130 |
|
|---|
| 131 |
def process_cleanup_with_session_management_support |
|---|
| 132 |
clear_persistent_model_associations |
|---|
| 133 |
process_cleanup_without_session_management_support |
|---|
| 134 |
end |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
def clear_persistent_model_associations |
|---|
| 140 |
if defined?(@_session) && @_session.respond_to?(:data) |
|---|
| 141 |
session_data = @_session.data |
|---|
| 142 |
|
|---|
| 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) |
|---|
| 146 |
end |
|---|
| 147 |
end |
|---|
| 148 |
end |
|---|
| 149 |
end |
|---|
| 150 |
end |
|---|
| 151 |
end |
|---|