aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-04-05 01:19:08 +0900
committerGitHub <noreply@github.com>2019-04-05 01:19:08 +0900
commitdc45130c44ebe0d8b621eba29332b35ea2840738 (patch)
treeeaf110d6389c5526b379a11e83247ff33c9cb507 /activemodel
parent9e6f9bc7732ec4eb7e67121ab209eae6293f9097 (diff)
parent4733e04dfaaa39b22292eef168bc5c1d1638c9b2 (diff)
downloadrails-dc45130c44ebe0d8b621eba29332b35ea2840738.tar.gz
rails-dc45130c44ebe0d8b621eba29332b35ea2840738.tar.bz2
rails-dc45130c44ebe0d8b621eba29332b35ea2840738.zip
Merge pull request #35700 from Futurelearn/seb-secure-password-fix
Reintroduce support for overriding `has_secure_password` attributes
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/secure_password.rb56
1 files changed, 30 insertions, 26 deletions
diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb
index 51d54f34f3..cc1368d3a0 100644
--- a/activemodel/lib/active_model/secure_password.rb
+++ b/activemodel/lib/active_model/secure_password.rb
@@ -69,38 +69,42 @@ module ActiveModel
raise
end
- attr_reader attribute
+ mod = Module.new do
+ attr_reader attribute
- define_method("#{attribute}=") do |unencrypted_password|
- if unencrypted_password.nil?
- self.send("#{attribute}_digest=", nil)
- elsif !unencrypted_password.empty?
- instance_variable_set("@#{attribute}", unencrypted_password)
- cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
- self.send("#{attribute}_digest=", BCrypt::Password.create(unencrypted_password, cost: cost))
+ define_method("#{attribute}=") do |unencrypted_password|
+ if unencrypted_password.nil?
+ self.send("#{attribute}_digest=", nil)
+ elsif !unencrypted_password.empty?
+ instance_variable_set("@#{attribute}", unencrypted_password)
+ cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
+ self.send("#{attribute}_digest=", BCrypt::Password.create(unencrypted_password, cost: cost))
+ end
end
- end
- define_method("#{attribute}_confirmation=") do |unencrypted_password|
- instance_variable_set("@#{attribute}_confirmation", unencrypted_password)
- end
+ define_method("#{attribute}_confirmation=") do |unencrypted_password|
+ instance_variable_set("@#{attribute}_confirmation", unencrypted_password)
+ end
+
+ # Returns +self+ if the password is correct, otherwise +false+.
+ #
+ # class User < ActiveRecord::Base
+ # has_secure_password validations: false
+ # end
+ #
+ # user = User.new(name: 'david', password: 'mUc3m00RsqyRe')
+ # user.save
+ # user.authenticate_password('notright') # => false
+ # user.authenticate_password('mUc3m00RsqyRe') # => user
+ define_method("authenticate_#{attribute}") do |unencrypted_password|
+ attribute_digest = send("#{attribute}_digest")
+ BCrypt::Password.new(attribute_digest).is_password?(unencrypted_password) && self
+ end
- # Returns +self+ if the password is correct, otherwise +false+.
- #
- # class User < ActiveRecord::Base
- # has_secure_password validations: false
- # end
- #
- # user = User.new(name: 'david', password: 'mUc3m00RsqyRe')
- # user.save
- # user.authenticate_password('notright') # => false
- # user.authenticate_password('mUc3m00RsqyRe') # => user
- define_method("authenticate_#{attribute}") do |unencrypted_password|
- attribute_digest = send("#{attribute}_digest")
- BCrypt::Password.new(attribute_digest).is_password?(unencrypted_password) && self
+ alias_method :authenticate, :authenticate_password if attribute == :password
end
- alias_method :authenticate, :authenticate_password if attribute == :password
+ include mod
if validations
include ActiveModel::Validations