Module: ActionInterceptor::Controller::ClassMethods

Defined in:
lib/action_interceptor/controller.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_interceptor(options = {}) ⇒ Object



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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/action_interceptor/controller.rb', line 133

def acts_as_interceptor(options = {})
  self.is_interceptor = true
  self.use_interceptor = options[:override_url_options].nil? ? \
                           ActionInterceptor.override_url_options : \
                           options[:override_url_options]

  class_exec do

    attr_writer :intercepted_url

    skip_before_filter :delete_intercepted_url

    helper_method :intercepted_url, :intercepted_url_hash

    protected

    def intercepted_url
      return @intercepted_url if @intercepted_url

      key = ActionInterceptor.intercepted_url_key
      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(params[key])
      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[key] || request.referer || '/'
      end
      # Session is a signed plaintext in Rails 3
      # In Rails 4, it is encrypted by default
      session[key] = @intercepted_url
      @intercepted_url
    end

    def intercepted_url_hash
      return @intercepted_url_hash if @intercepted_url_hash
      url = Encryptor.encrypt_and_sign(intercepted_url)
      key = ActionInterceptor.intercepted_url_key

      @intercepted_url_hash = {key => url}
    end

    def redirect_back(options = {})
      url = intercepted_url

      # Disable the return_to param
      without_interceptor do
        # Convert '/' back to root_url
        # Also, prevent self redirects
        url = root_url if url == '/' || current_page?(url)

        redirect_to url, options
      end
    end

  end
end

#interceptor(*interceptor_names, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/action_interceptor/controller.rb', line 103

def interceptor(*interceptor_names, &block)
  options = interceptor_names.extract_options!
  filter_name = options.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, options
end

#skip_interceptor(*interceptor_names) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/action_interceptor/controller.rb', line 123

def skip_interceptor(*interceptor_names)
  options = interceptor_names.extract_options!
  filter_name = options.delete(:filter_name)
  fnames = interceptor_names.collect do |iname|
    filter_name || interceptor_filters[iname] || iname
  end

  skip_before_filter *fnames, options
end