1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/lib/devise/failure_app.rb

63 lines
1.7 KiB
Ruby
Raw Normal View History

2010-02-19 03:52:04 -05:00
require "action_controller/metal"
2009-10-22 18:12:00 -04:00
module Devise
# 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
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)
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)
end
2010-02-19 03:52:04 -05:00
def respond
scope = warden_options[:scope]
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
2010-02-19 03:52:04 -05:00
protected
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
{}
end
2010-02-19 03:52:04 -05:00
end
def warden
env['warden']
end
2010-02-19 03:52:04 -05:00
def warden_options
env['warden.options']
end
# 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)
session[:"#{scope}.return_to"] = request.fullpath if request && request.get?
end
2009-10-22 18:12:00 -04:00
end
end