Class: Csv::Ldap
- Inherits:
-
Object
- Object
- Csv::Ldap
- Defined in:
- lib/csv/ldap.rb,
lib/csv/ldap/version.rb
Constant Summary collapse
- DEFAULT_HEADERS =
%w{cn sn mail uid homeDirectory uidNumber gidNumber}.freeze
- DEFAULT_TREE_BASE =
'ou=people, dc=example, dc=org'.freeze
- DEFAULT_OUTPUT_FILE_PATH =
"#{Dir.home}/ldap_data_output.csv"- VERSION =
'0.1.0'
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#ldap ⇒ Object
Returns the value of attribute ldap.
Instance Method Summary collapse
- #add_header ⇒ Object
- #bind ⇒ Object
- #delete(args) ⇒ Object
- #export(args = {}) ⇒ Object
- #import(input_file_path = nil, headers = []) ⇒ Object
-
#initialize(args) ⇒ Ldap
constructor
A new instance of Ldap.
- #valid_headers(headers = []) ⇒ Object
Constructor Details
#initialize(args) ⇒ Ldap
Returns a new instance of Ldap.
13 14 15 |
# File 'lib/csv/ldap.rb', line 13 def initialize(args) @ldap = Net::LDAP.new args end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
8 9 10 |
# File 'lib/csv/ldap.rb', line 8 def errors @errors end |
#ldap ⇒ Object
Returns the value of attribute ldap.
8 9 10 |
# File 'lib/csv/ldap.rb', line 8 def ldap @ldap end |
Instance Method Details
#add_header ⇒ Object
69 70 71 72 73 |
# File 'lib/csv/ldap.rb', line 69 def add_header CSV.open(@output_file_path, 'w+', { force_quotes: false }) do |csv| csv << @headers end end |
#bind ⇒ Object
17 18 19 |
# File 'lib/csv/ldap.rb', line 17 def bind @ldap.bind end |
#delete(args) ⇒ Object
21 22 23 |
# File 'lib/csv/ldap.rb', line 21 def delete(args) @ldap.delete(args) end |
#export(args = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/csv/ldap.rb', line 54 def export(args = {}) @headers = args[:headers] || DEFAULT_HEADERS @treebase = args[:treebase] || DEFAULT_TREE_BASE @output_file_path = args[:output_file_path] || DEFAULT_OUTPUT_FILE_PATH @filter ||= args[:filter] add_header CSV.open(@output_file_path, 'a+', { force_quotes: false }) do |csv| ldap.search(base: @treebase, filter: @filter) do |entry| csv << @headers.map { |x| entry.send(x).first } rescue nil end end end |
#import(input_file_path = nil, headers = []) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/csv/ldap.rb', line 31 def import(input_file_path = nil, headers = []) @headers ||= headers.empty? ? DEFAULT_HEADERS : headers @errors = [] CSV.foreach(input_file_path, headers: true, skip_blanks: true) do |row| valid_headers row.headers dn = "cn=#{row['cn']}, ou=people, dc=example, dc=org" attr = row.to_h attr['objectclass'] = ['inetOrgPerson', 'posixAccount', 'shadowAccount'] @ldap.add(dn: dn, attributes: attr) if @ldap.get_operation_result['message'] == 'Success' puts 'Adding cn:' + row['cn'] + ' ' + @ldap.get_operation_result['message'] else puts 'Error cn:' + row['cn'] + ' ' + @ldap.get_operation_result['message'] @errors << row['cn'] + ' ' + @ldap.get_operation_result['message'] end end end |
#valid_headers(headers = []) ⇒ Object
25 26 27 28 29 |
# File 'lib/csv/ldap.rb', line 25 def valid_headers(headers = []) unless headers.all? { |header| @headers.include?(header) } raise 'Require headers to process the file.' end end |