zm-ruby-client 3.1.2 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +126 -0
  3. data/README.md +42 -0
  4. data/VERSION +1 -1
  5. data/lib/zm/client/account/account.rb +214 -8
  6. data/lib/zm/client/account/account_jsns_builder.rb +6 -18
  7. data/lib/zm/client/account/account_jsns_initializer.rb +1 -1
  8. data/lib/zm/client/account/accounts_collection.rb +1 -0
  9. data/lib/zm/client/account/accounts_response.rb +21 -0
  10. data/lib/zm/client/backup/backups_collection.rb +1 -1
  11. data/lib/zm/client/backup/backups_response.rb +16 -0
  12. data/lib/zm/client/base/admin_objects_collection.rb +18 -8
  13. data/lib/zm/client/base/admin_objects_response.rb +23 -0
  14. data/lib/zm/client/base/base_jsns_initializer.rb +34 -3
  15. data/lib/zm/client/base/has_cos.rb +75 -0
  16. data/lib/zm/client/base/has_zimbra_attributes.rb +133 -0
  17. data/lib/zm/client/base/object.rb +2 -0
  18. data/lib/zm/client/base/objects_builder.rb +1 -1
  19. data/lib/zm/client/base/zimbra_attributes_collection.rb +43 -166
  20. data/lib/zm/client/base/zimbra_attrs_jsns.rb +44 -0
  21. data/lib/zm/client/cluster/cluster.rb +16 -9
  22. data/lib/zm/client/cluster/cluster_config.rb +50 -18
  23. data/lib/zm/client/contact/contact_jsns_initializer.rb +16 -16
  24. data/lib/zm/client/contact/contact_members_collection.rb +19 -1
  25. data/lib/zm/client/cos/cos.rb +183 -11
  26. data/lib/zm/client/cos/cos_jsns_builder.rb +4 -16
  27. data/lib/zm/client/cos/cos_jsns_initializer.rb +2 -2
  28. data/lib/zm/client/cos/coses_collection.rb +1 -0
  29. data/lib/zm/client/cos/coses_response.rb +27 -0
  30. data/lib/zm/client/deprecation.rb +24 -0
  31. data/lib/zm/client/distributionlist/distributionlist.rb +11 -4
  32. data/lib/zm/client/distributionlist/distributionlist_jsns_builder.rb +4 -16
  33. data/lib/zm/client/distributionlist/distributionlist_jsns_initializer.rb +1 -2
  34. data/lib/zm/client/distributionlist/distributionlists_collection.rb +1 -0
  35. data/lib/zm/client/distributionlist/distributionlists_response.rb +16 -0
  36. data/lib/zm/client/distributionlist/dls_membership_collection.rb +5 -5
  37. data/lib/zm/client/domain/domain.rb +51 -10
  38. data/lib/zm/client/domain/domain_jsns_builder.rb +4 -16
  39. data/lib/zm/client/domain/domain_jsns_initializer.rb +9 -2
  40. data/lib/zm/client/domain/domains_collection.rb +1 -0
  41. data/lib/zm/client/domain/domains_response.rb +23 -0
  42. data/lib/zm/client/folder/folder.rb +1 -0
  43. data/lib/zm/client/folder/folder_default.rb +3 -1
  44. data/lib/zm/client/folder/folder_grants_collection.rb +24 -0
  45. data/lib/zm/client/folder/folder_jsns_initializer.rb +4 -3
  46. data/lib/zm/client/folder/folder_retention_policies_collection.rb +8 -0
  47. data/lib/zm/client/folder/folder_retention_policy.rb +2 -2
  48. data/lib/zm/client/folder/folder_view_filters.rb +32 -0
  49. data/lib/zm/client/folder/folders_builder.rb +31 -16
  50. data/lib/zm/client/folder/folders_collection.rb +21 -13
  51. data/lib/zm/client/folder/folders_response.rb +14 -0
  52. data/lib/zm/client/mailbox/mailbox_item_concern.rb +1 -3
  53. data/lib/zm/client/mailbox/mailbox_item_id.rb +7 -1
  54. data/lib/zm/client/mountpoint/mount_points_builder.rb +3 -3
  55. data/lib/zm/client/mountpoint/mount_points_collection.rb +0 -4
  56. data/lib/zm/client/mountpoint/mount_points_response.rb +10 -0
  57. data/lib/zm/client/resource/resource.rb +215 -4
  58. data/lib/zm/client/resource/resource_jsns_builder.rb +4 -16
  59. data/lib/zm/client/resource/resource_jsns_initializer.rb +1 -1
  60. data/lib/zm/client/resource/resources_collection.rb +1 -0
  61. data/lib/zm/client/resource/resources_response.rb +16 -0
  62. data/lib/zm/client/server/server.rb +28 -2
  63. data/lib/zm/client/server/servers_collection.rb +1 -0
  64. data/lib/zm/client/server/servers_response.rb +43 -0
  65. data/lib/zm/client/upload.rb +2 -0
  66. data/lib/zm/client/zm_logger.rb +32 -2
  67. data/lib/zm/client.rb +15 -0
  68. data/lib/zm/connector/network_error_handler.rb +2 -0
  69. data/lib/zm/connector/rest_connector.rb +2 -0
  70. data/lib/zm/connector/soap_account_connector.rb +2 -0
  71. data/lib/zm/connector/soap_base_connector.rb +11 -7
  72. data/lib/zm/connector/ssl.rb +10 -0
  73. data/lib/zm/connector.rb +2 -1
  74. data/lib/zm/soap_request/request_methods_admin.rb +10 -6
  75. data/lib/zm/soap_request/soap_context.rb +2 -2
  76. data/lib/zm/support/cache/file_store.rb +2 -11
  77. data/lib/zm/support/cache/store.rb +4 -2
  78. data/lib/zm/utils/log_filter.rb +13 -5
  79. data/lib/zm-ruby-client.rb +0 -2
  80. data/sig/zm/client/admin_objects.rbs +119 -11
  81. data/sig/zm/client/base.rbs +81 -4
  82. data/sig/zm/client/cluster.rbs +13 -6
  83. data/sig/zm/client/mailbox.rbs +91 -2
  84. metadata +18 -17
  85. data/lib/zm/modules/common/zimbra-attrs.csv +0 -737
