Class: HammerCLIImport::ImportCommand::UserImportCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_import/user.rb

Instance Attribute Summary

Attributes included from PersistentMap::Extend

#map_description, #map_target_entity, #maps

Instance Method Summary collapse

Methods inherited from BaseCommand

#_compare_hash, #_create_entity, #api_call, api_call, api_init, #create_entity, csv_columns, #cvs_iterate, #data_dir, #delete, #delete_entity, #delete_entity_by_import_id, #execute, #find_uniq, #found_errors, #get_cache, #get_original_id, #get_translated_id, #import, #initialize, #last_in_cache?, #list_server_entities, #load_cache, #lookup_entity, #lookup_entity_in_array, #lookup_entity_in_cache, #map_entity, #mapped_api_call, #post_delete, #print_summary, #process_error, #recognizable_error, #report_summary, #split_multival, #to_singular, #unmap_entity, #update_entity, #wait_for_task, #was_translated

Methods included from PersistentMap::Extend

#persistent_map, #persistent_maps

Methods included from ImportTools::ImportLogging::Extend

#add_logging_options

Methods included from AsyncTasksReactor::Extend

#add_async_tasks_reactor_options

Methods included from AsyncTasksReactor::Include

#atr_exit, #atr_init, #postpone_till, #wait_for

Methods included from ImportTools::Exceptional::Include

#handle_missing_and_supress, #silently

Methods included from ImportTools::Task::Include

#annotate_tasks

Methods included from ImportTools::ImportLogging::Include

#debug, #error, #fatal, #info, #log, #logtrace, #progress, #setup_logging, #warn

Methods included from PersistentMap::Include

#load_persistent_maps, #map_target_entity, #maps, #prune_persistent_maps, #save_persistent_maps

Constructor Details

This class inherits a constructor from HammerCLIImport::BaseCommand

Instance Method Details

#admin?(data) ⇒ Boolean

Admin-flag should be set if any Sat5-role has ‘admin’ in its map

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
# File 'lib/hammer_cli_import/user.rb', line 65

def admin?(data)
  return false if @role_map.nil?

  roles = split_multival(data['role'], false)
  is_admin = false
  roles.each do |r|
    is_admin ||= (@role_map[r.gsub(' ', '-')].include? '_admin_')
  end
  return is_admin
end

#delete_single_row(data) ⇒ Object



149
150
151
# File 'lib/hammer_cli_import/user.rb', line 149

def delete_single_row(data)
  delete_entity(:users, data['user_id'].to_i)
end

#first_time_onlyObject

Override so we can read the role-map once, not *once per user*



51
52
53
54
55
56
57
58
# File 'lib/hammer_cli_import/user.rb', line 51

def first_time_only
  if File.exist? option_role_mapping
    @role_map = YAML.load_file(option_role_mapping)
  else
    warn "Role-mapping file #{option_role_mapping} not found, no roles will be assigned"
  end
  return 'loaded'
end

#genpw(username) ⇒ Object



60
61
62
# File 'lib/hammer_cli_import/user.rb', line 60

def genpw(username)
  username + '_' + (0...8).collect { ('a'..'z').to_a[rand(26)] }.join
end

#import_single_row(data) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/hammer_cli_import/user.rb', line 118

def import_single_row(data)
  @first_time ||= first_time_only
  user = mk_user_hash data
  new_user = true

  user_id = data['user_id'].to_i
   = user[:login]

  unless @pm[:users][user_id].nil?
    info "User #{} already imported."
    return
  end

  if option_merge_users?
    existing_user = lookup_entity_in_cache :users, 'login' => user[:login]

    unless existing_user.nil?
      info "User with login #{} already exists. Associating..."
      map_entity :users, user_id, existing_user['id']
      new_user = false
    end
  end

  return unless new_user

  create_entity :users, user, user_id

  @new_passwords ||= []
  @new_passwords << {:login => user[:login], :password => user[:password], :mail => user[:mail]}
end

#mk_user_hash(data) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hammer_cli_import/user.rb', line 96

def mk_user_hash(data)
  username = data['username']
  username = 'sat5_admin' if username == 'admin'
  {
    :login => username,
    :firstname => data['first_name'],
    :lastname => data['last_name'],
    :mail => data['email'],
    :auth_source_id => 1,
    :password => genpw(username),
    :organization_ids => [get_translated_id(:organizations, data['organization_id'].to_i)],
    :location_ids => [],
    :admin => admin?(data),
    :role_ids => role_ids_for(data)
  }
end

#post_import(_) ⇒ Object



113
114
115
116
# File 'lib/hammer_cli_import/user.rb', line 113

def post_import(_)
  return if @new_passwords.nil? || @new_passwords.empty?
  CSVHelper.csv_write_hashes option_new_passwords, [:mail, :login, :password], @new_passwords
end

#role_ids_for(data) ⇒ Object

Return list-of-role-ids that match any sat5-role associated with this user XXX: Role-api doesn’t return what it needs to Until BZ 1102816 is fixed, this doesn’t work It does serve to show the infrastructure/approach required



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hammer_cli_import/user.rb', line 80

def role_ids_for(data)
  role_list = []
  return role_list if @role_map.nil?

  users_roles = split_multival(data['role'], false)
  fm_roles = api_call(:roles, :index, 'per_page' => 999999)['results']
  debug fm_roles.inspect
  users_roles.each do |s5r|
    fm_roles.each do |fr|
      role_list << fr['id'] if @role_map[s5r.gsub(' ', '-')].include? fr['name']
    end
  end

  return role_list
end