Module: ZmLdifExport

Defined in:
lib/zmldifexport.rb,
lib/zmldifexport/version.rb

Defined Under Namespace

Classes: Account, Attribute, DistributionList, Domain, ZimbraResource

Constant Summary collapse

ZIMBRA_ATTRS_FILE =

CONSTANTS

'./data/zimbra-attrs.xml'.freeze
LDIF_FILE =
OCLASS_DOMAIN =
'zimbraDomain'
OCLASS_ACCOUNT =
'zimbraAccount'
OCLASS_DL =
'zimbraDistributionList'
VERSION =
"0.2.2"

Class Method Summary collapse

Class Method Details

.attrs_to_cmd(attributes = {}) ⇒ Object

Return an array of attributes



106
107
108
109
110
111
112
113
114
115
116
117
# 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_dcObject



86
87
88
# File 'lib/zmldifexport.rb', line 86

def domain_to_ldif_dc
  "dc=#{@domain.split('.').join(',dc=')}"
end

.exportAccountsObject



49
50
51
52
53
54
55
56
57
58
# 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

.exportAliasesObject



60
61
62
63
64
65
66
67
# 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

.exportDlsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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_dataObject

Load the LDIF in Memory



20
21
22
23
24
# 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_fileObject



11
12
13
14
15
16
17
# 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) ⇒ Object

Build the resulting command

Parameters:

  • zm_cmd (String) (defaults to: 'zmprov')

    ‘zmprov` or `zmmailbox`

  • command (String)

    command to excute, like ‘ca` or `createDomain`

  • attributes (Array) (defaults to: {})

    Array of attributes to pass to the command

  • full (Boolean) (defaults to: true)

    If print ‘zm_cmd` or just the `command`



96
97
98
99
100
101
102
103
# 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_ldifObject



26
27
28
29
30
31
32
# 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 (entry)
  end
end

.run(options) ⇒ Object

Entry point do all



39
40
41
42
43
44
45
46
47
# 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) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/zmldifexport.rb', line 34

def (entry)
  entry.objectclass.include?(OCLASS_ACCOUNT) && entry.dn =~ /(#{domain_to_ldif_dc}$|uid=admin|uid=spam|uid=virus|uid=nospam)/
end