Class: Foobara::Auth::ResetPassword

Inherits:
Command
  • Object
show all
Defined in:
foobara-auth-0.0.9/src/reset_password.rb

Defined Under Namespace

Classes: BothPasswordResetTokenAndOldPasswordGivenError, IncorrectOldPasswordError, InvalidResetPasswordTokenError, NoPasswordResetTokenOrOldPasswordGivenError, UserNotFoundForResetTokenError, UserNotGivenError

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome

Attributes included from CommandPatternImplementation::Concerns::Errors

#error_collection

Attributes included from CommandPatternImplementation::Concerns::Inputs

#inputs, #raw_inputs

Instance Method Summary collapse

Methods inherited from Command

install!, reset_all

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from CommandPatternImplementation::Concerns::Reflection

#initialize

Methods included from CommandPatternImplementation::Concerns::DomainMappers

#domain_map, #domain_map!, #run_mapped_subcommand!

Methods included from CommandPatternImplementation::Concerns::Subcommands

#run_subcommand!, #subcommand?

Methods included from CommandPatternImplementation::Concerns::Entities

#load_entities, #load_records

Methods included from CommandPatternImplementation::Concerns::Transactions

#auto_detect_current_transactions, #commit_transaction, #open_transaction, #opened_transactions, #relevant_entity_classes, #rollback_transaction, #transactions

Methods included from CommandPatternImplementation::Concerns::StateMachine

#state_machine

Methods included from CommandPatternImplementation::Concerns::Runtime

#halt!, #run, #run!, #run_execute, #succeed, #success?, #validate_records

Methods included from CommandPatternImplementation::Concerns::Errors

#initialize

Methods included from CommandPatternImplementation::Concerns::Inputs

#cast_and_validate_inputs, #initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?

Methods included from TruncatedInspect

#inspect, truncating

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs

Instance Attribute Details

#password_secretObject

Returns the value of attribute password_secret.



74
75
76
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 74

def password_secret
  @password_secret
end

#reset_password_token_recordObject

Returns the value of attribute reset_password_token_record.



74
75
76
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 74

def reset_password_token_record
  @reset_password_token_record
end

#userObject



110
111
112
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 110

def user
  @user || inputs[:user]
end

Instance Method Details

#build_new_passwordObject



114
115
116
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 114

def build_new_password
  self.password_secret = run_subcommand!(BuildSecret, secret: new_password)
end

#executeObject

TODO: should we enforce certain password requirements?



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 45

def execute
  if old_password_given?
    verify_old_password
  else
    verify_and_use_up_password_reset_token
    load_user
  end

  build_new_password
  set_password_on_user

  user
end

#load_userObject



102
103
104
105
106
107
108
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 102

def load_user
  self.user = Types::User.that_owns(reset_password_token_record, "reset")

  unless user
    add_runtime_error(UserNotFoundForResetTokenError)
  end
end

#old_password_given?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 77

def old_password_given?
  !!old_password
end

#reset_password_token_given?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 81

def reset_password_token_given?
  !!reset_password_token_secret
end

#set_password_on_userObject



118
119
120
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 118

def set_password_on_user
  user.password_secret = password_secret
end

#validateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 59

def validate
  if old_password_given?
    unless user
      add_runtime_error(UserNotGivenError)
    end
    if reset_password_token_given?
      add_runtime_error(BothPasswordResetTokenAndOldPasswordGivenError)
    end
  else
    unless reset_password_token_given?
      add_runtime_error(NoPasswordResetTokenOrOldPasswordGivenError)
    end
  end
end

#verify_and_use_up_password_reset_tokenObject



91
92
93
94
95
96
97
98
99
100
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 91

def verify_and_use_up_password_reset_token
  verified_result = run_subcommand!(VerifyToken, token_string: reset_password_token_secret)

  unless verified_result[:verified]
    add_runtime_error(InvalidResetPasswordTokenError)
  end

  self.reset_password_token_record = verified_result[:token_record]
  reset_password_token_record.use_up!
end

#verify_old_passwordObject



85
86
87
88
89
# File 'foobara-auth-0.0.9/src/reset_password.rb', line 85

def verify_old_password
  unless run_subcommand!(VerifyPassword, user:, plaintext_password: old_password)
    add_runtime_error(IncorrectOldPasswordError)
  end
end