Module: ActionInterceptor::ActionController::ClassMethods
- Defined in:
- lib/action_interceptor/action_controller.rb
Instance Method Summary collapse
- #acts_as_interceptor(opts = {}) ⇒ Object
- #interceptor(*interceptor_names, &block) ⇒ Object
- #skip_interceptor(*interceptor_names) ⇒ Object
Instance Method Details
#acts_as_interceptor(opts = {}) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/action_interceptor/action_controller.rb', line 97 def acts_as_interceptor(opts = {}) self.interceptor_config = ActionInterceptor::DEFAULT_CONFIG.merge(opts) class_exec do attr_writer :intercepted_url # Ensure that we always store the intercepted url before_filter :intercepted_url helper_method :intercepted_url, :intercepted_url_hash protected def intercepted_url return @intercepted_url if @intercepted_url key = interceptor_config[:intercepted_url_key] encrypted_url = params[key] session_hash = session[:interceptor] \ unless interceptor_config[:skip_session] session_hash ||= {} begin # URL params are the most reliable, as they preserve # state even if the user presses the back button # We need to sign them to prevent the Open Redirect vulnerability @intercepted_url = Encryptor.decrypt_and_verify(encrypted_url) # If we got this far, the encrypted url is valid # (i.e. we signed it), so reuse it @intercepted_url_hash = {key => encrypted_url} rescue ActiveSupport::MessageVerifier::InvalidSignature # If the param is not available, use our best guess # Session and referer are safe for redirects (for that user) # Also, can't call root_url here, so use '/' instead @intercepted_url = session_hash[key] || request.referer || '/' end # Prevent self-redirect @intercepted_url = '/' if current_page?(@intercepted_url) # Session is a signed plaintext in Rails 3 # In Rails 4, it is encrypted by default session[:interceptor] = session_hash.merge( key => @intercepted_url ) unless interceptor_config[:skip_session] @intercepted_url end def intercepted_url_hash # Run intercepted_url to verify the params in case the # encrypted url is in there and can be reused unencrypted_url = intercepted_url return @intercepted_url_hash if @intercepted_url_hash url = Encryptor.encrypt_and_sign(unencrypted_url) key = interceptor_config[:intercepted_url_key] @intercepted_url_hash = {key => url} end def redirect_back( = {}) # Disable the return_to param without_interceptor do redirect_to intercepted_url, end end end end |
#interceptor(*interceptor_names, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/action_interceptor/action_controller.rb', line 67 def interceptor(*interceptor_names, &block) = interceptor_names. filter_name = .delete(:filter_name) fnames = interceptor_names.collect do |iname| fname = filter_name || iname interceptor_filters[iname] = fname define_method fname do blk = block || ActionInterceptor.interceptors[iname] raise UndefinedInterceptor, iname unless blk with_interceptor &blk end fname end before_filter *fnames, end |
#skip_interceptor(*interceptor_names) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/action_interceptor/action_controller.rb', line 87 def skip_interceptor(*interceptor_names) = interceptor_names. filter_name = .delete(:filter_name) fnames = interceptor_names.collect do |iname| filter_name || interceptor_filters[iname] || iname end skip_before_filter *fnames, end |