2011-06-24 19:34:13 +00:00
|
|
|
# All Devise controllers are inherited from here.
|
2012-01-02 19:39:22 +00:00
|
|
|
class DeviseController < Devise.parent_controller.constantize
|
2012-01-02 21:01:28 +00:00
|
|
|
include Devise::Controllers::ScopedViews
|
|
|
|
|
|
|
|
helper DeviseHelper
|
|
|
|
|
|
|
|
helpers = %w(resource scope_name resource_name signed_in_resource
|
2012-02-15 15:49:50 +00:00
|
|
|
resource_class devise_mapping)
|
2012-01-02 21:01:28 +00:00
|
|
|
hide_action *helpers
|
|
|
|
helper_method *helpers
|
|
|
|
|
|
|
|
prepend_before_filter :assert_is_devise_resource!
|
|
|
|
respond_to *Mime::SET.map(&:to_sym) if mimes_for_respond_to.empty?
|
|
|
|
|
|
|
|
# Gets the actual resource stored in the instance variable
|
|
|
|
def resource
|
|
|
|
instance_variable_get(:"@#{resource_name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Proxy to devise map name
|
|
|
|
def resource_name
|
|
|
|
devise_mapping.name
|
|
|
|
end
|
|
|
|
alias :scope_name :resource_name
|
|
|
|
|
|
|
|
# Proxy to devise map class
|
|
|
|
def resource_class
|
|
|
|
devise_mapping.to
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a signed in resource from session (if one exists)
|
|
|
|
def signed_in_resource
|
|
|
|
warden.authenticate(:scope => resource_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Attempt to find the mapped route for devise based on request path
|
|
|
|
def devise_mapping
|
|
|
|
@devise_mapping ||= request.env["devise.mapping"]
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Checks whether it's a devise mapped resource or not.
|
|
|
|
def assert_is_devise_resource! #:nodoc:
|
|
|
|
unknown_action! <<-MESSAGE unless devise_mapping
|
|
|
|
Could not find devise mapping for path #{request.fullpath.inspect}.
|
|
|
|
Maybe you forgot to wrap your route inside the scope block? For example:
|
|
|
|
|
|
|
|
devise_scope :user do
|
|
|
|
match "/some/route" => "some_devise_controller"
|
2011-06-24 19:34:13 +00:00
|
|
|
end
|
2012-01-02 21:01:28 +00:00
|
|
|
MESSAGE
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns real navigational formats which are supported by Rails
|
|
|
|
def navigational_formats
|
|
|
|
@navigational_formats ||= Devise.navigational_formats.select { |format| Mime::EXTENSION_LOOKUP[format.to_s] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def unknown_action!(msg)
|
|
|
|
logger.debug "[Devise] #{msg}" if logger
|
2012-01-24 12:41:19 +00:00
|
|
|
raise AbstractController::ActionNotFound, msg
|
2012-01-02 21:01:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the resource creating an instance variable
|
|
|
|
def resource=(new_resource)
|
|
|
|
instance_variable_set(:"@#{resource_name}", new_resource)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Build a devise resource.
|
|
|
|
def build_resource(hash=nil)
|
|
|
|
hash ||= params[resource_name] || {}
|
|
|
|
self.resource = resource_class.new(hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper for use in before_filters where no authentication is required.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# before_filter :require_no_authentication, :only => :new
|
|
|
|
def require_no_authentication
|
|
|
|
assert_is_devise_resource!
|
|
|
|
return unless is_navigational_format?
|
|
|
|
no_input = devise_mapping.no_input_strategies
|
|
|
|
|
|
|
|
authenticated = if no_input.present?
|
|
|
|
args = no_input.dup.push :scope => resource_name
|
|
|
|
warden.authenticate?(*args)
|
|
|
|
else
|
|
|
|
warden.authenticated?(resource_name)
|
|
|
|
end
|
|
|
|
|
2012-03-03 11:09:26 +00:00
|
|
|
if authenticated && resource = warden.user(resource_name)
|
2012-01-02 21:01:28 +00:00
|
|
|
flash[:alert] = I18n.t("devise.failure.already_authenticated")
|
|
|
|
redirect_to after_sign_in_path_for(resource)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper for use after calling send_*_instructions methods on a resource.
|
|
|
|
# If we are in paranoid mode, we always act as if the resource was valid
|
|
|
|
# and instructions were sent.
|
|
|
|
def successfully_sent?(resource)
|
|
|
|
notice = if Devise.paranoid
|
|
|
|
resource.errors.clear
|
|
|
|
:send_paranoid_instructions
|
|
|
|
elsif resource.errors.empty?
|
|
|
|
:send_instructions
|
|
|
|
end
|
|
|
|
|
|
|
|
if notice
|
|
|
|
set_flash_message :notice, notice if is_navigational_format?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the flash message with :key, using I18n. By default you are able
|
|
|
|
# to setup your messages using specific resource scope, and if no one is
|
|
|
|
# found we look to default scope.
|
|
|
|
# Example (i18n locale file):
|
|
|
|
#
|
|
|
|
# en:
|
|
|
|
# devise:
|
|
|
|
# passwords:
|
|
|
|
# #default_scope_messages - only if resource_scope is not found
|
|
|
|
# user:
|
|
|
|
# #resource_scope_messages
|
|
|
|
#
|
|
|
|
# Please refer to README or en.yml locale file to check what messages are
|
|
|
|
# available.
|
2012-01-02 21:19:05 +00:00
|
|
|
def set_flash_message(key, kind, options={})
|
2012-01-02 21:01:28 +00:00
|
|
|
options[:scope] = "devise.#{controller_name}"
|
|
|
|
options[:default] = Array(options[:default]).unshift(kind.to_sym)
|
|
|
|
options[:resource_name] = resource_name
|
2012-02-15 15:55:19 +00:00
|
|
|
options = devise_i18n_options(options) if respond_to?(:devise_i18n_options, true)
|
2012-01-02 21:01:28 +00:00
|
|
|
message = I18n.t("#{resource_name}.#{kind}", options)
|
|
|
|
flash[key] = message if message.present?
|
|
|
|
end
|
|
|
|
|
2012-01-02 21:19:05 +00:00
|
|
|
def clean_up_passwords(object)
|
2012-01-02 21:01:28 +00:00
|
|
|
object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_with_navigational(*args, &block)
|
|
|
|
respond_with(*args) do |format|
|
|
|
|
format.any(*navigational_formats, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_format
|
|
|
|
@request_format ||= request.format.try(:ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_navigational_format?
|
|
|
|
Devise.navigational_formats.include?(request.format.try(:ref))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Override prefixes to consider the scoped view.
|
2012-01-02 21:19:05 +00:00
|
|
|
def _prefixes #:nodoc:
|
2012-02-15 15:55:19 +00:00
|
|
|
@_prefixes ||= if self.class.scoped_views? && devise_mapping
|
2012-01-02 21:01:28 +00:00
|
|
|
super.unshift("#{devise_mapping.scoped_path}/#{controller_name}")
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2012-03-03 17:30:53 +00:00
|
|
|
end
|