Class: Morpheus::Cli::ForgotPassword

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/commands/forgot_password.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#connect(opts) ⇒ Object



9
10
11
12
# File 'lib/morpheus/cli/commands/forgot_password.rb', line 9

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @forgot_interface = @api_client.forgot
end

#handle(args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/morpheus/cli/commands/forgot_password.rb', line 14

def handle(args)
#   handle_subcommand(args)
# end

# def email(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = "Usage: #{prog_name} #{command_name} [username]"
    opts.on( '-U', '--username USERNAME', "Username of the user to be emailed." ) do |val|
      options[:options]['username'] = val
    end
    opts.on("--email [USERNAME]", String, "Email only. Only send an email, skip the reset password.") do |val|
      options[:email_only] = true
      if !val.to_s.empty?
        options[:options]['username'] = val
      end
    end
    opts.on("--reset [TOKEN]", "Reset only. Only reset password, skip sending an email.") do |val|
      options[:reset_only] = true
      if !val.to_s.empty?
        options[:options]['token'] = val
      end
    end
    opts.on( '-T', '--token TOKEN', "Token, the secret token that was emailed to the user. Only reset password, skip sending an email." ) do |val|
      options[:reset_only] = true
      options[:options]['token'] = val
    end
    opts.on( '-P', '--password PASSWORD', "New Password, the new password for the user." ) do |val|
      options[:options]['password'] = val
    end

    build_standard_post_options(opts, options, [], [:remote_username,:remote_password,:remote_token])
    opts.footer = <<-EOT
Send a forgot password email and reset your password.
[username] is required. This is the username to be notified.
By default this command prompts to perform two actions. 
First it sends a forgot password email to the specified user.
Then it attempts to reset the password with the secret token and a new password.
Use the --email and --token options to only perform one of these actions, instead of prompting to do both.
That is, only send the email or only reset the password.

EOT
  end
  optparse.parse!(args)
  connect(options)
  verify_args!(args:args, optparse:optparse, max:1)

  if options[:email_only] && options[:options]['token']
    raise_command_error "Invalid usage. --email cannot be used with --token or --reset, use one or the other", args, optparse
  end

  if args[0]
    options[:options]['username'] = args[0]
  end
  
  params.merge!(parse_query_options(options))

  # Step 1. Send Forgot Password Email
  if options[:reset_only] != true
    print_h1 "Forgot Password", [], options unless options[:quiet] || options[:json] || options[:yaml]
    payload = {}
    payload['username'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'username', 'fieldLabel' => 'Username', 'type' => 'text', 'description' => "Enter the username of your Morpheus User.", 'required' => true, :fmt => :natural}], options[:options],@api_client)['username']
    @forgot_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @forgot_interface.dry.send_email(payload, params)
      return
    end
    json_response = @forgot_interface.send_email(payload, params)
    if options[:email_only]
      render_response(json_response, options) do
        print_green_success(json_response["msg"] || "Email has been sent") unless options[:quiet]
      end
      return 0, nil
    else
      print_green_success(json_response["msg"] || "Email has been sent") unless options[:quiet]
    end
  end

  # Step 2. Reset Password
  print_h1 "Reset Password", [], options unless options[:quiet] || options[:json] || options[:yaml]
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!(parse_passed_options(options))
  else
    payload.deep_merge!(parse_passed_options(options))
    # prompt for Token
    payload['token'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'token', 'fieldLabel' => 'Token', 'type' => 'text', 'description' => "Enter the token that you obtained from the forgot password email.", 'required' => true, :fmt => :natural}], options[:options],@api_client)['token']
    # New Password
    # todo: prompt_password_with_confirmation()
    password_value = options[:options]['password']
    confirm_password_value = password_value
    while password_value.to_s.empty?
      password_value = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'password', 'fieldLabel' => 'New Password', 'type' => 'password', 'description' => "Enter your new password.", 'required' => true, :fmt => :natural}], options[:options],@api_client)['password']
      confirm_password_value = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'password', 'fieldLabel' => 'Confirm New Password', 'type' => 'password', 'description' => "Enter your new password again to confirm it is what you intend.", 'required' => true, :fmt => :natural}], options[:options],@api_client)['password']
      if password_value != confirm_password_value
        print_red_alert("Passwords did not match. Please try again.")
        password_value = nil
      end
    end
    payload['password'] = password_value
  end
  
  @forgot_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @forgot_interface.dry.reset_password(payload, params)
    return
  end
  json_response = @forgot_interface.reset_password(payload, params)
  render_response(json_response, options) do
    print_green_success(json_response["msg"] || "Password has been updated") unless options[:quiet]
  end
  return 0, nil
end