From edee511cd13a1bacafb2fd9493eaa3e3c34fb160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 25 Mar 2011 15:39:08 +0100 Subject: [PATCH] Rename active? to active_for_authentication? --- lib/devise/hooks/activatable.rb | 4 ++-- lib/devise/models/authenticatable.rb | 30 +++++++++++++++++++--------- lib/devise/models/confirmable.rb | 4 ++-- lib/devise/models/lockable.rb | 4 ++-- test/models/confirmable_test.rb | 18 ++++++++--------- test/models/lockable_test.rb | 6 +++--- test/test_helpers_test.rb | 4 ++-- 7 files changed, 41 insertions(+), 29 deletions(-) diff --git a/lib/devise/hooks/activatable.rb b/lib/devise/hooks/activatable.rb index d2d86d41..e9b2d910 100644 --- a/lib/devise/hooks/activatable.rb +++ b/lib/devise/hooks/activatable.rb @@ -1,9 +1,9 @@ # Deny user access whenever his account is not active yet. All strategies that inherits from -# Devise::Strategies::Authenticatable and uses the validate already check if the user is active? +# Devise::Strategies::Authenticatable and uses the validate already check if the user is active_for_authentication? # before actively signing him in. However, we need this as hook to validate the user activity # in each request and in case the user is using other strategies beside Devise ones. Warden::Manager.after_set_user do |record, warden, options| - if record && record.respond_to?(:active?) && !record.active? + if record && record.respond_to?(:active_for_authentication?) && !record.active_for_authentication? scope = options[:scope] warden.logout(scope) throw :warden, :scope => scope, :message => record.inactive_message diff --git a/lib/devise/models/authenticatable.rb b/lib/devise/models/authenticatable.rb index 28e5c95a..639e2629 100644 --- a/lib/devise/models/authenticatable.rb +++ b/lib/devise/models/authenticatable.rb @@ -24,19 +24,19 @@ module Devise # * +params_authenticatable+: if this model allows authentication through request params. By default true. # It also accepts an array specifying the strategies that should allow params authentication. # - # == Active? + # == active_for_authentication? # # Before authenticating a user and in each request, Devise checks if your model is active by - # calling model.active?. This method is overwriten by other devise modules. For instance, - # :confirmable overwrites .active? to only return true if your model was confirmed. + # calling model.active_for_authentication?. This method is overwriten by other devise modules. For instance, + # :confirmable overwrites .active_for_authentication? to only return true if your model was confirmed. # # You overwrite this method yourself, but if you do, don't forget to call super: # - # def active? + # def active_for_authentication? # super && special_condition_is_valid? # end # - # Whenever active? returns false, Devise asks the reason why your model is inactive using + # Whenever active_for_authentication? returns false, Devise asks the reason why your model is inactive using # the inactive_message method. You can overwrite it as well: # # def inactive_message @@ -55,10 +55,10 @@ module Devise # find_for_authentication are the methods used in a Warden::Strategy to check # if a model should be signed in or not. # - # However, you should not overwrite this method, you should overwrite active? and - # inactive_message instead. + # However, you should not overwrite this method, you should overwrite active_for_authentication? + # and inactive_message instead. def valid_for_authentication? - if active? + if active_for_authentication? block_given? ? yield : true else inactive_message @@ -66,7 +66,19 @@ module Devise end def active? - true + ActiveSupport::Deprecation.warn "[DEVISE] active? is deprecated, please use active_for_authentication? instead.", caller + active_for_authentication? + end + + def active_for_authentication? + my_methods = self.class.instance_methods(false) + if my_methods.include?("active?") || my_methods.include?(:active?) + ActiveSupport::Deprecation.warn "[DEVISE] Overriding active? is deprecated to avoid conflicts. " \ + "Please use active_for_authentication? instead.", caller + active? + else + true + end end def inactive_message diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index a16ea15a..6dbdb18b 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -55,11 +55,11 @@ module Devise unless_confirmed { send_confirmation_instructions } end - # Overwrites active? from Devise::Models::Activatable for confirmation + # Overwrites active_for_authentication? for confirmation # by verifying whether a user is active to sign in or not. If the user # is already confirmed, it should never be blocked. Otherwise we need to # calculate if the confirm time has not expired for this user. - def active? + def active_for_authentication? super && (!confirmation_required? || confirmed? || confirmation_period_valid?) end diff --git a/lib/devise/models/lockable.rb b/lib/devise/models/lockable.rb index 55aef2ef..dd917468 100644 --- a/lib/devise/models/lockable.rb +++ b/lib/devise/models/lockable.rb @@ -57,9 +57,9 @@ module Devise if_access_locked { send_unlock_instructions } end - # Overwrites active? from Devise::Models::Activatable for locking purposes + # Overwrites active_for_authentication? from Devise::Models::Activatable for locking purposes # by verifying whether a user is active to sign in or not based on locked? - def active? + def active_for_authentication? super && !access_locked? end diff --git a/test/models/confirmable_test.rb b/test/models/confirmable_test.rb index 50420dd6..a3273614 100644 --- a/test/models/confirmable_test.rb +++ b/test/models/confirmable_test.rb @@ -167,10 +167,10 @@ class ConfirmableTest < ActiveSupport::TestCase swap Devise, :confirm_within => 1.day do user = new_user user.confirmation_sent_at = 2.days.ago - assert_not user.active? + assert_not user.active_for_authentication? Devise.confirm_within = 3.days - assert user.active? + assert user.active_for_authentication? end end @@ -180,35 +180,35 @@ class ConfirmableTest < ActiveSupport::TestCase user = create_user user.confirmation_sent_at = 4.days.ago - assert user.active? + assert user.active_for_authentication? user.confirmation_sent_at = 5.days.ago - assert_not user.active? + assert_not user.active_for_authentication? end end test 'should be active when already confirmed' do user = create_user assert_not user.confirmed? - assert_not user.active? + assert_not user.active_for_authentication? user.confirm! assert user.confirmed? - assert user.active? + assert user.active_for_authentication? end test 'should not be active when confirm in is zero' do Devise.confirm_within = 0.days user = create_user user.confirmation_sent_at = Date.today - assert_not user.active? + assert_not user.active_for_authentication? end test 'should not be active without confirmation' do user = create_user user.confirmation_sent_at = nil user.save - assert_not user.reload.active? + assert_not user.reload.active_for_authentication? end test 'should be active without confirmation when confirmation is not required' do @@ -216,7 +216,7 @@ class ConfirmableTest < ActiveSupport::TestCase user.instance_eval { def confirmation_required?; false end } user.confirmation_sent_at = nil user.save - assert user.reload.active? + assert user.reload.active_for_authentication? end test 'should find a user to send email instructions for the user confirm it\'s email by authentication_keys' do diff --git a/test/models/lockable_test.rb b/test/models/lockable_test.rb index ff41b101..02970de5 100644 --- a/test/models/lockable_test.rb +++ b/test/models/lockable_test.rb @@ -47,12 +47,12 @@ class LockableTest < ActiveSupport::TestCase assert user.access_locked? end - test "active? should be the opposite of locked?" do + test "active_for_authentication? should be the opposite of locked?" do user = create_user user.confirm! - assert user.active? + assert user.active_for_authentication? user.lock_access! - assert_not user.active? + assert_not user.active_for_authentication? end test "should unlock a user by cleaning locked_at, falied_attempts and unlock_token" do diff --git a/test/test_helpers_test.rb b/test/test_helpers_test.rb index fcb4e382..c4dd91f3 100644 --- a/test/test_helpers_test.rb +++ b/test/test_helpers_test.rb @@ -19,7 +19,7 @@ class TestHelpersTest < ActionController::TestCase test "redirects if attempting to access a page with an unconfirmed account" do swap Devise, :confirm_within => 0 do user = create_user - assert !user.active? + assert !user.active_for_authentication? sign_in user get :index @@ -30,7 +30,7 @@ class TestHelpersTest < ActionController::TestCase test "returns nil if accessing current_user with an unconfirmed account" do swap Devise, :confirm_within => 0 do user = create_user - assert !user.active? + assert !user.active_for_authentication? sign_in user get :accept, :id => user