2011-06-24 15:34:13 -04:00
|
|
|
# All Devise controllers are inherited from here.
|
2012-01-02 14:39:22 -05:00
|
|
|
class DeviseController < Devise.parent_controller.constantize
|
2012-01-02 16:01:28 -05:00
|
|
|
include Devise::Controllers::ScopedViews
|
|
|
|
|
|
|
|
helper DeviseHelper
|
|
|
|
|
|
|
|
helpers = %w(resource scope_name resource_name signed_in_resource
|
2012-05-15 04:07:02 -04:00
|
|
|
resource_class resource_params devise_mapping)
|
2014-07-11 15:06:56 -04:00
|
|
|
helper_method(*helpers)
|
2012-01-02 16:01:28 -05:00
|
|
|
|
|
|
|
prepend_before_filter :assert_is_devise_resource!
|
2012-12-13 03:20:46 -05:00
|
|
|
respond_to :html if mimes_for_respond_to.empty?
|
2012-01-02 16:01:28 -05:00
|
|
|
|
2015-05-26 03:53:38 -04:00
|
|
|
# Override prefixes to consider the scoped view.
|
|
|
|
# Notice we need to check for the request due to a bug in
|
|
|
|
# Action Controller tests that forces _prefixes to be
|
|
|
|
# loaded before even having a request object.
|
|
|
|
#
|
|
|
|
# This method should be public as it is is in ActionPack
|
|
|
|
# itself. Changing its visibility may break other gems.
|
|
|
|
def _prefixes #:nodoc:
|
|
|
|
@_prefixes ||= if self.class.scoped_views? && request && devise_mapping
|
|
|
|
["#{devise_mapping.scoped_path}/#{controller_name}"] + super
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-01 15:05:20 -05:00
|
|
|
protected
|
|
|
|
|
2012-01-02 16:01:28 -05:00
|
|
|
# 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
|
2014-02-25 11:42:55 -05:00
|
|
|
warden.authenticate(scope: resource_name)
|
2012-01-02 16:01:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Attempt to find the mapped route for devise based on request path
|
|
|
|
def devise_mapping
|
|
|
|
@devise_mapping ||= request.env["devise.mapping"]
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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}.
|
2012-06-07 07:34:13 -04:00
|
|
|
This may happen for two reasons:
|
|
|
|
|
|
|
|
1) You forgot to wrap your route inside the scope block. For example:
|
|
|
|
|
|
|
|
devise_scope :user do
|
2013-01-22 20:13:36 -05:00
|
|
|
get "/some/route" => "some_devise_controller"
|
2012-06-07 07:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
2) You are testing a Devise controller bypassing the router.
|
|
|
|
If so, you can explicitly tell Devise which mapping to use:
|
2012-06-26 10:58:50 -04:00
|
|
|
|
2012-06-07 07:34:13 -04:00
|
|
|
@request.env["devise.mapping"] = Devise.mappings[:user]
|
2012-01-02 16:01:28 -05: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 07:41:19 -05:00
|
|
|
raise AbstractController::ActionNotFound, msg
|
2012-01-02 16:01:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the resource creating an instance variable
|
|
|
|
def resource=(new_resource)
|
|
|
|
instance_variable_set(:"@#{resource_name}", new_resource)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper for use in before_filters where no authentication is required.
|
|
|
|
#
|
|
|
|
# Example:
|
2014-02-25 11:42:55 -05:00
|
|
|
# before_filter :require_no_authentication, only: :new
|
2012-01-02 16:01:28 -05:00
|
|
|
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?
|
2014-02-25 11:42:55 -05:00
|
|
|
args = no_input.dup.push scope: resource_name
|
2012-01-02 16:01:28 -05:00
|
|
|
warden.authenticate?(*args)
|
|
|
|
else
|
|
|
|
warden.authenticated?(resource_name)
|
|
|
|
end
|
|
|
|
|
2012-03-03 06:09:26 -05:00
|
|
|
if authenticated && resource = warden.user(resource_name)
|
2012-01-02 16:01:28 -05: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
|
2013-11-01 16:47:40 -04:00
|
|
|
set_flash_message :notice, notice if is_flashing_format?
|
2012-01-02 16:01:28 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the flash message with :key, using I18n. By default you are able
|
2014-10-19 00:28:20 -04:00
|
|
|
# to setup your messages using specific resource scope, and if no message is
|
|
|
|
# found we look to the default scope. Set the "now" options key to a true
|
|
|
|
# value to populate the flash.now hash in lieu of the default flash hash (so
|
|
|
|
# the flash message will be available to the current action instead of the
|
|
|
|
# next action).
|
2012-01-02 16:01:28 -05:00
|
|
|
# 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.
|
2013-04-07 02:52:11 -04:00
|
|
|
def set_flash_message(key, kind, options = {})
|
|
|
|
message = find_message(kind, options)
|
2014-10-19 00:28:20 -04:00
|
|
|
if options[:now]
|
|
|
|
flash.now[key] = message if message.present?
|
|
|
|
else
|
|
|
|
flash[key] = message if message.present?
|
|
|
|
end
|
2013-04-06 12:46:21 -04:00
|
|
|
end
|
|
|
|
|
2014-12-16 16:14:32 -05:00
|
|
|
# Sets minimum password length to show to user
|
|
|
|
def set_minimum_password_length
|
2014-12-27 08:22:40 -05:00
|
|
|
if devise_mapping.validatable?
|
2014-12-16 16:14:32 -05:00
|
|
|
@minimum_password_length = resource_class.password_length.min
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-24 11:29:53 -04:00
|
|
|
def devise_i18n_options(options)
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
2013-04-06 12:46:21 -04:00
|
|
|
# Get message for given
|
2013-04-07 02:52:11 -04:00
|
|
|
def find_message(kind, options = {})
|
2015-01-07 08:35:45 -05:00
|
|
|
options[:scope] ||= translation_scope
|
2012-01-02 16:01:28 -05:00
|
|
|
options[:default] = Array(options[:default]).unshift(kind.to_sym)
|
|
|
|
options[:resource_name] = resource_name
|
2013-10-24 11:29:53 -04:00
|
|
|
options = devise_i18n_options(options)
|
2013-04-06 12:46:21 -04:00
|
|
|
I18n.t("#{options[:resource_name]}.#{kind}", options)
|
2012-01-02 16:01:28 -05:00
|
|
|
end
|
|
|
|
|
2015-01-07 08:35:45 -05:00
|
|
|
# Controllers inheriting DeviseController are advised to override this
|
|
|
|
# method so that other controllers inheriting from them would use
|
|
|
|
# existing translations.
|
|
|
|
def translation_scope
|
|
|
|
"devise.#{controller_name}"
|
|
|
|
end
|
|
|
|
|
2012-01-02 16:19:05 -05:00
|
|
|
def clean_up_passwords(object)
|
2012-01-02 16:01:28 -05: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
|
2013-03-13 12:37:54 -04:00
|
|
|
|
|
|
|
def resource_params
|
2013-04-10 11:33:50 -04:00
|
|
|
params.fetch(resource_name, {})
|
2013-03-13 12:37:54 -04:00
|
|
|
end
|
2014-10-28 07:17:06 -04:00
|
|
|
|
|
|
|
ActiveSupport.run_load_hooks(:devise_controller, self)
|
2012-03-03 12:30:53 -05:00
|
|
|
end
|