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 217 217: def find(*args) 218: options = extract_options_from_args!(args) 219: args = [:first] if args.empty? and !options.empty? 220: case args.first 221: when :first 222: options[:value] ||= args[1] 223: find_initial(options) 224: when :last 225: options[:value] ||= args[1] 226: find_last(options) 227: when :all 228: options[:value] ||= args[1] 229: find_every(options) 230: else 231: find_from_dns(args, options) 232: end 233: end
# File lib/active_ldap/operations.rb, line 371 371: def ensure_dn(target) 372: attr, value, prefix = split_search_value(target) 373: "#{attr || dn_attribute}=#{value},#{prefix || base}" 374: end
# File lib/active_ldap/operations.rb, line 278 278: def find_every(options) 279: options = options.dup 280: sort_by = options.delete(:sort_by) || self.sort_by 281: order = options.delete(:order) || self.order 282: limit = options.delete(:limit) if sort_by or order 283: options[:attributes] |= ["objectClass"] if options[:attributes] 284: 285: results = search(options).collect do |dn, attrs| 286: instantiate([dn, attrs, {:connection => options[:connection]}]) 287: end 288: return results if sort_by.nil? and order.nil? 289: 290: sort_by ||= "dn" 291: if sort_by.downcase == "dn" 292: results = results.sort_by {|result| DN.parse(result.dn)} 293: else 294: results = results.sort_by {|result| result.send(sort_by)} 295: end 296: 297: results.reverse! if normalize_sort_order(order || "ascend") == :descend 298: results = results[0, limit] if limit 299: results 300: end
# File lib/active_ldap/operations.rb, line 302 302: def find_from_dns(dns, options) 303: expects_array = dns.first.is_a?(Array) 304: return [] if expects_array and dns.first.empty? 305: 306: dns = dns.flatten.compact.uniq 307: 308: case dns.size 309: when 0 310: raise EntryNotFound, _("Couldn't find %s without a DN") % name 311: when 1 312: result = find_one(dns.first, options) 313: expects_array ? [result] : result 314: else 315: find_some(dns, options) 316: end 317: end
# File lib/active_ldap/operations.rb, line 257 257: def find_initial(options) 258: find_every(options.merge(:limit => 1)).first 259: end
# File lib/active_ldap/operations.rb, line 261 261: def find_last(options) 262: order = options[:order] || self.order || 'ascend' 263: order = normalize_sort_order(order) == :ascend ? :descend : :ascend 264: find_initial(options.merge(:order => order)) 265: end
# File lib/active_ldap/operations.rb, line 319 319: def find_one(dn, options) 320: attr, value, prefix = split_search_value(dn) 321: filter = [attr || ensure_search_attribute, 322: Escape.ldap_filter_escape(value)] 323: filter = [:and, filter, options[:filter]] if options[:filter] 324: options = {:prefix => prefix}.merge(options.merge(:filter => filter)) 325: result = find_initial(options) 326: if result 327: result 328: else 329: args = [self.is_a?(Class) ? name : self.class.name, 330: dn] 331: if options[:filter] 332: format = _("Couldn't find %s: DN: %s: filter: %s") 333: args << options[:filter].inspect 334: else 335: format = _("Couldn't find %s: DN: %s") 336: end 337: raise EntryNotFound, format % args 338: end 339: end
# File lib/active_ldap/operations.rb, line 341 341: def find_some(dns, options) 342: dn_filters = dns.collect do |dn| 343: attr, value, prefix = split_search_value(dn) 344: attr ||= ensure_search_attribute 345: filter = [attr, value] 346: if prefix 347: filter = [:and, 348: filter, 349: [dn, "*,#{Escape.ldap_filter_escape(prefix)},#{base}"]] 350: end 351: filter 352: end 353: filter = [:or, *dn_filters] 354: filter = [:and, filter, options[:filter]] if options[:filter] 355: result = find_every(options.merge(:filter => filter)) 356: if result.size == dns.size 357: result 358: else 359: args = [self.is_a?(Class) ? name : self.class.name, 360: dns.join(", ")] 361: if options[:filter] 362: format = _("Couldn't find all %s: DNs (%s): filter: %s") 363: args << options[:filter].inspect 364: else 365: format = _("Couldn't find all %s: DNs (%s)") 366: end 367: raise EntryNotFound, format % args 368: end 369: end