Class: QuoVadis::Password

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/quo_vadis/password.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#new_passwordObject

Returns the value of attribute new_password.



12
13
14
# File 'app/models/quo_vadis/password.rb', line 12

def new_password
  @new_password
end

Instance Method Details

#change(current_plaintext, new_plaintext, new_plaintext_confirmation) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/quo_vadis/password.rb', line 15

def change(current_plaintext, new_plaintext, new_plaintext_confirmation)
  unless authenticate current_plaintext
    errors.add :password, :incorrect
    return false
  end

  # has_secure_password ignores empty passwords ("") on update so reject them here.
  if new_plaintext.empty?
    errors.add :new_password, :blank
    return false
  end

  self.password = new_plaintext
  self.password_confirmation = new_plaintext_confirmation

  if save
    true
  else
    errors.delete(:password)&.each { |e| errors.add :new_password, e }
    errors.delete(:password_confirmation)&.each { |e| errors.add :new_password_confirmation, e }
    false
  end
end

#reset(new_plaintext, new_plaintext_confirmation) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/quo_vadis/password.rb', line 40

def reset(new_plaintext, new_plaintext_confirmation)
  # has_secure_password ignores empty passwords ("") on update so reject them here.
  if new_plaintext.empty?
    errors.add :password, :blank
    return false
  end

  self.password = new_plaintext
  self.password_confirmation = new_plaintext_confirmation
  if save
    # Logout account's sessions because password has changed.
    # Assumes model is not logged in.
    .sessions.destroy_all
    true
  end
end