Module: Revise::Models::DatabaseAuthenticatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/revise/models/database_authenticatable.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

MAILERS =
[]
HELPERS =
['Authentication']
CONTROLLERS =
['Sessions', 'Accounts']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.required_fields(klass) ⇒ Object



25
26
27
# File 'lib/revise/models/database_authenticatable.rb', line 25

def self.required_fields(klass)
  [:encrypted_password] + klass.authentication_keys
end

Instance Method Details

#after_database_authenticationObject



79
80
# File 'lib/revise/models/database_authenticatable.rb', line 79

def after_database_authentication
end

#authenticatable_saltObject



82
83
84
# File 'lib/revise/models/database_authenticatable.rb', line 82

def authenticatable_salt
  encrypted_password[0,29] if encrypted_password
end

#clean_up_passwordsObject



45
46
47
# File 'lib/revise/models/database_authenticatable.rb', line 45

def clean_up_passwords
  self.password = self.password_confirmation = nil
end

#password=(new_password) ⇒ Object



29
30
31
32
# File 'lib/revise/models/database_authenticatable.rb', line 29

def password=(new_password)
  @password = new_password
  self.encrypted_password = password_digest(@password) if @password.present?
end

#update_with_password(params, *options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/revise/models/database_authenticatable.rb', line 49

def update_with_password(params, *options)
  current_password = params.delete(:current_password)

  if params[:password].blank?
    params.delete(:password)
    params.delete(:password_confirmation) if params[:password_confirmation].blank?
  end

  result = if valid_password?(current_password)
    update_attributes(params, *options)
  else
    self.assign_attributes(params, *options)
    self.valid?
    self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
    false
  end

  clean_up_passwords
  result
end

#update_without_password(params, *options) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/revise/models/database_authenticatable.rb', line 70

def update_without_password(params, *options)
  params.delete(:password)
  params.delete(:password_confirmation)

  result = update_attributes(params, *options)
  clean_up_passwords
  result
end

#valid_for_authentication?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
# File 'lib/revise/models/database_authenticatable.rb', line 17

def valid_for_authentication?
  if super && valid_password?
    true
  else
    false
  end
end

#valid_password?(password = nil) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
# File 'lib/revise/models/database_authenticatable.rb', line 34

def valid_password?(password=nil)
  password = @password if password == nil

  return false if encrypted_password.blank?

  bcrypt   = ::BCrypt::Password.new(encrypted_password)
  password = ::BCrypt::Engine.hash_secret("#{password}#{self.class.pepper}", bcrypt.salt)

  String.secure_compare(password, encrypted_password)
end