@@ -11,6 +11,7 @@ module Zm
11
11
  super()
12
12
  @parent = parent
13
13
  @persistent = false
14
+ @response_class ||= AdminObjectsResponse
14
15
  reset_query_params
15
16
  end
16
17
 
@@ -62,28 +63,33 @@ module Zm
62
63
  self
63
64
  end
64
65
 
65
- def find_each(offset: 0, limit: 1_000, &)
66
- previous_persistent = @persistent.dup
66
+ def find_in_batches(offset: 0, limit: 1_000)
67
+ previous_persistent = @persistent
67
68
  @persistent = true
68
69
 
69
- _attrs = @attrs
70
- total = count
71
- @attrs = _attrs
70
+ total = dup.count
72
71
 
73
72
  @offset = offset
74
73
  @limit = limit
75
74
 
76
75
  while @offset < total
77
- build_response.each(&)
76
+ batch = build_response
77
+ break if batch.empty?
78
+
79
+ yield batch
78
80
  @offset += @limit
79
81
  end
80
-
82
+ nil
83
+ ensure
81
84
  ldap_filter.clear
82
85
  reset_query_params
83
-
84
86
  @persistent = previous_persistent
85
87
  end
86
88
 
89
+ def find_each(offset: 0, limit: 1_000, &)
90
+ find_in_batches(offset:, limit:) { |batch| batch.each(&) }
91
+ end
92
+
87
93
  def make_query
88
94
  response = sac.invoke(build_query)
89
95
  clear unless @persistent
@@ -136,6 +142,10 @@ module Zm
136
142
 
137
143
  private
138
144
 
145
+ def build_response
146
+ @response_class.new(self, @builder_class.new(@parent, make_query).make)
147
+ end
148
+
139
149
  def format_mass_items_parameter(items)
140
150
  items.map! do |item|
141
151
  if item.is_a?(@child_class)
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zm
4
+ module Client
5
+ module Base
6
+ # objects returned by AdminObjectsCollection#all and find_in_batches
7
+ class AdminObjectsResponse < Array
8
+ def initialize(collection, items = [])
9
+ super(items)
10
+ @collection = collection
11
+ end
12
+
13
+ def update_all!(hash = nil, onerror: AdminObjectsCollection::ONERRORS.first, update_attributes: true, **attrs)
14
+ @collection.mass_update!(self, hash, onerror:, update_attributes:, **attrs)
15
+ end
16
+
17
+ def delete_all!(onerror: AdminObjectsCollection::ONERRORS.first)
18
+ @collection.mass_delete!(self, onerror:)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -19,13 +19,44 @@ module Zm
19
19
  item.id = json.delete(:id)
