Do not send confirmation e-mail when e-mail changes.

This commit is contained in:
José Valim 2009-10-30 07:49:18 -02:00
parent 9d56aa9603
commit 8c1bab4951
3 changed files with 10 additions and 36 deletions

View File

@ -3,6 +3,7 @@
* deprecations * deprecations
* Renamed confirm_in to confirm_within * Renamed confirm_in to confirm_within
* [#14] Do not send confirmation messages when user changes his e-mail
== 0.2.3 == 0.2.3

View File

@ -34,8 +34,8 @@ module Devise
base.class_eval do base.class_eval do
extend ClassMethods extend ClassMethods
before_save :reset_confirmation, :if => :email_changed? before_create :generate_confirmation_token
after_save :send_confirmation_instructions, :if => :email_changed? after_create :send_confirmation_instructions
end end
end end
@ -64,7 +64,7 @@ module Devise
# confirming it's account # confirming it's account
def reset_confirmation! def reset_confirmation!
unless_confirmed do unless_confirmed do
reset_confirmation generate_confirmation_token
save(false) save(false)
send_confirmation_instructions send_confirmation_instructions
end end
@ -115,26 +115,14 @@ module Devise
end end
end end
# Remove confirmation date from the user, ensuring after a user update
# it's email, it won't be able to sign in without confirming it.
def reset_confirmation
generate_confirmation_token
self.confirmed_at = nil
end
# Generates a new random token for confirmation, and stores the time # Generates a new random token for confirmation, and stores the time
# this token is being generated # this token is being generated
def generate_confirmation_token def generate_confirmation_token
self.confirmed_at = nil
self.confirmation_token = friendly_token self.confirmation_token = friendly_token
self.confirmation_sent_at = Time.now.utc self.confirmation_sent_at = Time.now.utc
end end
# Resets the confirmation token with and save the record without
# validating.
def generate_confirmation_token!
generate_confirmation_token && save(false)
end
module ClassMethods module ClassMethods
# Attempt to find a user by it's email. If a record is found, send new # Attempt to find a user by it's email. If a record is found, send new

View File

@ -149,38 +149,23 @@ class ConfirmableTest < ActiveSupport::TestCase
end end
end end
test 'should resend email instructions for the user reconfirming the email if it has changed' do test 'should not resend email instructions if the user change his email' do
user = create_user user = create_user
user.email = 'new_test@example.com' user.email = 'new_test@example.com'
assert_email_sent do
user.save!
end
end
test 'should not resend email instructions if the user is updated but the email is not' do
user = create_user
user.confirmed_at = Time.now
assert_email_not_sent do assert_email_not_sent do
user.save! user.save!
end end
end end
test 'should reset confirmation status when updating email' do test 'should not reset confirmation status or token when updating email' do
user = create_user user = create_user
assert_not user.confirmed?
user.confirm! user.confirm!
assert user.confirmed?
user.email = 'new_test@example.com' user.email = 'new_test@example.com'
user.save! user.save!
assert_not user.reload.confirmed?
end
test 'should reset confirmation token when updating email' do user.reload
user = create_user assert user.confirmed?
token = user.confirmation_token assert_nil user.confirmation_token
user.email = 'new_test@example.com'
user.save!
assert_not_equal token, user.reload.confirmation_token
end end
test 'should not be able to send instructions if the user is already confirmed' do test 'should not be able to send instructions if the user is already confirmed' do