mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Allow overwriting find for authentication method.
This commit is contained in:
parent
ab81bc344f
commit
6829619330
5 changed files with 35 additions and 10 deletions
|
@ -60,16 +60,23 @@ module Devise
|
||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
# Authenticate a user based on email and password. Returns the
|
# Authenticate a user based on configured attribute keys. Returns the
|
||||||
# authenticated user if it's valid or nil.
|
# authenticated user if it's valid or nil. Attributes are by default
|
||||||
# Attributes are :email and :password
|
# :email and :password, the latter is always required.
|
||||||
def authenticate(attributes={})
|
def authenticate(attributes={})
|
||||||
return unless authentication_keys.all? { |k| attributes[k].present? }
|
return unless authentication_keys.all? { |k| attributes[k].present? }
|
||||||
conditions = attributes.slice(*authentication_keys)
|
conditions = attributes.slice(*authentication_keys)
|
||||||
authenticatable = find(:first, :conditions => conditions)
|
authenticatable = find_for_authentication(conditions)
|
||||||
authenticatable if authenticatable.try(:valid_password?, attributes[:password])
|
authenticatable if authenticatable.try(:valid_password?, attributes[:password])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Find first record based on conditions given (ie by the sign in form).
|
||||||
|
# Overwrite to add customized conditions, create a join, or maybe use a
|
||||||
|
# namedscope to filter records while authenticating.
|
||||||
|
def find_for_authentication(conditions)
|
||||||
|
find(:first, :conditions => conditions)
|
||||||
|
end
|
||||||
|
|
||||||
# Attempt to find a user by it's email. If not user is found, returns a
|
# Attempt to find a user by it's email. If not user is found, returns a
|
||||||
# new user with an email not found error.
|
# new user with an email not found error.
|
||||||
def find_or_initialize_with_error_by_email(email)
|
def find_or_initialize_with_error_by_email(email)
|
||||||
|
|
|
@ -136,6 +136,16 @@ class AuthenticatableTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'should allow overwriting find for authentication conditions' do
|
||||||
|
admin = Admin.create!(valid_attributes)
|
||||||
|
assert_not_nil Admin.authenticate(:email => admin.email, :password => admin.password)
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'should never authenticate an account' do
|
||||||
|
account = Account.create!(valid_attributes)
|
||||||
|
assert_nil Account.authenticate(:email => account.email, :password => account.password)
|
||||||
|
end
|
||||||
|
|
||||||
test 'should serialize user into session' do
|
test 'should serialize user into session' do
|
||||||
user = create_user
|
user = create_user
|
||||||
assert_equal [User, user.id], User.serialize_into_session(user)
|
assert_equal [User, user.id], User.serialize_into_session(user)
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
class Account < ActiveRecord::Base
|
class Account < ActiveRecord::Base
|
||||||
devise :all
|
devise :all
|
||||||
|
|
||||||
|
def self.find_for_authentication(conditions)
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
class Admin < ActiveRecord::Base
|
class Admin < ActiveRecord::Base
|
||||||
devise :all, :except => [:recoverable, :confirmable, :rememberable, :validatable]
|
devise :all, :except => [:recoverable, :confirmable, :rememberable, :validatable]
|
||||||
|
|
||||||
|
def self.find_for_authentication(conditions)
|
||||||
|
last(:conditions => conditions)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,12 +15,12 @@ ActiveRecord::Base.logger = Logger.new(nil)
|
||||||
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
||||||
|
|
||||||
ActiveRecord::Schema.define(:version => 1) do
|
ActiveRecord::Schema.define(:version => 1) do
|
||||||
[:users, :admins].each do |table|
|
[:users, :admins, :accounts].each do |table|
|
||||||
create_table table do |t|
|
create_table table do |t|
|
||||||
t.authenticatable :null => table == :admins
|
t.authenticatable :null => table == :admins
|
||||||
t.string :username if table == :users
|
|
||||||
|
|
||||||
if table == :users
|
if table != :admin
|
||||||
|
t.string :username
|
||||||
t.confirmable
|
t.confirmable
|
||||||
t.recoverable
|
t.recoverable
|
||||||
t.rememberable
|
t.rememberable
|
||||||
|
|
Loading…
Reference in a new issue