3

Is there a better way to search for users and computers specifically using the Net-ldap gem?

Here is what I am currently having to do to get only users.

results = search :base => @base, :filter => Net::LDAP::Filter.eq("cn", "*")
  @results = Array.new

  results.each do |result|
    @results.push result if result[:objectclass].include? "person" unless result[:objectclass].include? "computer"

Seems like there would be a better way. I can't see anything obvious in the documentation.

Beaon
  • 347
  • 1
  • 4
  • 16

2 Answers2

5

You can use the Join filter functionality of net-ldap:

filter = Net::LDAP::Filter.eq("sAMAccountName", "*")
filter2 = Net::LDAP::Filter.eq("objectCategory", "organizationalPerson")

joined_filter = Net::LDAP::Filter.join(filter, filter2)

ldap.search(:base => treebase, :filter => joined_filter) do |entry|
    puts entry.sAMAccountName
end
Neil Hoff
  • 2,025
  • 4
  • 29
  • 53
2

If you know the objectClass that is used for persons, you could use the filter '(objectClass=person)', replacing 'person' with the objectClass. Most implementations will use 'person' or an objectClass that inherits from 'person' such as 'inetOrgPerson'. Using the filter '(cn=*)' will most likely get entries that are not persons.

Try using Filter.eq("objectClass","person")

Terry Gardner
  • 10,957
  • 2
  • 28
  • 38
  • Unfortunately that was one of the first things I tried. I'll give it another go and show you what output I get later today. – Beaon Jun 24 '11 at 18:42