Module: HasEditablePassword

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::SecurePassword
Defined in:
lib/has_editable_password.rb

Instance Method Summary collapse

Instance Method Details

#current_password_match?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/has_editable_password.rb', line 35

def current_password_match?
  if @current_password
    if @old_password_digest
      BCrypt::Password.new(@old_password_digest) == @current_password
    else
      # almost same as #authenticate (returns true instead of the object)
      BCrypt::Password.new(self.password_digest) == @current_password
    end
  else
    false
  end
end

#generate_recovery_token(options = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/has_editable_password.rb', line 23

def generate_recovery_token(options = {})
  token = SecureRandom.urlsafe_base64(options.delete(:length) || 32)
  self.password_recovery_token = BCrypt::Password.create(token)
  self.password_recovery_token_creation = Time.now
  save unless options.delete(:save) == false
  token
end

#valid_recovery_token?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/has_editable_password.rb', line 31

def valid_recovery_token?
  recovery_token_match? and !recovery_token_expired?
end