module ZmLdifExport

Constants

LDIF_FILE
OCLASS_ACCOUNT
OCLASS_DL
OCLASS_DOMAIN
VERSION
ZIMBRA_ATTRS_FILE

CONSTANTS

Public Class Methods

attrs_to_cmd(attributes = {}) click to toggle source

Return an array of attributes

# File lib/zmldifexport.rb, line 106
def attrs_to_cmd(attributes = {})
  lines = []
  attributes.each do |k,v|
    values = [v].flatten.compact
    values.each do |value|
      lines.push "#{k} #{value} \\"
    end
  end
  return nil if lines.empty?
  lines.last.gsub!(' \\', '')
  return lines
end
domain_to_ldif_dc() click to toggle source
# File lib/zmldifexport.rb, line 86
def domain_to_ldif_dc
  "dc=#{@domain.split('.').join(',dc=')}"
end
exportAccounts() click to toggle source
# File lib/zmldifexport.rb, line 49
def exportAccounts
  puts "export LC_ALL=en_US.UTF-8"
  puts "export LANG=en_US.UTF-8"
  Account.all.each do |a|
    next if a.name.first =~ /(admin|spam|ham|galsync|virus|wiki)/
    puts "# zmprov ca #{a.name(:zmprov, @domain)}"
    puts a.zmprov_ca(domain: @domain)
    puts ""
  end
end
exportAliases() click to toggle source
# File lib/zmldifexport.rb, line 60
def exportAliases
  Account.all.each do |a|
    next unless a.has_alias?
    puts "# zmprov aaa #{a.name(:zmprov, @domain)}"
    puts a.zmprov_aaa(domain: @domain)
    puts ""
  end
end
exportDls() click to toggle source
# File lib/zmldifexport.rb, line 69
def exportDls
  puts "export LC_ALL=en_US.UTF-8"
  puts "export LANG=en_US.UTF-8"
  DistributionList.all.each do |dl|
    next if dl.name(:zmprov) =~ /archive/i
    puts "# Create DistributionList #{dl.name(:zmprov, @domain)}"
    puts dl.zmprov_cdl domain: "@#{@domain}"
    puts ""
    puts "# Add members to DistributionList #{dl.name(:zmprov, @domain)}"
    puts dl.zmprov_adlm domain: "@#{@domain}"
    puts ""
    puts "# Add Alias to DistributionList #{dl.name(:zmprov, @domain)}"
    puts dl.zmprov_adla domain: "@#{@domain}"
    puts ""
  end
end
ldif_data() click to toggle source

Load the LDIF in Memory

# File lib/zmldifexport.rb, line 20
def ldif_data
  ldif_data = Net::LDAP::Dataset.read_ldif ldif_file
  puts "File #{@file_path} not a valid LDIF file" if ldif_data.empty?
  ldif_data
end
ldif_file() click to toggle source
# File lib/zmldifexport.rb, line 11
def ldif_file
  begin
    open @file_path
  rescue Errno::ENOENT => _
    puts "File #{@file_path} not found"
  end
end
mk_command(zm_cmd: 'zmprov', command:, resource:, attributes: {}, full: true) click to toggle source

Build the resulting command @param zm_cmd [String] `zmprov` or `zmmailbox` @param command [String] command to excute, like `ca` or `createDomain` @param attributes [Array] Array of attributes to pass to the command @param full [Boolean] If print `zm_cmd` or just the `command` @returns [String]

# File lib/zmldifexport.rb, line 96
def mk_command(zm_cmd: 'zmprov', command:, resource:, attributes: {}, full: true)
  comment = "# #{zm_cmd} #{command} #{resource}"
  command = full ? "#{zm_cmd} #{command} #{resource}" : "#{command} #{resource}"
  command = command + " \\" if attributes.keys.any?
  attributes = attrs_to_cmd(attributes)
  
  [comment, command, attributes].compact.join("\n")
end
parse_ldif() click to toggle source
# File lib/zmldifexport.rb, line 26
def parse_ldif
  ldif_data.to_entries.each do |entry|
    Domain.add Domain.new(entry) if entry.objectclass.include?(OCLASS_DOMAIN)
    DistributionList.add DistributionList.new(entry) if entry.objectclass.include?(OCLASS_DL)
    Account.add Account.new(entry) if valid_account_entry?(entry)
  end
end
run(options) click to toggle source

Entry point do all

# File lib/zmldifexport.rb, line 39
def run(options)
 @options = options
 @file_path = @options[:ldif_file]
 @domain = @options[:domain]
 @command = @options[:command]

 parse_ldif
 self.send(@command)
end
valid_account_entry?(entry) click to toggle source
# File lib/zmldifexport.rb, line 34
def valid_account_entry?(entry)
  entry.objectclass.include?(OCLASS_ACCOUNT) && entry.dn =~ /(#{domain_to_ldif_dc}$|uid=admin|uid=spam|uid=virus|uid=nospam)/
end