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

Updating sessions controller to use resource oriented style. Changing authenticate method to accept a hash of attributes.

This commit is contained in:
Carlos A. da Silva 2009-10-10 16:20:23 -03:00
parent 15c5d9e049
commit 4e263b96c9
7 changed files with 26 additions and 24 deletions

View file

@ -10,8 +10,9 @@ class SessionsController < ApplicationController
# POST /session
#
def create
if user = resource_class.authenticate(params[:session][:email], params[:session][:password]) #authenticate
self.current_user = user
self.resource = resource_class.authenticate(params[resource_name])
if resource #authenticate
self.current_user = resource
flash[:success] = I18n.t(:signed_in, :scope => [:devise, :sessions], :default => 'Signed in successfully.')
redirect_to root_path
else

View file

@ -1,6 +1,6 @@
<h2><%= t '.title', :default => 'Sign in', :scope => :devise %></h2>
<% form_for :session, :url => session_path do |f| -%>
<% form_for resource_name, :url => session_path do |f| -%>
<p><%= f.label :email %></p>
<p><%= f.text_field :email %></p>
<p><%= f.label :password %></p>

View file

@ -82,11 +82,12 @@ module Devise
module ClassMethods
# Authenticate a user based on email and password. Returns the
# authenticated user if it's valid or nil
# authenticated user if it's valid or nil.
# Attributes are :email and :password
#
def authenticate(email, password)
authenticable = self.find_by_email(email)
authenticable if authenticable.valid_password?(password) unless authenticable.nil?
def authenticate(attributes={})
authenticable = self.find_by_email(attributes[:email])
authenticable if authenticable.valid_password?(attributes[:password]) unless authenticable.nil?
end
end
end

View file

@ -81,7 +81,7 @@ module Devise
# Hook default authenticate to test whether the account is confirmed or not
# Returns the authenticated_user if it's confirmed, otherwise returns nil
#
def authenticate(email, password)
def authenticate(attributes={})
confirmable = super
confirmable if confirmable.confirmed? unless confirmable.nil?
end
@ -91,8 +91,8 @@ module Devise
# with an email not found error.
# Options must contain the user email
#
def send_confirmation_instructions(options={})
confirmable = find_or_initialize_with_error_by_email(options[:email])
def send_confirmation_instructions(attributes={})
confirmable = find_or_initialize_with_error_by_email(attributes[:email])
confirmable.reset_confirmation! unless confirmable.new_record?
confirmable
end
@ -102,8 +102,8 @@ module Devise
# If the user is already confirmed, create an error for the user
# Options must have the perishable_token
#
def confirm!(options={})
confirmable = find_or_initialize_with_error_by_perishable_token(options[:perishable_token])
def confirm!(attributes={})
confirmable = find_or_initialize_with_error_by_perishable_token(attributes[:perishable_token])
confirmable.confirm! unless confirmable.new_record?
confirmable
end

View file

@ -48,10 +48,10 @@ module Devise
# Attempt to find a user by it's email. If a record is found, send new
# password instructions to it. If not user is found, returns a new user
# with an email not found error.
# Options must contain the user email
# Attributes must contain the user email
#
def send_reset_password_instructions(options={})
recoverable = find_or_initialize_with_error_by_email(options[:email])
def send_reset_password_instructions(attributes={})
recoverable = find_or_initialize_with_error_by_email(attributes[:email])
recoverable.send_reset_password_instructions unless recoverable.new_record?
recoverable
end
@ -60,11 +60,11 @@ module Devise
# If a user is found, reset it's password and automatically try saving the
# record. If not user is found, returns a new user containing an error
# in perishable_token attribute.
# Options must contain perishable_token, password and confirmation
# Attributes must contain perishable_token, password and confirmation
#
def reset_password!(options={})
recoverable = find_or_initialize_with_error_by_perishable_token(options[:perishable_token])
recoverable.reset_password!(options[:password], options[:password_confirmation]) unless recoverable.new_record?
def reset_password!(attributes={})
recoverable = find_or_initialize_with_error_by_perishable_token(attributes[:perishable_token])
recoverable.reset_password!(attributes[:password], attributes[:password_confirmation]) unless recoverable.new_record?
recoverable
end
end

View file

@ -100,19 +100,19 @@ class AuthenticableTest < ActiveSupport::TestCase
test 'should authenticate a valid user with email and password and return it' do
user = create_user
User.any_instance.stubs(:confirmed?).returns(true)
authenticated_user = User.authenticate(user.email, user.password)
authenticated_user = User.authenticate(:email => user.email, :password => user.password)
assert_equal authenticated_user, user
end
test 'should return nil when authenticating an invalid user by email' do
user = create_user
authenticated_user = User.authenticate('another.email@email.com', user.password)
authenticated_user = User.authenticate(:email => 'another.email@email.com', :password => user.password)
assert_nil authenticated_user
end
test 'should return nil when authenticating an invalid user by password' do
user = create_user
authenticated_user = User.authenticate(user.email, 'another_password')
authenticated_user = User.authenticate(:email => user.email, :password => 'another_password')
assert_nil authenticated_user
end
end

View file

@ -63,14 +63,14 @@ class ConfirmableTest < ActiveSupport::TestCase
test 'should not authenticate a user not confirmed' do
user = create_user
authenticated_user = User.authenticate(user.email, user.password)
authenticated_user = User.authenticate(:email => user.email, :password => user.password)
assert_nil authenticated_user
end
test 'should authenticate a confirmed user' do
user = create_user
user.confirm!
authenticated_user = User.authenticate(user.email, user.password)
authenticated_user = User.authenticate(:email => user.email, :password => user.password)
assert_not_nil authenticated_user
assert_equal authenticated_user, user
end