| 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 97
97: def count(options={})
98: search(options).size
99: end
# File lib/active_ldap/operations.rb, line 75
75: def exist?(dn, options={})
76: attr, value, prefix = split_search_value(dn)
77:
78: options_for_leaf = {
79: :attribute => attr,
80: :value => value,
81: :prefix => prefix,
82: }
83:
84: attribute = attr || ensure_search_attribute
85: options_for_non_leaf = {
86: :attribute => attr,
87: :value => value,
88: :prefix => ["#{attribute}=#{value}", prefix].compact.join(","),
89: :scope => :base,
90: }
91:
92: !search(options_for_leaf.merge(options)).empty? or
93: !search(options_for_non_leaf.merge(options)).empty?
94: 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, value, _prefix = split_search_value(value)
38: attr ||= _attr || ensure_search_attribute
39: prefix ||= _prefix
40: filter ||= [attr, value]
41: filter = [:and, filter, *object_class_filters(classes)]
42: _base = options[:base] ? [options[:base]] : [prefix, base]
43: _base = prepare_search_base(_base)
44: if options.has_key?(:ldap_scope)
45: logger.warning do
46: _(":ldap_scope search option is deprecated. Use :scope instead.")
47: end
48: options[:scope] ||= options[:ldap_scope]
49: end
50: search_options = {
51: :base => _base,
52: :scope => options[:scope] || scope,
53: :filter => filter,
54: :limit => options[:limit],
55: :attributes => options[:attributes],
56: :sort_by => options[:sort_by] || sort_by,
57: :order => options[:order] || order,
58: }
59:
60: options[:connection] ||= connection
61: options[:connection].search(search_options) do |dn, attrs|
62: attributes = {}
63: attrs.each do |key, value|
64: normalized_attr, normalized_value =
65: normalize_attribute_options(key, value)
66: attributes[normalized_attr] ||= []
67: attributes[normalized_attr].concat(normalized_value)
68: end
69: value = [dn, attributes]
70: value = yield(value) if block_given?
71: value
72: end
73: end
# File lib/active_ldap/operations.rb, line 119
119: def ensure_base(target)
120: [truncate_base(target), base].join(',')
121: end
# File lib/active_ldap/operations.rb, line 114
114: def ensure_dn_attribute(target)
115: "#{dn_attribute}=" +
116: target.gsub(/^\s*#{Regexp.escape(dn_attribute)}\s*=\s*/i, '')
117: end
# File lib/active_ldap/operations.rb, line 110
110: def ensure_search_attribute(*candidates)
111: default_search_attribute || "objectClass"
112: end
# File lib/active_ldap/operations.rb, line 106
106: def extract_options_from_args!(args)
107: args.last.is_a?(Hash) ? args.pop : {}
108: end
# File lib/active_ldap/operations.rb, line 145
145: def object_class_filters(classes=nil)
146: expected_classes = (classes || required_classes).collect do |name|
147: Escape.ldap_filter_escape(name)
148: end
149: unexpected_classes = excluded_classes.collect do |name|
150: Escape.ldap_filter_escape(name)
151: end
152: filters = []
153: unless expected_classes.empty?
154: filters << ["objectClass", "=", *expected_classes]
155: end
156: unless unexpected_classes.empty?
157: filters << [:not, [:or, ["objectClass", "=", *unexpected_classes]]]
158: end
159: filters
160: end
# File lib/active_ldap/operations.rb, line 135
135: def prepare_search_base(components)
136: components.compact.collect do |component|
137: if component.is_a?(String)
138: component
139: else
140: DN.new(*component).to_s
141: end
142: end.reject{|x| x.empty?}.join(",")
143: end
# File lib/active_ldap/operations.rb, line 162
162: def split_search_value(value)
163: attr = prefix = nil
164:
165: begin
166: dn = DN.parse(value)
167: attr, value = dn.rdns.first.to_a.first
168: rest = dn.rdns[1..-1]
169: prefix = DN.new(*rest).to_s unless rest.empty?
170: rescue DistinguishedNameInputInvalid
171: return [attr, value, prefix]
172: rescue DistinguishedNameInvalid
173: begin
174: dn = DN.parse("DUMMY=#{value}")
175: _, value = dn.rdns.first.to_a.first
176: rest = dn.rdns[1..-1]
177: prefix = DN.new(*rest).to_s unless rest.empty?
178: rescue DistinguishedNameInvalid
179: end
180: end
181:
182: prefix = nil if prefix == base
183: prefix = truncate_base(prefix) if prefix
184: [attr, value, prefix]
185: end
# File lib/active_ldap/operations.rb, line 123
123: def truncate_base(target)
124: if /,/ =~ target
125: begin
126: (DN.parse(target) - DN.parse(base)).to_s
127: rescue DistinguishedNameInvalid, ArgumentError
128: target
129: end
130: else
131: target
132: end
133: end