1

我如何制作这个多维的?每个搜索结果条目都有多个属性:attributes => ['sAMAccountName','givenName','SN','mail']。结果中可能有很多条目。此代码适用于仅创建一个具有多个属性的条目。

  def self.Find(attribute, loginID)
        conn = Net::LDAP.new :host => SERVER,
                             :port => PORT,
                             :base => BASE,
                             :auth => { 
                                         :username => 'admin',
                                         :password => 'admin',
                                         :method => :simple
                                      }
        if  conn.bind
          result = HashWithIndifferentAccess.new
          conn.search( :base => LDAPBASE,
                       :filter => Net::LDAP::Filter.eq( attribute, loginID+"*" ),
                       :attributes => ['sAMAccountName','givenName','SN','mail'],
                       :return_result => true
                      ).each do |entries|
                        entries.each do |attribute, values|
                          values.each do |value|
                            result[attribute] = value
                          end
                        end
                      end
          result
        end
  end

我从 ldap 得到的实际响应是这样的——

puts result.to_s

{
 "dn"=>"CN=somename\\, somefirstname,OU=Users,DC=site,DC=com", 
 "sn"=>"somename", 
 "givenname"=>"somefirstname",
 "samaccountname"=>"someuserid", 
 "mail"=>"someone@somthing.com"
}

由于搜索是有 * 通配符来查找所有匹配的条目。Ldap 将以上述格式返回多个条目。与此类似 -

    {
      "dn"=>"CN=somename\\, somefirstname1,OU=Users,DC=site,DC=com", 
      "sn"=>"somename1", 
      "givenname"=>"somefirstname1",
      "samaccountname"=>"someuserid1", 
      "mail"=>"someone1@somthing.com"
    },
    {
      "dn"=>"CN=somename\\, somefirstname2,OU=Users,DC=site,DC=com", 
      "sn"=>"somename2", 
      "givenname"=>"somefirstname2", 
      "samaccountname"=>"someuserid2",
      "mail"=>"someone2@somthing.com"
    },
    {
      "dn"=>"CN=somename\\, somefirstname3,OU=Users,DC=site,DC=com",
      "sn"=>"somename3",
      "givenname"=>"somefirstname3",
      "samaccountname"=>"someuserid3", 
      "mail"=>"someone3@somthing.com"
    },


A[a=>1,b=>11,c=>111]
B[a=>2,b=>22,c=>222]
C[a=>3,b=>33,c=>333]
D[a=>4,b=>44,c=>444]
4

1 回答 1

0

我不确定我是否理解了这个问题。

def self.Find(attribute, loginID)
        conn = Net::LDAP.new :host => SERVER,
                             :port => PORT,
                             :base => BASE,
                             :auth => { 
                                         :username => 'admin',
                                         :password => 'admin',
                                         :method => :simple
                                      }
        if  conn.bind
          conn.search( :base => LDAPBASE,
                       :filter => Net::LDAP::Filter.eq( attribute, loginID+"*" ),
                       :attributes => ['sAMAccountName','givenName','SN','mail'],
                       :return_result => true
                      ).reduce(Array.new) do |acc, el|

                         #
                         # Any modification of the entry must be here
                         #

                         acc + [el] 

                      end
          end
        end
end

我觉得Array.map也是不错的选择

我的假设:

  • conn.search 返回哈希数组
  • 您想要的结果形式:

    [
          {"uid":"123","displayName":"User 123","mail":"123@example.com"},
          {"uid":"456","displayName":"User 456","mail":"456@example.com"},
          {"uid":"789","displayName":"User 789","mail":"789@example.com"}
    ]
    
于 2012-05-01T06:59:57.160 回答