Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processes modifications from LDIF files #311

Open
HarlemSquirrel opened this issue Jun 15, 2018 · 1 comment
Open

Processes modifications from LDIF files #311

HarlemSquirrel opened this issue Jun 15, 2018 · 1 comment

Comments

@HarlemSquirrel
Copy link
Member

I see in the documentation how to read from LDIF files with Net::LDAP::Dataset.read_ldif(File.open(file_path)) and how to save entries to LDIF files with File.write file_path, entry.to_ldif.

However, I do not see a clear way of processing adds, deletes, and modifications from LDIF files. Reading from an LDIF file shows :changetype=>["add"] set as if it were an attribute.

Right now I have thrown together something like this.

ldif_mods = Net::LDAP::Dataset.read_ldif(File.open(file_path))

Net::LDAP.open(CONFIG) do |ldap|
  ldif_mods.each do |dn, attrs|
    case attrs[:changetype].first
    when 'add'
      ldap.add dn: dn, attributes: attrs.reject{ |k,_v| k == :changetype }
    when 'delete'
      ldap.delete dn
    when 'modify'
      # More logic to process attribute modifications
    end
    result = ldap.get_operation_result
    puts result unless result.code.zero?
  end
end

I feel like there should be a better way, similar to how LDIFReader provides #processChange on LDIFChangeRecord.

# JRuby
java_import 'com.unboundid.ldif.LDIFReader'

ldif_reader = LDIFReader.new(file_path)

loop do
  change_record = ldif_reader.readChangeRecord
  change_record.nil? ? break : change_record.processChange(CONN)
end
ldif_reader.close
@HarlemSquirrel
Copy link
Member Author

This has been working pretty well for me so far.

def modify_entries_from_file(file_path)
  Net::LDAP.open(CONFIG) do |ldap|
    Net::LDAP::Dataset.read_ldif(File.open(file_path)).each do |dn, args|
      case args[:changetype].first
      when 'add' || nil
        ldap.add dn: dn, attributes: args.reject { |k, _v| k == :changetype }
      when 'delete'
        ldap.delete dn: dn
      when 'modify'

        operations = []
        # Collect the modify operations to perform
        %i[add delete replace].each do |operation_type|
          args[operation_type].each do |attribute|
            operations << [operation_type, attribute.to_sym, args[attribute.downcase.to_sym]]
          end
        end

        ldap.modify dn: dn, operations: operations
      end
      result = ldap.get_operation_result
      raise(self::Error, result) unless result.code.zero?
      
      puts result unless result.code.zero?
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant