Class: Socialcast::CommandLine::ProvisionUser

Inherits:
Object
  • Object
show all
Includes:
Provisioner
Defined in:
lib/socialcast/command_line/provision_user.rb

Constant Summary

Constants included from Provisioner

Socialcast::CommandLine::Provisioner::DEFAULT_OUTPUT_FILE

Instance Method Summary collapse

Methods included from Provisioner

#initialize

Instance Method Details

#each_user_hashObject



13
14
15
16
17
18
19
# File 'lib/socialcast/command_line/provision_user.rb', line 13

def each_user_hash
  each_ldap_connector do |connector|
    connector.each_user_hash do |user_hash|
      yield user_hash
    end
  end
end

#fetch_user_hash(identifier, options = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/socialcast/command_line/provision_user.rb', line 21

def fetch_user_hash(identifier, options = {})
  each_ldap_connector do |connector|
    user_hash = connector.fetch_user_hash(identifier, options)
    return user_hash if user_hash
  end
  nil
end

#provisionObject



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
# File 'lib/socialcast/command_line/provision_user.rb', line 29

def provision
  user_whitelist = Set.new
  output_file = File.join Dir.pwd, @options[:output]
  params = http_config.merge(:external_system => !!@options[:external_system])

  Zlib::GzipWriter.open(output_file) do |gz|
    xml = Builder::XmlMarkup.new(:target => gz, :indent => 1)
    xml.instruct!
    xml.export do |export|
      export.users(:type => "array") do |users|
        each_user_hash do |user_hash|
          users << user_hash.to_xml(:skip_instruct => true, :root => 'user')
          user_whitelist << [user_hash['contact_info'][LDAPConnector::EMAIL_ATTRIBUTE], user_hash[LDAPConnector::UNIQUE_IDENTIFIER_ATTRIBUTE], user_hash['employee_number']]
        end
      end # users
    end # export
  end # gzip

  if user_whitelist.empty? && !@options[:force]
    raise ProvisionError.new "Skipping upload to Socialcast since no users were found"
  else
    log "Uploading dataset to Socialcast..."
    resource = Socialcast::CommandLine.resource_for_path '/api/users/provision', params
    begin
      File.open(output_file, 'r') do |file|
        request_params = {:file => file}
        request_params[:skip_emails] = 'true' if (@ldap_config.fetch('options', {})["skip_emails"] || @options[:skip_emails])
        request_params[:test] = 'true' if (@ldap_config.fetch('options', {})["test"] || @options[:test])
        request_params[:add_only] = 'true' if (@ldap_config.fetch('options', {})['add_only'] || @options[:add_only])
        resource.post request_params, :accept => :json
      end
    rescue RestClient::Unauthorized, RestClient::Forbidden => e
      raise ProvisionError.new provision_error_message(e)
    end
    log "Finished"
  end
  File.delete(output_file) if (@ldap_config.fetch('options', {})['delete_users_file'] || @options[:delete_users_file])
end