20
20
  item.name = json.delete(:name)
21
21
 
22
- formatted_json(json).each do |k, v|
23
- valorise(item, k, v)
24
- end
22
+ nodes = json[:a]
23
+ return item if nodes.nil?
24
+
25
+ ivars = item.class.zimbra_attrs_ivars if item.class.respond_to?(:zimbra_attrs_ivars)
26
+ each_attribute(nodes) { |key, value| assign(item, ivars, key, value) }
25
27
 
26
28
  item
27
29
  end
28
30
 
31
+ # Zimbra sends the values of a multi-valued attribute one after the other
32
+ def each_attribute(nodes)
33
+ key = value = nil
34
+ multi = false
35
+
36
+ nodes.each do |node|
37
+ name = node[:n]
38
+ next if name.nil?
39
+
40
+ if name == key
41
+ value = [value] unless multi
42
+ multi = true
43
+ value << node[:_content]
44
+ else
45
+ yield(key, value) unless key.nil?
46
+ key = name
47
+ value = node[:_content]
48
+ multi = false
49
+ end
50
+ end
51
+
52
+ yield(key, value) unless key.nil?
53
+ end
54
+
55
+ def assign(item, ivars, key, value)
56
+ ivar = ivars&.[](key)
57
+ ivar ? item.instance_variable_set(ivar, convert_json_string_value(value)) : valorise(item, key, value)
58
+ end
59
+
29
60
  def formatted_json(json)
30
61
  return [] if json[:a].nil?
