| Module | ActiveLdap::Operations::Common |
| In: |
lib/active_ldap/operations.rb
|
| VALID_SEARCH_OPTIONS | = | [:attribute, :value, :filter, :prefix, :classes, :scope, :limit, :attributes, :sort_by, :order, :connection, :base] |
# File lib/active_ldap/operations.rb, line 103
103: def count(options={})
104: search(options).size
105: end
# File lib/active_ldap/operations.rb, line 79
79: def exist?(dn, options={})
80: attr, value, prefix = split_search_value(dn)
81:
82: options_for_leaf = {
83: :attribute => attr,
84: :value => value,
85: :prefix => prefix,
86: :limit => 1,
87: }
88:
89: attribute = attr || ensure_search_attribute
90: options_for_non_leaf = {
91: :attribute => attr,
92: :value => value,
93: :prefix => ["#{attribute}=#{value}", prefix].compact.join(","),
94: :limit => 1,
95: :scope => :base,
96: }
97:
98: !search(options_for_leaf.merge(options)).empty? or
99: !search(options_for_non_leaf.merge(options)).empty?
100: end
# File lib/active_ldap/operations.rb, line 27
27: def search(options={}, &block)
28: validate_search_options(options)
29: attr = options[:attribute]
30: value = options[:value] || '*'
31: filter = options[:filter]
32: prefix = options[:prefix]
33: classes = options[:classes]
34:
35: value = value.first if value.is_a?(Array) and value.first.size == 1
36:
37: _attr = nil
38: _prefix = nil
39: if attr.nil? or attr == dn_attribute
40: _attr, value, _prefix = split_search_value(value)
41: end
42: attr ||= _attr || ensure_search_attribute
43: prefix ||= _prefix
44: filter ||= [attr, value]
45: filter = [:and, filter, *object_class_filters(classes)]
46: _base = options[:base] ? [options[:base]] : [prefix, base]
47: _base = prepare_search_base(_base)
48: if options.has_key?(:ldap_scope)
49: message = _(":ldap_scope search option is deprecated. " \
50: "Use :scope instead.")
51: ActiveSupport::Deprecation.warn(message)
52: options[:scope] ||= options[:ldap_scope]
53: end
54: search_options = {
55: :base => _base,
56: :scope => options[:scope] || scope,
57: :filter => filter,
58: :limit => options[:limit],
59: :attributes => options[:attributes],
60: :sort_by => options[:sort_by] || sort_by,
61: :order => options[:order] || order,
62: }
63:
64: options[:connection] ||= connection
65: values = options[:connection].search(search_options) do |dn, attrs|
66: attributes = {}
67: attrs.each do |key, _value|
68: normalized_attr, normalized_value =
69: normalize_attribute_options(key, _value)
70: attributes[normalized_attr] ||= []
71: attributes[normalized_attr].concat(normalized_value)
72: end
73: [dn, attributes]
74: end
75: values = values.collect {|_value| yield(_value)} if block_given?
76: values
77: end
# File lib/active_ldap/operations.rb, line 125
125: def ensure_base(target)
126: [truncate_base(target), base.to_s].reject do |component|
127: component.blank?
128: end.join(',')
129: end
# File lib/active_ldap/operations.rb, line 120
120: def ensure_dn_attribute(target)
121: "#{dn_attribute}=" +
122: target.gsub(/^\s*#{Regexp.escape(dn_attribute)}\s*=\s*/i, '')
123: end
# File lib/active_ldap/operations.rb, line 116
116: def ensure_search_attribute(*candidates)
117: default_search_attribute || "objectClass"
118: end
# File lib/active_ldap/operations.rb, line 112
112: def extract_options_from_args!(args)
113: args.last.is_a?(Hash) ? args.pop : {}
114: end
# File lib/active_ldap/operations.rb, line 166
166: def object_class_filters(classes=nil)
167: expected_classes = (classes || required_classes).collect do |name|
168: Escape.ldap_filter_escape(name)
169: end
170: unexpected_classes = excluded_classes.collect do |name|
171: Escape.ldap_filter_escape(name)
172: end
173: filters = []
174: unless expected_classes.empty?
175: filters << ["objectClass", "=", *expected_classes]
176: end
177: unless unexpected_classes.empty?
178: filters << [:not, [:or, ["objectClass", "=", *unexpected_classes]]]
179: end
180: filters
181: end
# File lib/active_ldap/operations.rb, line 153
153: def prepare_search_base(components)
154: components.compact.collect do |component|
155: case component
156: when String
157: component
158: when DN
159: component.to_s
160: else
161: DN.new(*component).to_s
162: end
163: end.reject{|x| x.empty?}.join(",")
164: end
# File lib/active_ldap/operations.rb, line 183
183: def split_search_value(value)
184: attr = prefix = nil
185:
186: begin
187: dn = DN.parse(value)
188: attr, value = dn.rdns.first.to_a.first
189: rest = dn.rdns[1..-1]
190: prefix = DN.new(*rest).to_s unless rest.empty?
191: rescue DistinguishedNameInputInvalid
192: return [attr, value, prefix]
193: rescue DistinguishedNameInvalid
194: begin
195: dn = DN.parse("DUMMY=#{value}")
196: _, value = dn.rdns.first.to_a.first
197: rest = dn.rdns[1..-1]
198: prefix = DN.new(*rest).to_s unless rest.empty?
199: rescue DistinguishedNameInvalid
200: end
201: end
202:
203: prefix = nil if prefix == base
204: prefix = truncate_base(prefix) if prefix
205: [attr, value, prefix]
206: end
# File lib/active_ldap/operations.rb, line 131
131: def truncate_base(target)
132: return nil if target.blank?
133: return target if base.nil?
134:
135: parsed_target = nil
136: if target.is_a?(DN)
137: parsed_target = target
138: elsif /,/ =~ target
139: begin
140: parsed_target = DN.parse(target)
141: rescue DistinguishedNameInvalid
142: end
143: end
144:
145: return target if parsed_target.nil?
146: begin
147: (parsed_target - base).to_s
148: rescue ArgumentError
149: target
150: end
151: end