Module: Devise::Models::PasswordHistory

Extended by:
ActiveSupport::Concern
Defined in:
lib/devise_password_history/models/password_history.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#old_password_being_used?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/devise_password_history/models/password_history.rb', line 25

def old_password_being_used?
  if active_password_history_support?
    if self.password.present?
      # we need to go through each of the old passwords
      # and check to see if the new password would authenticate
      # the user (via valid_password?); if so that indicates
      # the password has been used in the past
      self.old_passwords.each do |old_pw|
        temp = self.class.new
        temp.encrypted_password = old_pw.encrypted_password
        temp.password_salt = old_pw.password_salt

        # return true if this password "passes" (authenticates)
        return true if temp.valid_password?(self.password)
      end
    end
  end

  # otherwise, we're safe to let this
  # password go through
  false
end

#validate_old_passwordsObject

validation applied here



19
20
21
22
23
# File 'lib/devise_password_history/models/password_history.rb', line 19

def validate_old_passwords
  if self.encrypted_password_changed? && self.old_password_being_used?
    self.errors.add(:password, "has been used already (you can't use your last #{self.class.password_history_count} passwords)")
  end
end