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)
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. = "Send a forgot password email and reset your password.\n[username] is required. This is the username to be notified.\nBy default this command prompts to perform two actions. \nFirst it sends a forgot password email to the specified user.\nThen it attempts to reset the password with the secret token and a new password.\nUse the --email and --token options to only perform one of these actions, instead of prompting to do both.\nThat is, only send the email or only reset the password.\n\n"
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))
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
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))
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']
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
|