Home > front end >  how to create a filter of net-ldap for Ruby
how to create a filter of net-ldap for Ruby

Time:10-16

I have a working ldap search query

  ldapsearch -H ldaps://ldap.google.com -b dc=pangeare,dc=com '(memberOf=cn=pcore_readonly,ou=Groups,dc=pangeare,dc=com)'

I wonder how should I create the corresponding filter in ruby net-ldap:

treebase="dc=pangeare,dc=com"
filter = Net::LDAP::Filter.XXXXXXXXXX
ldap.search(:base => treebase, :filter=> filter, :return_result => true) do |entry|
entry.each do |attr,values|
   puts "DN: #{entry.dn}"

CodePudding user response:

Net::LDAP::Filter#eq should be what your looking for

require 'net/ldap'

host = 'ldap.google.com'
port = 636
auth = {
  :method => :simple, # or :simple_tls ? (I don't know Google's LDAP)
  :username => 'uid=username,dc=xxx,dc=xxx',
  :password => 'password'
}

ldap = Net::LDAP.new( :host => host, :port => port, :auth => auth)

base = 'dc=pangeare,dc=com'
filter = Net::LDAP::Filter.eq('memberOf','cn=pcore_readonly,ou=Groups,dc=pangeare,dc=com')

ldap.search(:base => base, :filter => filter) do |entry|
  p entry
end

Or you can just use Net::LDAP::Filter#construct:

filter = Net::LDAP::Filter.construct('(memberOf=cn=pcore_readonly,ou=Groups,dc=pangeare,dc=com)')
  •  Tags:  
  • ruby
  • Related