31
62
 
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zm
4
+ module Client
5
+ module Base
6
+ # cos and cos= accessors of an admin object linked to a COS by one of its Zimbra attributes
7
+ module HasCos
8
+ DEFAULT_COS_NAME = 'default'
9
+ NOT_INHERITED_FROM_COS = %i[cn description zimbraNotes].freeze
10
+
11
+ def has_cos(attribute, effective: false)
12
+ define_cos_reader(:"@#{attribute}")
13
+ define_cos_writer(:"#{attribute}=")
14
+ include(EffectiveCos) if effective
15
+ end
16
+
17
+ private
18
+
19
+ def define_cos_reader(ivar)
20
+ define_method(:cos) do
21
+ id = instance_variable_get(ivar)
22
+ return nil if id.nil?
23
+ return @cos if @cos&.id == id
24
+
25
+ @cos = @parent.coses.find_by(id:)
26
+ end
27
+ end
28
+
29
+ def define_cos_writer(setter)
30
+ define_method(:cos=) do |value|
31
+ unless value.nil? || (value.is_a?(Cos) && value.recorded?)
32
+ raise ArgumentError, "cos must be nil or a recorded Zm::Client::Cos, got #{value.class}"
33
+ end
34
+
35
+ @cos = value
36
+ public_send(setter, value&.id)
37
+ value
38
+ end
39
+ end
40
+
41
+ # COS applied by Zimbra to a mailbox (its own, else the default COS of its domain, else the "default" COS)
42
+ # and reset of the mailbox attributes to the values inherited from it
43
+ module EffectiveCos
44
+ def effective_cos
45
+ cos || @parent.domains.find_by(name: domain_name).cos || @parent.coses.find_by(name: DEFAULT_COS_NAME)
46
+ end
47
+
48
+ def reset_to_cos!(cos: effective_cos)
49
+ reset = attrs_equal_to_cos(cos)
50
+ return reset if reset.empty?
51
+
52
+ hash = reset.to_h { |name| [name, ''] }
53
+ do_update!(hash)
54
+ do_update_attributes(hash)
55
+ reset
56
+ end
57
+
58
+ private
59
+
60
+ def attrs_equal_to_cos(cos)
61
+ cos_inheritable_attrs.select { |name| same_as_cos_value?(public_send(name), cos.public_send(name)) }
62
+ end
63
+
64
+ def cos_inheritable_attrs
65
+ (self.class.zimbra_attrs_writable & Cos.zimbra_attrs_writable) - NOT_INHERITED_FROM_COS
66
+ end
67
+
68
+ def same_as_cos_value?(value, cos_value)
69
+ !value.nil? && !cos_value.nil? && same_zimbra_values?(value, cos_value)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zm
4
+ module Client
5
+ module Base
6
+ # declares the Zimbra LDAP attributes of an admin object: accessors, writable attributes list and changes tracking
7
+ module HasZimbraAttributes
8
+ def self.extended(base)
9
+ base.include(InstanceMethods)
10
+ end
11
+
12
+ def zimbra_attr(*names, readonly: false)
13
+ names = names.flatten.map!(&:to_sym).uniq
14
+ readonly ? attr_accessor(*names) : define_tracked_accessors(names)
15
+
16
+ added = zimbra_attrs_writable
17
+ removed = zimbra_attrs_readonly
18
+ added, removed = removed, added if readonly
19
+ removed.reject! { |name| names.include?(name) }
20
+ added.concat(names - added)
21
+
22
+ @attrs_write = @attrs_write_ivars = @zimbra_attrs_ivars = nil
23
+ names
24
+ end
25
+
26
+ def zimbra_attrs_writable
27
+ @zimbra_attrs_writable ||= inherited_zimbra_attrs(:zimbra_attrs_writable)
28
+ end
29
+
30
+ def zimbra_attrs_readonly
31
+ @zimbra_attrs_readonly ||= inherited_zimbra_attrs(:zimbra_attrs_readonly)
32
+ end
33
+
34
+ def zimbra_attrs
35
+ zimbra_attrs_writable + zimbra_attrs_readonly
36
+ end
37
+
38
+ def attrs_write
39
+ @attrs_write ||= zimbra_attrs_writable.map(&:name).freeze
40
+ end
41
+
42
+ def attrs_write_ivars
43
+ @attrs_write_ivars ||= zimbra_attrs_writable.map { |name| :"@#{name}" }.freeze
44
+ end
45
+
46
+ def zimbra_attrs_ivars
47
+ @zimbra_attrs_ivars ||= zimbra_attrs.to_h { |name| [name.name, :"@#{name}"] }.freeze
48
+ end
49
+
50
+ private
51
+
52
+ def define_tracked_accessors(names)
53
+ attr_reader(*names)
54
+
55
+ names.each do |name|
56
+ ivar = :"@#{name}"
57
+ define_method(:"#{name}=") do |value|
58
+ (@zimbra_attrs_changes ||= {})[name] = true
59
+ instance_variable_set(ivar, value)
60
+ end
61
+ end
62
+ end
63
+
64
+ def inherited_zimbra_attrs(method)
65
+ superclass.respond_to?(method) ? superclass.public_send(method).dup : []
66
+ end
67
+
68
+ # changes tracking of the writable Zimbra attributes, sent by modify!
69
+ module InstanceMethods
70
+ EMPTY_CHANGES = [].freeze
71
+
72
+ def attrs_write
73
+ self.class.attrs_write
74
+ end
75
+
76
+ def zimbra_attrs_changed
77
+ @zimbra_attrs_changes.nil? ? EMPTY_CHANGES : @zimbra_attrs_changes.keys
78
+ end
79
+
80
+ def zimbra_attrs_changed?
81
+ !@zimbra_attrs_changes.nil?
82
+ end
83
+
84
+ def zimbra_attr_changed?(name)
85
+ !@zimbra_attrs_changes.nil? && @zimbra_attrs_changes.key?(name.to_sym)
86
+ end
87
+
88
+ def zimbra_attr_will_change!(name)
89
+ name = name.to_sym
90
+ raise ArgumentError, "#{name} is not a writable Zimbra attribute" unless attrs_write.include?(name.name)
91
+
92
+ (@zimbra_attrs_changes ||= {})[name] = true
93
+ self
94
+ end
95
+
96
+ def clear_zimbra_attrs_changes
97
+ remove_instance_variable(:@zimbra_attrs_changes) if defined?(@zimbra_attrs_changes)
98
+ self
99
+ end
100
+
101
+ private
102
+
103
+ def zimbra_attrs_to_update(hash, force)
104
+ hash.delete_if do |key, value|
105
+ value.nil? || !respond_to?(key) || (!force && zimbra_attr_unchanged?(key, value))
106
+ end
107
+ end
108
+
109
+ def zimbra_attr_unchanged?(key, value)
110
+ ivar = self.class.zimbra_attrs_ivars[key.to_s]
111
+ return false if ivar.nil? || zimbra_attr_changed?(key)
112
+
113
+ current = instance_variable_get(ivar)
114
+ current == value || same_zimbra_values?(current, value)
115
+ end
116
+
117
+ def same_zimbra_values?(current, value)
118
+ current = Array(current)
119
+ value = Array(value)
120
+ current.size == value.size && current.each_with_index.all? { |item, i| item.to_s == value[i].to_s }
121
+ end
122
+
123
+ def forget_zimbra_attr_change(key)
124
+ return if @zimbra_attrs_changes.nil?
125
+
126
+ @zimbra_attrs_changes.delete(key.to_sym)
127
+ clear_zimbra_attrs_changes if @zimbra_attrs_changes.empty?
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -29,6 +29,7 @@ module Zm
29
29
  obj = super
