From 4e263b96c977d49bfa495b328a7d0e7344286dfe Mon Sep 17 00:00:00 2001 From: "Carlos A. da Silva" Date: Sat, 10 Oct 2009 16:20:23 -0300 Subject: [PATCH] Updating sessions controller to use resource oriented style. Changing authenticate method to accept a hash of attributes. --- app/controllers/sessions_controller.rb | 5 +++-- app/views/sessions/new.html.erb | 2 +- lib/devise/models/authenticable.rb | 9 +++++---- lib/devise/models/confirmable.rb | 10 +++++----- lib/devise/models/recoverable.rb | 14 +++++++------- test/models/authenticable_test.rb | 6 +++--- test/models/confirmable_test.rb | 4 ++-- 7 files changed, 26 insertions(+), 24 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 64acfcc0..250b6c16 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index e6d13d08..50fe1e58 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -1,6 +1,6 @@

<%= t '.title', :default => 'Sign in', :scope => :devise %>

-<% form_for :session, :url => session_path do |f| -%> +<% form_for resource_name, :url => session_path do |f| -%>

<%= f.label :email %>

<%= f.text_field :email %>

<%= f.label :password %>

diff --git a/lib/devise/models/authenticable.rb b/lib/devise/models/authenticable.rb index 7ac24456..cbc1f356 100644 --- a/lib/devise/models/authenticable.rb +++ b/lib/devise/models/authenticable.rb @@ -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 diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index 7ee3952e..94fb982f 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -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 diff --git a/lib/devise/models/recoverable.rb b/lib/devise/models/recoverable.rb index f951af9e..95a0a03a 100644 --- a/lib/devise/models/recoverable.rb +++ b/lib/devise/models/recoverable.rb @@ -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 diff --git a/test/models/authenticable_test.rb b/test/models/authenticable_test.rb index f0271a62..e69e58df 100644 --- a/test/models/authenticable_test.rb +++ b/test/models/authenticable_test.rb @@ -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 diff --git a/test/models/confirmable_test.rb b/test/models/confirmable_test.rb index 6e19fee7..1dde058b 100644 --- a/test/models/confirmable_test.rb +++ b/test/models/confirmable_test.rb @@ -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