Class: Morpheus::Cli::ChangePasswordCommand

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from AccountsHelper

#account_column_definitions, #account_users_interface, #accounts_interface, #find_account_by_id, #find_account_by_name, #find_account_by_name_or_id, #find_account_from_options, #find_all_user_ids, #find_role_by_id, #find_role_by_name, #find_role_by_name_or_id, #find_user_by_id, #find_user_by_username, #find_user_by_username_or_id, #find_user_group_by_id, #find_user_group_by_name, #find_user_group_by_name_or_id, #format_access_string, #format_role_type, #format_user_role_names, #format_user_status, #get_access_color, #get_access_string, included, #list_account_column_definitions, #list_user_column_definitions, #list_user_group_column_definitions, #role_column_definitions, #roles_interface, #subtenant_role_column_definitions, #user_column_definitions, #user_group_column_definitions, #user_groups_interface

Methods included from CliCommand

#apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_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, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #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_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #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

#change_password(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/change_password_command.rb', line 21

def change_password(args)
  options = {}
  username = nil
  new_password = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[username] [options]")
    opts.on('--username USERNAME', String, "Username. Default is your own.") do |val|
      username = val
    end
    opts.on('--password VALUE', String, "New password") do |val|
      new_password = val
    end
    build_common_options(opts, options, [:account, :options, :json, :dry_run, :remote, :quiet, :auto_confirm], [:username,:password])
    opts.footer = "Change your password or the password of another user.\n" +
                  "[username] is optional. This is the username of the user to update. Default is your own.\n" +
                  "Be careful with this command, the default behavior is to update your own password."
  end
  optparse.parse!(args)

  if args.count > 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 0-1 and got #{args.count} #{args}\n#{optparse}"
    return 1
  end

  connect(options)
  @current_remote = @appliance_name ? ::Morpheus::Cli::Remote.load_remote(@appliance_name) : ::Morpheus::Cli::Remote.load_active_remote()

  if args[0]
    username = args[0]
  end
  if username.nil?
    if !@current_remote
      raise_command_error "No current appliance, see `remote use`."
    end
    if !@current_remote[:username]
      raise_command_error "You are not currently logged in to #{display_appliance(@current_remote[:name], @current_remote[:url])}"
    end
    username = @current_remote[:username]
  end

   = (options)
   =  ? ['id'] : nil

  user = find_user_by_username_or_id(, username)
  return 1 if user.nil?

  if @current_remote && @current_remote[:username] == username
    if !options[:quiet]
      if options[:dry_run]
        print cyan,bold,  "DRY RUN. This is just a dry run, the password is not being updated.",reset,"\n"
      else
        print cyan,bold,  "WARNING! You are about to update your own password!",reset,"\n"
        print yellow,bold,"WARNING! You are about to update your own password!",reset,"\n"
        print reset,bold, "WARNING! You are about to update your own password!",reset,"\n"
      end
    end
  end

  if !options[:quiet]
    print cyan, "Changing password for #{user['username']}", reset, "\n"
  end

  if new_password.nil? && options[:options]['password']
    new_password = options[:options]['password']
  end
  if new_password.nil?
    password_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'password', 'fieldLabel' => 'New Password', 'type' => 'password', 'required' => true}], options[:options], @api_client)
    new_password = password_prompt['password']
    confirm_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'passwordConfirmation', 'fieldLabel' => 'Confirm Password', 'type' => 'password', 'required' => true}], options[:options], @api_client)
    confirm_password = confirm_prompt['passwordConfirmation']
    if confirm_password != new_password
      print_red_alert "Confirm password did not match."
      return 1
    end
  end

  if new_password.nil? || new_password.empty?
    print_red_alert "A new password is required"
    return 1
  end

  payload = {
    'user' => {
      'password' => new_password
    } 
  }
  @account_users_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @account_users_interface.dry.update(, user['id'], payload)
    return 0
  end

  unless options[:yes]
    unless ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to update the password for user #{user['username']}?", options)
      return 9, "aborted command"
    end
  end

  json_response = @account_users_interface.update(, user['id'], payload)
  if options[:json]
    puts as_json(json_response)
  elsif !options[:quiet]
    print_green_success "Updated password for user #{user['username']}"
  end
  return 0
end

#connect(opts) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/morpheus/cli/commands/change_password_command.rb', line 9

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @whoami_interface = @api_client.whoami
  @account_users_interface = @api_client.
  @accounts_interface = @api_client.accounts
  @roles_interface = @api_client.roles
end

#handle(args) ⇒ Object



17
18
19
# File 'lib/morpheus/cli/commands/change_password_command.rb', line 17

def handle(args)
  change_password(args)
end