class Ldap::Cli
Tool for reading/writing entries in an LDAP directory to/from CSV files
Constants
- VERSION
Attributes
errors[RW]
headers[RW]
ldap[RW]
table_dn[RW]
Public Class Methods
new(config, headers, table_dn)
click to toggle source
# File lib/ldap/cli.rb, line 15 def initialize(config, headers, table_dn) @ldap = Net::LDAP.new config @headers = headers @table_dn = table_dn end
Public Instance Methods
bind()
click to toggle source
# File lib/ldap/cli.rb, line 21 def bind ldap.bind end
delete(args)
click to toggle source
# File lib/ldap/cli.rb, line 25 def delete(args) ldap.delete(args) end
export(args = {})
click to toggle source
# File lib/ldap/cli.rb, line 42 def export(args = {}) @output_file_path = args[:output_file_path] @filter = args[:filter] add_header export_data rescue StandardError => e puts "ERROR: On LdapCli#export: #{e.message}" end
import(input_file_path = nil)
click to toggle source
# File lib/ldap/cli.rb, line 34 def import(input_file_path = nil) @errors = [] CSV.foreach(input_file_path, headers: true, skip_blanks: true) do |row| valid_headers row.headers save_record(row) end end
valid_headers(input_headers = [])
click to toggle source
# File lib/ldap/cli.rb, line 29 def valid_headers(input_headers = []) no_header = 'Require headers to process the file.' raise no_header unless (input_headers - headers).empty? end
Private Instance Methods
add_header()
click to toggle source
# File lib/ldap/cli.rb, line 77 def add_header CSV.open(@output_file_path, 'w+', force_quotes: false) do |csv| csv << headers end end
dn_value(row)
click to toggle source
# File lib/ldap/cli.rb, line 65 def dn_value(row) "uid=#{row['uid']}, #{table_dn}" end
export_data()
click to toggle source
# File lib/ldap/cli.rb, line 83 def export_data CSV.open(@output_file_path, 'a+', force_quotes: false) do |csv| ldap.search(base: table_dn, filter: @filter, scope: 1) do |entry| csv << headers.map { |x| entry.send(x).first } end end rescue StandardError => e puts "ERROR: on add entry to CSV from LDAP search: #{e.message}" end
save_record(row)
click to toggle source
# File lib/ldap/cli.rb, line 53 def save_record(row) dn = dn_value(row) attr = row.to_h attr['objectclass'] = ['inetOrgPerson'] if search(row['uid']).empty? ldap.add(dn: dn, attributes: attr) else ldap.modify dn: dn, attributes: attr end save_record_log(ldap, row) end
save_record_log(ldap, row)
click to toggle source
# File lib/ldap/cli.rb, line 93 def save_record_log(ldap, row) status = ldap.get_operation_result['message'] error_message = ldap.get_operation_result.error_message if status == 'Success' puts "Entry created successfully for uid #{row['uid']}" elsif error_message.include?('did not contain any modifications') puts error_message else @errors << error_message puts error_message end end
search(uid)
click to toggle source
# File lib/ldap/cli.rb, line 69 def search(uid) ldap.search( base: table_dn, filter: "uid=#{uid}", scope: 1 ) end