mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
changed case_insensitive_keys config setting to an array and added downcasing of keys as a before filter on database authentication module
This commit is contained in:
parent
26b1bc4107
commit
e911abf13b
4 changed files with 22 additions and 18 deletions
|
@ -59,17 +59,17 @@ module Devise
|
||||||
mattr_accessor :stretches
|
mattr_accessor :stretches
|
||||||
@@stretches = 10
|
@@stretches = 10
|
||||||
|
|
||||||
# Keys used when authenticating an user.
|
# Keys used when authenticating a user.
|
||||||
mattr_accessor :authentication_keys
|
mattr_accessor :authentication_keys
|
||||||
@@authentication_keys = [ :email ]
|
@@authentication_keys = [ :email ]
|
||||||
|
|
||||||
# Request keys used when authenticating an user.
|
# Request keys used when authenticating a user.
|
||||||
mattr_accessor :request_keys
|
mattr_accessor :request_keys
|
||||||
@@request_keys = []
|
@@request_keys = []
|
||||||
|
|
||||||
# If authentication keys should be case-insensitive by default.
|
# Keys that should be case-insensitive.
|
||||||
mattr_accessor :case_insensitive_keys
|
mattr_accessor :case_insensitive_keys
|
||||||
@@case_insensitive_keys = false
|
@@case_insensitive_keys = [ :email ]
|
||||||
|
|
||||||
# If http authentication is enabled by default.
|
# If http authentication is enabled by default.
|
||||||
mattr_accessor :http_authenticatable
|
mattr_accessor :http_authenticatable
|
||||||
|
|
|
@ -100,9 +100,7 @@ module Devise
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
def find_for_authentication(conditions)
|
def find_for_authentication(conditions)
|
||||||
if case_insensitive_keys
|
case_insensitive_keys.each { |k| attributes[k].try(:downcase!) }
|
||||||
authentication_keys.each { |k| conditions[k].try(:downcase!) }
|
|
||||||
end
|
|
||||||
to_adapter.find_first(conditions)
|
to_adapter.find_first(conditions)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -113,9 +111,7 @@ module Devise
|
||||||
|
|
||||||
# Find an initialize a group of attributes based on a list of required attributes.
|
# Find an initialize a group of attributes based on a list of required attributes.
|
||||||
def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid) #:nodoc:
|
def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid) #:nodoc:
|
||||||
if case_insensitive_keys
|
case_insensitive_keys.each { |k| attributes[k].try(:downcase!) }
|
||||||
authentication_keys.each { |k| attributes[k].try(:downcase!) }
|
|
||||||
end
|
|
||||||
|
|
||||||
attributes = attributes.slice(*required_attributes)
|
attributes = attributes.slice(*required_attributes)
|
||||||
attributes.delete_if { |key, value| value.blank? }
|
attributes.delete_if { |key, value| value.blank? }
|
||||||
|
|
|
@ -22,6 +22,7 @@ module Devise
|
||||||
included do
|
included do
|
||||||
attr_reader :password, :current_password
|
attr_reader :password, :current_password
|
||||||
attr_accessor :password_confirmation
|
attr_accessor :password_confirmation
|
||||||
|
before_save :downcase_keys
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generates password encryption based on the given value.
|
# Generates password encryption based on the given value.
|
||||||
|
@ -73,13 +74,18 @@ module Devise
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
# Downcase case-insensitive keys
|
||||||
|
def downcase_keys
|
||||||
|
self.class.case_insensitive_keys.each { |k| self[k].try(:downcase!) }
|
||||||
|
end
|
||||||
|
|
||||||
# Digests the password using bcrypt.
|
# Digests the password using bcrypt.
|
||||||
def password_digest(password)
|
def password_digest(password)
|
||||||
::BCrypt::Password.create("#{password}#{self.class.pepper}", :cost => self.class.stretches).to_s
|
::BCrypt::Password.create("#{password}#{self.class.pepper}", :cost => self.class.stretches).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
Devise::Models.config(self, :pepper, :stretches)
|
Devise::Models.config(self, :pepper, :stretches, :case_insensitive_keys)
|
||||||
|
|
||||||
# We assume this method already gets the sanitized values from the
|
# We assume this method already gets the sanitized values from the
|
||||||
# DatabaseAuthenticatable strategy. If you are using this method on
|
# DatabaseAuthenticatable strategy. If you are using this method on
|
||||||
|
|
|
@ -15,24 +15,26 @@ Devise.setup do |config|
|
||||||
require 'devise/orm/<%= options[:orm] %>'
|
require 'devise/orm/<%= options[:orm] %>'
|
||||||
|
|
||||||
# ==> Configuration for any authentication mechanism
|
# ==> Configuration for any authentication mechanism
|
||||||
# Configure which keys are used when authenticating an user. By default is
|
# Configure which keys are used when authenticating a user. The default is
|
||||||
# just :email. You can configure it to use [:username, :subdomain], so for
|
# just :email. You can configure it to use [:username, :subdomain], so for
|
||||||
# authenticating an user, both parameters are required. Remember that those
|
# authenticating a user, both parameters are required. Remember that those
|
||||||
# parameters are used only when authenticating and not when retrieving from
|
# parameters are used only when authenticating and not when retrieving from
|
||||||
# session. If you need permissions, you should implement that in a before filter.
|
# session. If you need permissions, you should implement that in a before filter.
|
||||||
# You can also supply hash where the value is a boolean expliciting if authentication
|
# You can also supply a hash where the value is a boolean determining whether
|
||||||
# should be aborted or not if the value is not present. By default is empty.
|
# or not authentication should be aborted when the value is not present.
|
||||||
# config.authentication_keys = [ :email ]
|
# config.authentication_keys = [ :email ]
|
||||||
|
|
||||||
# Configure parameters from the request object used for authentication. Each entry
|
# Configure parameters from the request object used for authentication. Each entry
|
||||||
# given should be a request method and it will automatically be passed to
|
# given should be a request method and it will automatically be passed to the
|
||||||
# find_for_authentication method and considered in your model lookup. For instance,
|
# find_for_authentication method and considered in your model lookup. For instance,
|
||||||
# if you set :request_keys to [:subdomain], :subdomain will be used on authentication.
|
# if you set :request_keys to [:subdomain], :subdomain will be used on authentication.
|
||||||
# The same considerations mentioned for authentication_keys also apply to request_keys.
|
# The same considerations mentioned for authentication_keys also apply to request_keys.
|
||||||
# config.request_keys = []
|
# config.request_keys = []
|
||||||
|
|
||||||
# If authentication keys should be case-insensitive. False by default.
|
# Configure which authentication keys should be case-insensitive.
|
||||||
# config.case_insensitive_keys = false
|
# These keys will be downcased upon creating or modifying a user and when used
|
||||||
|
# to authenticate or find a user. Default is :email.
|
||||||
|
# config.case_insensitive_keys = [ :email ]
|
||||||
|
|
||||||
# Tell if authentication through request.params is enabled. True by default.
|
# Tell if authentication through request.params is enabled. True by default.
|
||||||
# config.params_authenticatable = true
|
# config.params_authenticatable = true
|
||||||
|
|
Loading…
Reference in a new issue