30
30
  obj.remove_instance_variable(:@id) if obj.instance_variable_defined?(:@id)
31
31
  obj.remove_instance_variable(:@jsns_builder) if obj.instance_variable_defined?(:@jsns_builder)
32
+ obj.remove_instance_variable(:@zimbra_attrs_changes) if obj.instance_variable_defined?(:@zimbra_attrs_changes)
32
33
  obj.remove_dependents_of(self)
33
34
  yield(obj) if block_given?
34
35
  obj
@@ -64,6 +65,7 @@ module Zm
64
65
  def instance_variables_map
65
66
  keys = instance_variables.dup
66
67
  keys.delete(:@parent)
68
+ keys.delete(:@zimbra_attrs_changes)
67
69
  keys.map { |key| [key, instance_variable_get(key)] }
68
70
  end
69
71
 
@@ -11,7 +11,7 @@ module Zm
11
11
  end
12
12
 
13
13
  def ids
14
- @json.dig(:Body, json_key, :hit)&.map { |s| s[:id] } || []
14
+ @json.dig(json_key, :hit)&.map { |s| s[:id] } || []
15
15
  end
16
16
 
17
17
  private
@@ -1,201 +1,78 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'csv'
4
-
5
3
  module Zm
6
4
  module Client
7
5
  module Base
6
+ # deprecated: attributes are declared in each class with zimbra_attr, will be removed in 4.0
8
7
  class ZimbraAttributesCollection
9
8
  include Zm::Utils::MissingMethodStaticCollection
10
9
 
11
10
  MUTEX = Mutex.new
