2010-02-19 03:52:04 -05:00
|
|
|
require "action_controller/metal"
|
|
|
|
|
2009-10-22 18:12:00 -04:00
|
|
|
module Devise
|
2009-11-21 21:49:23 -05:00
|
|
|
# Failure application that will be called every time :warden is thrown from
|
|
|
|
# any strategy or hook. Responsible for redirect the user to the sign in
|
|
|
|
# page based on current scope and mapping. If no scope is given, redirect
|
|
|
|
# to the default_url.
|
2010-02-19 03:52:04 -05:00
|
|
|
class FailureApp < ActionController::Metal
|
|
|
|
include ActionController::RackDelegation
|
|
|
|
include ActionController::UrlFor
|
|
|
|
include ActionController::Redirecting
|
2009-11-21 21:49:23 -05:00
|
|
|
|
2010-02-19 03:52:04 -05:00
|
|
|
mattr_accessor :default_message
|
|
|
|
self.default_message = :unauthenticated
|
2009-10-22 18:12:00 -04:00
|
|
|
|
|
|
|
def self.call(env)
|
2010-02-19 03:52:04 -05:00
|
|
|
action(:respond).call(env)
|
2009-11-21 21:49:23 -05:00
|
|
|
end
|
2009-11-16 11:31:09 -05:00
|
|
|
|
2010-02-19 03:52:04 -05:00
|
|
|
def self.default_url_options(*args)
|
|
|
|
ApplicationController.default_url_options(*args)
|
2009-11-21 21:49:23 -05:00
|
|
|
end
|
|
|
|
|
2010-02-19 03:52:04 -05:00
|
|
|
def respond
|
2010-02-19 06:07:37 -05:00
|
|
|
scope = warden_options[:scope]
|
2009-11-21 21:49:23 -05:00
|
|
|
store_location!(scope)
|
2010-02-19 03:52:04 -05:00
|
|
|
redirect_to send(:"new_#{scope}_session_path", query_string_params)
|
2009-10-22 18:12:00 -04:00
|
|
|
end
|
2009-11-21 21:49:23 -05:00
|
|
|
|
2010-02-19 03:52:04 -05:00
|
|
|
protected
|
2009-11-21 21:49:23 -05:00
|
|
|
|
2010-02-19 03:52:04 -05:00
|
|
|
# Build the proper query string based on the given message.
|
|
|
|
def query_string_params
|
|
|
|
message = warden.try(:message) || warden_options[:message] || self.class.default_message
|
|
|
|
|
|
|
|
case message
|
|
|
|
when Symbol
|
|
|
|
{ message => true }
|
|
|
|
when String
|
|
|
|
{ :message => message }
|
|
|
|
else
|
|
|
|
{}
|
2009-11-21 21:49:23 -05:00
|
|
|
end
|
2010-02-19 03:52:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def warden
|
|
|
|
env['warden']
|
|
|
|
end
|
2009-11-21 21:49:23 -05:00
|
|
|
|
2010-02-19 03:52:04 -05:00
|
|
|
def warden_options
|
|
|
|
env['warden.options']
|
2009-11-21 21:49:23 -05:00
|
|
|
end
|
|
|
|
|
2010-03-11 12:39:32 -05:00
|
|
|
def attempted_path
|
|
|
|
warden_options[:attempted_path]
|
|
|
|
end
|
|
|
|
|
2009-11-21 21:49:23 -05:00
|
|
|
# Stores requested uri to redirect the user after signing in. We cannot use
|
|
|
|
# scoped session provided by warden here, since the user is not authenticated
|
|
|
|
# yet, but we still need to store the uri based on scope, so different scopes
|
|
|
|
# would never use the same uri to redirect.
|
|
|
|
def store_location!(scope)
|
2010-03-11 12:39:32 -05:00
|
|
|
session[:"#{scope}.return_to"] = attempted_path if request && request.get?
|
2009-11-21 21:49:23 -05:00
|
|
|
end
|
2009-10-22 18:12:00 -04:00
|
|
|
end
|
|
|
|
end
|