Skip to content

Commit 94d859c

Browse files
author
Sergio Cambra
committed
clear invitation token when password is reset
1 parent ecafe06 commit 94d859c

File tree

7 files changed

+20
-5
lines changed

7 files changed

+20
-5
lines changed

lib/devise_invitable/model.rb

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ def valid_password?(password)
7777

7878
protected
7979

80+
# Clear invitation token when reset password token is cleared too
81+
def clear_reset_password_token
82+
self.invitation_token = nil if invited?
83+
super
84+
end
85+
8086
# Checks if the invitation for the user is within the limit time.
8187
# We do this by calculating if the difference between today and the
8288
# invitation sent date does not exceed the invite for time configured.

test/models/invitable_test.rb

+9
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ def setup
118118
assert_present user.invitation_token
119119
end
120120

121+
test 'should clear invitation token while resetting the password' do
122+
user = User.invite!(:email => "[email protected]")
123+
user.send(:generate_reset_password_token!)
124+
assert_present user.reset_password_token
125+
assert_present user.invitation_token
126+
User.reset_password_by_token(:reset_password_token => user.reset_password_token, :password => '123456789', :password_confirmation => '123456789')
127+
assert_nil user.reload.invitation_token
128+
end
129+
121130
test 'should reset invitation token and send invitation by email' do
122131
user = new_user
123132
assert_difference('ActionMailer::Base.deliveries.size') do

test/models_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def assert_include_modules(klass, *modules)
1717
end
1818

1919
test 'should include Devise modules' do
20-
assert_include_modules User, :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
20+
assert_include_modules User, :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
2121
end
2222

2323
test 'should have a default value for invite_for' do

test/rails_app/app/active_record/user.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class User < ActiveRecord::Base
2-
devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
2+
devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
33

44
attr_accessible :email, :username, :password, :password_confirmation
55

test/rails_app/app/mongoid/user.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class User
55
field :created_at, :type => DateTime
66
field :username, :type => String
77

8-
devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
8+
devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
99

1010
validates :username, :length => { :maximum => 20 }
1111
end

test/rails_app/config/initializers/devise.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282

8383
# If true, uses the password salt as remember token. This should be turned
8484
# to false if you are not using database authenticatable.
85-
config.use_salt_as_remember_token = true
85+
config.use_salt_as_remember_token = false
8686

8787
# ==> Configuration for :validatable
8888
# Range for password length. Default is 6..20.

test/rails_app/db/migrate/20100401102949_create_tables.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def self.up
55
t.string :username
66
t.confirmable
77
t.invitable
8-
t.encryptable
8+
t.recoverable
99

1010
t.timestamps
1111
end

0 commit comments

Comments
 (0)