12
- CSV_OPTIONS = {
13
- headers: true, skip_blanks: true, strip: true, header_converters: ->(header) { header.to_sym }
11
+ OBJECTS = {
12
+ account: [:Account, 'account'],
13
+ cos: [:Cos, 'cos'],
14
+ domain: [:Domain, 'domain'],
15
+ server: [:Server, 'server'],
16
+ distributionlist: [:DistributionList, 'distributionList'],
17
+ resource: [:Resource, 'calendarResource']
14
18
  }.freeze
15
19
 
16
20
  class << self
17
- def shared(parent)
18
- path = File.expand_path(parent.config.zimbra_attributes_path)
19
- MUTEX.synchronize { shared_instances[path] ||= new(parent).tap(&:set_methods) }
20
- end
21
-
22
- def load_attributes(path)
23
- CSV.open(path, **CSV_OPTIONS).map do |attr|
24
- attr_h = attr.to_h
25
- attr_h.compact!
26
- ZimbraAttribute.new(**attr_h)
27
- end.freeze
28
- end
29
-
30
- private
31
-
32
- def shared_instances
33
- @shared_instances ||= {}
21
+ def shared(_parent = nil)
22
+ MUTEX.synchronize { @shared ||= new }
34
23
  end
35
24
  end
36
25
 
37
- attr_reader :path
38
-
39
- def initialize(parent)
40
- @path = File.expand_path(parent.config.zimbra_attributes_path)
41
- @all = self.class.load_attributes(@path)
42
- end
43
-
44
- def set_methods
45
- set_server_methods
46
- set_cos_methods
47
- set_domain_methods
48
- set_account_methods
49
- set_distributionlist_methods
50
- set_resource_methods
51
- end
52
-
53
- # ###############################
54
- # Cos
55
- # ###############################
56
-
57
- def all_cos_attrs
58
- @all_cos_attrs ||= @all.select(&:is_cos_scoped?).freeze
59
- end
60
-
61
- def all_cos_attrs_writable
62
- @all_cos_attrs_writable ||= all_cos_attrs.reject(&:immutable?).freeze
63
- end
64
-
65
- def all_cos_attrs_writable_names
66
- @all_cos_attrs_writable_names ||= all_cos_attrs_writable.map(&:name).freeze
67
- end
68
-
69
- def all_cos_attr_types_h
70
- @all_cos_attr_types_h ||= all_cos_attrs.to_h { |attr| [attr.name, attr.type] }.freeze
71
- end
72
-
73
- def set_cos_methods
74
- Cos.attr_accessor(*all_cos_attrs.map { |attr| attr.name.to_sym })
75
- end
76
-
77
- # ###############################
78
- # Server
79
- # ###############################
80
-
81
- def all_server_attrs
82
- @all_server_attrs ||= @all.select(&:is_server_scoped?).freeze
83
- end
84
-
85
- def all_server_attrs_writable
86
- @all_server_attrs_writable ||= all_server_attrs.reject(&:immutable?).freeze
87
- end
88
-
89
- def all_server_attrs_writable_names
90
- @all_server_attrs_writable_names ||= all_server_attrs_writable.map(&:name).freeze
91
- end
92
-
93
- def all_server_attr_types_h
94
- @all_server_attr_types_h ||= all_server_attrs.to_h { |attr| [attr.name, attr.type] }.freeze
95
- end
96
-
97
- def set_server_methods
98
- Server.attr_accessor(*all_server_attrs.map { |attr| attr.name.to_sym })
26
+ def initialize(_parent = nil)
27
+ readonly = readonly_names
28
+ @all = object_scopes.map { |name, scope| build_attribute(name, scope, readonly.include?(name)) }.freeze
29
+ @by_name = @all.to_h { |attr| [attr.name, attr] }
30
+ @by_object = {}
99
31
  end
100
32
 
101
- # ###############################
102
- # Domain
103
- # ###############################
104
-
105
- def all_domain_attrs
106
- @all_domain_attrs ||= @all.select(&:is_domain_scoped?).freeze
107
- end
108
-
109
- def all_domain_attrs_writable
110
- @all_domain_attrs_writable ||= all_domain_attrs.reject(&:immutable?).freeze
111
- end
112
-
113
- def all_domain_attrs_writable_names
114
- @all_domain_attrs_writable_names ||= all_domain_attrs_writable.map(&:name).freeze
115
- end
116
-
117
- def all_domain_attr_types_h
118
- @all_domain_attr_types_h ||= all_domain_attrs.to_h { |attr| [attr.name, attr.type] }.freeze
119
- end
120
-
121
- def set_domain_methods
122
- Domain.attr_accessor(*all_domain_attrs.map { |attr| attr.name.to_sym })
123
- end
124
-
125
- # ###############################
126
- # Account
127
- # ###############################
128
-
129
- def all_account_attrs
130
- @all_account_attrs ||= @all.select { |attr| attr.is_account_scoped? || attr.is_mailRecipient_scoped? }.freeze
33
+ def path
34
+ nil
131
35
  end
132
36
 
133
- def all_account_attrs_writable
134
- @all_account_attrs_writable ||= all_account_attrs.reject(&:immutable?).freeze
135
- end
136
-
137
- def all_account_attrs_writable_names
138
- @all_account_attrs_writable_names ||= all_account_attrs_writable.map(&:name).freeze
139
- end
37
+ def set_methods; end
140
38
 
141
- def all_account_attr_types_h
142
- @all_account_attr_types_h ||= all_account_attrs.to_h { |attr| [attr.name, attr.type] }.freeze
143
- end
144
-
145
- def set_account_methods
146
- Account.attr_accessor(*all_account_attrs.map { |attr| attr.name.to_sym })
147
- end
148
-
149
- # ###############################
150
- # Distribution List
151
- # ###############################
39
+ OBJECTS.each do |object, (class_name, _scope)|
40
+ define_method(:"all_#{object}_attrs") do
41
+ @by_object[object] ||= Client.const_get(class_name).zimbra_attrs.map { |name| @by_name[name.name] }.freeze
42
+ end
152
43
 
153
- def all_distributionlist_attrs
154
- @all_distributionlist_attrs ||= @all.select do |attr|
155
- attr.is_distributionList_scoped? || attr.is_group_scoped? || attr.is_groupDynamicUnit_scoped? || attr.is_groupStaticUnit_scoped?
156
- end.freeze
157
- end
44
+ define_method(:"all_#{object}_attrs_writable") do
45
+ send(:"all_#{object}_attrs").reject(&:immutable?)
46
+ end
158
47
 
159
- def all_distributionlist_attrs_writable
160
- @all_distributionlist_attrs_writable ||= all_distributionlist_attrs.reject(&:immutable?).freeze
161
- end
48
+ define_method(:"all_#{object}_attrs_writable_names") { Client.const_get(class_name).attrs_write }
162
49
 
163
- def all_distributionlist_attrs_writable_names
164
- @all_distributionlist_attrs_writable_names ||= all_distributionlist_attrs_writable.map(&:name).freeze
165
- end
166
-
167
- def all_distributionlist_attrs_types_h
168
- @all_distributionlist_attrs_types_h ||= all_distributionlist_attrs.to_h do |attr|
169
- [attr.name, attr.type]
170
- end.freeze
171
- end
50
+ define_method(:"all_#{object}_attrs_types_h") do
51
+ send(:"all_#{object}_attrs").to_h { |attr| [attr.name, attr.type] }
52
+ end
172
53
 
173
- def set_distributionlist_methods
174
- DistributionList.attr_accessor(*all_distributionlist_attrs.map { |attr| attr.name.to_sym })
54
+ define_method(:"set_#{object}_methods") { nil }
175
55
  end
176
56
 
177
- # ###############################
178
- # Calendar Resource
179
- # ###############################
57
+ alias all_account_attr_types_h all_account_attrs_types_h
58
+ alias all_cos_attr_types_h all_cos_attrs_types_h
59
+ alias all_domain_attr_types_h all_domain_attrs_types_h
60
+ alias all_server_attr_types_h all_server_attrs_types_h
180
61
 
181
- def all_resource_attrs
182
- @all_resource_attrs ||= @all.select(&:is_calendarResource_scoped?).freeze
183
- end
62
+ private
184
63
 
185
- def all_resource_attrs_writable
186
- @all_resource_attrs_writable ||= all_resource_attrs.reject(&:immutable?).freeze
187
- end
188
-
189
- def all_resource_attrs_writable_names
190
- @all_resource_attrs_writable_names ||= all_resource_attrs_writable.map(&:name).freeze
64
+ def object_scopes
65
+ OBJECTS.each_value.with_object(Hash.new { |hash, name| hash[name] = [] }) do |(class_name, scope), scopes|
66
+ Client.const_get(class_name).zimbra_attrs.each { |name| scopes[name] << scope }
67
+ end
191
68
  end
192
69
 
193
- def all_resource_attrs_types_h
194
- @all_resource_attrs_types_h ||= all_resource_attrs.to_h { |attr| [attr.name, attr.type] }.freeze
70
+ def readonly_names
71
+ OBJECTS.each_value.flat_map { |class_name, _scope| Client.const_get(class_name).zimbra_attrs_readonly }.to_set
195
72
  end
196
73
 
197
- def set_resource_methods
198
- Resource.attr_accessor(*all_resource_attrs.map { |attr| attr.name.to_sym })
74
+ def build_attribute(name, scope, immutable)
75
+ ZimbraAttribute.new(name: name.name, optionalIn: scope.join(','), immutable: immutable ? '1' : nil)
199
76
  end
200
77
  end
201
78
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zm
4
+ module Client
5
+ module Base
6
+ # Zimbra attributes of an admin object as jsns values: all the set ones (create) or the changed ones (modify)
7
+ module ZimbraAttrsJsns
8
+ CLEARED_VALUES = [''].freeze
9
+
10
+ def attrs_only_set_h
11
+ selected_attrs = @item.class.attrs_write_ivars
12
+ attrs_only_set = @item.instance_variables & selected_attrs
13
+
14
+ attrs_only_set.to_h do |name|
15
+ values = @item.instance_variable_get(name)
16
+ values = [values] unless values.is_a?(Array)
17
+ [name.name[1..], values]
18
+ end
19
+ end
20
+
21
+ def attrs_changed_h
22
+ ivars = @item.class.zimbra_attrs_ivars
23
+
24
+ @item.zimbra_attrs_changed.to_h do |name|
25
+ key = name.name
26
+ [key, changed_values(@item.instance_variable_get(ivars[key]))]
27
+ end
28
+ end
29
+
30
+ def attrs_to_update_h(force)
31
+ force ? attrs_only_set_h : attrs_changed_h
32
+ end
33
+
34
+ private
35
+
36
+ def changed_values(values)
37
+ return CLEARED_VALUES if values.nil? || (values.is_a?(Array) && values.empty?)
38
+
39
+ values.is_a?(Array) ? values : [values]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end