Module | ActiveLdap::Operations::Find |
In: |
lib/active_ldap/operations.rb
|
Finds the first match for value where |value| is the value of some |field|, or the wildcard match. This is only useful for derived classes. usage: Subclass.find(:all, :attribute => "cn", :value => "some*val")
Subclass.find(:all, 'some*val')
# File lib/active_ldap/operations.rb, line 201 201: def find(*args) 202: options = extract_options_from_args!(args) 203: args = [:first] if args.empty? and !options.empty? 204: case args.first 205: when :first 206: options[:value] ||= args[1] 207: find_initial(options) 208: when :last 209: options[:value] ||= args[1] 210: find_last(options) 211: when :all 212: options[:value] ||= args[1] 213: find_every(options) 214: else 215: find_from_dns(args, options) 216: end 217: end
# File lib/active_ldap/operations.rb, line 355 355: def ensure_dn(target) 356: attr, value, prefix = split_search_value(target) 357: "#{attr || dn_attribute}=#{value},#{prefix || base}" 358: end
# File lib/active_ldap/operations.rb, line 262 262: def find_every(options) 263: options = options.dup 264: sort_by = options.delete(:sort_by) || self.sort_by 265: order = options.delete(:order) || self.order 266: limit = options.delete(:limit) if sort_by or order 267: options[:attributes] |= ["objectClass"] if options[:attributes] 268: 269: results = search(options).collect do |dn, attrs| 270: instantiate([dn, attrs, {:connection => options[:connection]}]) 271: end 272: return results if sort_by.nil? and order.nil? 273: 274: sort_by ||= "dn" 275: if sort_by.downcase == "dn" 276: results = results.sort_by {|result| DN.parse(result.dn)} 277: else 278: results = results.sort_by {|result| result.send(sort_by)} 279: end 280: 281: results.reverse! if normalize_sort_order(order || "ascend") == :descend 282: results = results[0, limit] if limit 283: results 284: end
# File lib/active_ldap/operations.rb, line 286 286: def find_from_dns(dns, options) 287: expects_array = dns.first.is_a?(Array) 288: return [] if expects_array and dns.first.empty? 289: 290: dns = dns.flatten.compact.uniq 291: 292: case dns.size 293: when 0 294: raise EntryNotFound, _("Couldn't find %s without a DN") % name 295: when 1 296: result = find_one(dns.first, options) 297: expects_array ? [result] : result 298: else 299: find_some(dns, options) 300: end 301: end
# File lib/active_ldap/operations.rb, line 241 241: def find_initial(options) 242: find_every(options.merge(:limit => 1)).first 243: end
# File lib/active_ldap/operations.rb, line 245 245: def find_last(options) 246: order = options[:order] || self.order || 'ascend' 247: order = normalize_sort_order(order) == :ascend ? :descend : :ascend 248: find_initial(options.merge(:order => order)) 249: end
# File lib/active_ldap/operations.rb, line 303 303: def find_one(dn, options) 304: attr, value, prefix = split_search_value(dn) 305: filter = [attr || ensure_search_attribute, 306: Escape.ldap_filter_escape(value)] 307: filter = [:and, filter, options[:filter]] if options[:filter] 308: options = {:prefix => prefix}.merge(options.merge(:filter => filter)) 309: result = find_initial(options) 310: if result 311: result 312: else 313: args = [self.is_a?(Class) ? name : self.class.name, 314: dn] 315: if options[:filter] 316: format = _("Couldn't find %s: DN: %s: filter: %s") 317: args << options[:filter].inspect 318: else 319: format = _("Couldn't find %s: DN: %s") 320: end 321: raise EntryNotFound, format % args 322: end 323: end
# File lib/active_ldap/operations.rb, line 325 325: def find_some(dns, options) 326: dn_filters = dns.collect do |dn| 327: attr, value, prefix = split_search_value(dn) 328: attr ||= ensure_search_attribute 329: filter = [attr, value] 330: if prefix 331: filter = [:and, 332: filter, 333: [dn, "*,#{Escape.ldap_filter_escape(prefix)},#{base}"]] 334: end 335: filter 336: end 337: filter = [:or, *dn_filters] 338: filter = [:and, filter, options[:filter]] if options[:filter] 339: result = find_every(options.merge(:filter => filter)) 340: if result.size == dns.size 341: result 342: else 343: args = [self.is_a?(Class) ? name : self.class.name, 344: dns.join(", ")] 345: if options[:filter] 346: format = _("Couldn't find all %s: DNs (%s): filter: %s") 347: args << options[:filter].inspect 348: else 349: format = _("Couldn't find all %s: DNs (%s)") 350: end 351: raise EntryNotFound, format % args 352: end 353: end