| 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 101
101: def count(options={})
102: search(options).size
103: 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: }
87:
88: attribute = attr || ensure_search_attribute
89: options_for_non_leaf = {
90: :attribute => attr,
91: :value => value,
92: :prefix => ["#{attribute}=#{value}", prefix].compact.join(","),
93: :scope => :base,
94: }
95:
96: !search(options_for_leaf.merge(options)).empty? or
97: !search(options_for_non_leaf.merge(options)).empty?
98: 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: if filter.nil? and !value.is_a?(String)
37: message = _("Search value must be a String: %s") % value.inspect
38: raise ArgumentError, message
39: end
40:
41: _attr, value, _prefix = split_search_value(value)
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]
47: _base ||= [prefix, base].compact.reject{|x| x.empty?}.join(",")
48: if options.has_key?(:ldap_scope)
49: logger.warning do
50: _(":ldap_scope search option is deprecated. Use :scope instead.")
51: end
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: 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: value = [dn, attributes]
74: value = yield(value) if block_given?
75: value
76: end
77: end
# File lib/active_ldap/operations.rb, line 123
123: def ensure_base(target)
124: [truncate_base(target), base].join(',')
125: end
# File lib/active_ldap/operations.rb, line 118
118: def ensure_dn_attribute(target)
119: "#{dn_attribute}=" +
120: target.gsub(/^\s*#{Regexp.escape(dn_attribute)}\s*=\s*/i, '')
121: end
# File lib/active_ldap/operations.rb, line 114
114: def ensure_search_attribute(*candidates)
115: default_search_attribute || "objectClass"
116: end
# File lib/active_ldap/operations.rb, line 110
110: def extract_options_from_args!(args)
111: args.last.is_a?(Hash) ? args.pop : {}
112: end
# File lib/active_ldap/operations.rb, line 139
139: def object_class_filters(classes=nil)
140: (classes || required_classes).collect do |name|
141: ["objectClass", Escape.ldap_filter_escape(name)]
142: end
143: end
# File lib/active_ldap/operations.rb, line 145
145: def split_search_value(value)
146: attr = prefix = nil
147: begin
148: dn = DN.parse(value)
149: attr, value = dn.rdns.first.to_a.first
150: rest = dn.rdns[1..-1]
151: prefix = DN.new(*rest).to_s unless rest.empty?
152: rescue DistinguishedNameInvalid
153: begin
154: dn = DN.parse("DUMMY=#{value}")
155: _, value = dn.rdns.first.to_a.first
156: rest = dn.rdns[1..-1]
157: prefix = DN.new(*rest).to_s unless rest.empty?
158: rescue DistinguishedNameInvalid
159: end
160: end
161:
162: prefix = nil if prefix == base
163: prefix = truncate_base(prefix) if prefix
164: [attr, value, prefix]
165: end
# File lib/active_ldap/operations.rb, line 127
127: def truncate_base(target)
128: if /,/ =~ target
129: begin
130: (DN.parse(target) - DN.parse(base)).to_s
131: rescue DistinguishedNameInvalid, ArgumentError
132: target
133: end
134: else
135: target
136: end
137: end