Skip to content

Commit

Permalink
Do not send confirmation e-mail when e-mail changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 30, 2009
1 parent 9d56aa9 commit 8c1bab4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Expand Up @@ -3,6 +3,7 @@

* deprecations
* Renamed confirm_in to confirm_within
* [#14] Do not send confirmation messages when user changes his e-mail

== 0.2.3

Expand Down
20 changes: 4 additions & 16 deletions lib/devise/models/confirmable.rb
Expand Up @@ -34,8 +34,8 @@ def self.included(base)
base.class_eval do
extend ClassMethods

before_save :reset_confirmation, :if => :email_changed?
after_save :send_confirmation_instructions, :if => :email_changed?
before_create :generate_confirmation_token
after_create :send_confirmation_instructions
end
end

Expand Down Expand Up @@ -64,7 +64,7 @@ def send_confirmation_instructions
# confirming it's account
def reset_confirmation!
unless_confirmed do
reset_confirmation
generate_confirmation_token
save(false)
send_confirmation_instructions
end
Expand Down Expand Up @@ -115,26 +115,14 @@ def unless_confirmed
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
# this token is being generated
def generate_confirmation_token
self.confirmed_at = nil
self.confirmation_token = friendly_token
self.confirmation_sent_at = Time.now.utc
end

# Resets the confirmation token with and save the record without
# validating.
def generate_confirmation_token!
generate_confirmation_token && save(false)
end

module ClassMethods

# Attempt to find a user by it's email. If a record is found, send new
Expand Down
25 changes: 5 additions & 20 deletions test/models/confirmable_test.rb
Expand Up @@ -149,38 +149,23 @@ def setup
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.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
user.save!
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
assert_not user.confirmed?
user.confirm!
assert user.confirmed?
user.email = 'new_test@example.com'
user.save!
assert_not user.reload.confirmed?
end

test 'should reset confirmation token when updating email' do
user = create_user
token = user.confirmation_token
user.email = 'new_test@example.com'
user.save!
assert_not_equal token, user.reload.confirmation_token
user.reload
assert user.confirmed?
assert_nil user.confirmation_token
end

test 'should not be able to send instructions if the user is already confirmed' do
Expand Down

0 comments on commit 8c1bab4

Please sign in to comment.