Skip to content

Commit

Permalink
Merge pull request rails#43378 from Stellenticket/set_empty_secure_pa…
Browse files Browse the repository at this point in the history
…ssword

clear secure password cache if password is set to `nil`
  • Loading branch information
rafaelfranca committed Oct 14, 2021
2 parents 6acaebd + 9bd186a commit 3b5db8e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions activemodel/CHANGELOG.md
@@ -1,3 +1,21 @@
* Clear secure password cache if password is set to `nil`

Before:

user.password = 'something'
user.password = nil

user.password # => 'something'

Now:

user.password = 'something'
user.password = nil

user.password # => nil

*Markus Doits*

## Rails 7.0.0.alpha2 (September 15, 2021) ##

* No changes.
Expand Down
1 change: 1 addition & 0 deletions activemodel/lib/active_model/secure_password.rb
Expand Up @@ -94,6 +94,7 @@ def initialize(attribute)

define_method("#{attribute}=") do |unencrypted_password|
if unencrypted_password.nil?
instance_variable_set("@#{attribute}", nil)
self.public_send("#{attribute}_digest=", nil)
elsif !unencrypted_password.empty?
instance_variable_set("@#{attribute}", unencrypted_password)
Expand Down
6 changes: 6 additions & 0 deletions activemodel/test/cases/secure_password_test.rb
Expand Up @@ -91,6 +91,12 @@ class SecurePasswordTest < ActiveModel::TestCase
assert_equal ["doesn't match Password"], @user.errors[:password_confirmation]
end

test "resetting password to nil clears the password cache" do
@user.password = "password"
@user.password = nil
assert_nil @user.password
end

test "update an existing user with validation and no change in password" do
assert @existing_user.valid?(:update), "user should be valid"
end
Expand Down

0 comments on commit 3b5db8e

Please sign in to comment.