Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#4245] Allowing password to nil #4261

Merged
merged 4 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/devise/models/database_authenticatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ def self.required_fields(klass)
# the hashed password.
def password=(new_password)
@password = new_password
self.encrypted_password = password_digest(@password) if @password.present?
self.encrypted_password = password_digest(@password)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucasmazza Most of the test cases has been failed because of I have removed @password.present? condition. I couldn't find the reason that why it is failing. Could you help me?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the looks of it we need to update clean_up_passwords to clear the instance variables directly, instead of calling password=.

       def clean_up_passwords
-        self.password = self.password_confirmation = nil
+        @password = @password_confirmation = nil
       end

end

# Verifies whether a password (ie from sign in) is the user password.
def valid_password?(password)
return false if password.blank?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can remove this condition since it's already present inside Devise::Encryptor.compare

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, Devise::Encryptor.compare is checking that encrypted_password is present (or at least it is as of now).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but since we're returning nil from #password_digest when the password is blank, encrypted_password will always be nil, and the .compare method will return in the return false if hashed_password.blank? condition.

Devise::Encryptor.compare(self.class, encrypted_password, password)
end

# Set password and password confirmation to nil
def clean_up_passwords
self.password = self.password_confirmation = nil
@password = @password_confirmation = nil
end

# Update record attributes when :current_password matches, otherwise
Expand Down Expand Up @@ -144,6 +145,7 @@ def send_password_change_notification
# See https://github.com/plataformatec/devise-encryptable for examples
# of other hashing engines.
def password_digest(password)
return if password.blank?
Devise::Encryptor.digest(self.class, password)
end

Expand Down
13 changes: 10 additions & 3 deletions test/models/database_authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ def setup
assert_nil user.authenticatable_salt
end

test 'should not generate a hashed password if password is blank' do
assert_blank new_user(password: nil).encrypted_password
assert_blank new_user(password: '').encrypted_password
test 'should set encrypted password to nil if password is nil' do
assert_nil new_user(password: nil).encrypted_password
assert_nil new_user(password: '').encrypted_password
end

test 'should hash password again if password has changed' do
Expand Down Expand Up @@ -266,4 +266,11 @@ def setup
]
end
end

test 'nil password should be invalid if password is set to nil' do
user = User.create(email: "HEllO@example.com", password: "12345678")
user.password = nil
refute user.valid_password?('12345678')
refute user.valid_password?(nil)
end
end