ruby 通过空字段的 ldap 搜索抛出异常

ruby throws exception by ldap search by empty field

我尝试从 LDAP(内部 AD)获取一些信息。一切正常,除非我想从空白字段中获取数据。

require 'net/ldap'
...

def getDatafromAD(ad_hostname,ad_dn,ad_password,username)
  ldap = Net::LDAP.new :host => ad_hostname, :port => 389, :auth => {
          :method => :simple,
          :username => ad_dn,
          :password => ad_password }

  filter = Net::LDAP::Filter.eq("cn", username)
  treebase = "ou=myOU,dc=example,dc=com"
  attrs = ["givenName", "mobile", "mail", "cn", "sn", "person"]

  ldap.search( :base => treebase, :filter => filter, :attributes => attrs ) do |entry|
         puts "UID: #{entry.dn}"
         puts "#{entry.cn.first}: Surename: #{entry.sn.first} Givenname: #{entry.givenName.first} Mail: #{entry.mail.first} Mobile: #{entry.mobile.first}"
  end

end

这 运行 很好,直到一个字段为空(在本例中为移动设备):

Traceback (most recent call last):
    16: from query_userdata.rb:79:in `<main>'
    15: from query_userdata.rb:79:in `each'
    14: from query_userdata.rb:81:in `block in <main>'
    13: from query_userdata.rb:58:in `getDatafromAD'
    12: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap.rb:782:in `search'
    11: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap/instrumentation.rb:19:in `instrument'
    10: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap.rb:783:in `block in search'
     9: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap.rb:1311:in `use_connection'
     8: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap.rb:784:in `block (2 levels) in search'
     7: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap/connection.rb:388:in `search'
     6: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap/instrumentation.rb:19:in `instrument'
     5: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap/connection.rb:399:in `block in search'
     4: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap/connection.rb:399:in `loop'
     3: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap/connection.rb:445:in `block (2 levels) in search'
     2: from /usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap.rb:786:in `block (3 levels) in search'
     1: from query_userdata.rb:62:in `block in getDatafromAD'
/usr/share/gems/gems/net-ldap-0.17.0/lib/net/ldap/entry.rb:185:in `method_missing': undefined method `mobile' for #<Net::LDAP::Entry:0x000055c0d912ac60> (NoMethodError)

谁能告诉我如何解决这个问题。我的 knowledge/XP 无法处理这个。 我尝试使用 .empty?() 提前检查,但看起来在访问空字段时异常就在这里。 如果一个字段为空,我想在输出中放置一个自定义字符串(“数据不存在”)。可能只是一个小开关,我找不到它...

非常感谢!

我 2 天前遇到过同样的问题!我可以推荐两件事:

  • 通过散列键访问 entry 属性。调用缺失的方法将引发错误,但尝试访问缺失键的值将 return 一个空数组
entry[:missing_key]
# => []

entry[:missing_key][0]
# => nil

entry.missing_key.first
# => *** NoMethodError Exception: undefined method `missing_key' for #<Net::LDAP::Entry:0x00005605a06bbf88>
  • 将潜在的 nil 值与字符串连接时使用 || 运算符。
puts "SN:     #{(entry[:sn][0]     || 'data not there')}" +
     "Mobile: #{(entry[:mobile][0] || 'data not there')}" +
     "Foo:    #{(entry[:foo][0]    || 'data not there')}"