2011-11-29 39 views
4

更新11/30/11 我在代碼段中發現了一些發現錯誤的更改。我現在成功認證肯定的,但在嘗試ldap.search電話後我得到這個錯誤:將用戶從Active Directory加載到Rails 3.1 Active Record數據庫中

<OpenStruct code = 1, message="Operations Error"> 

的Windows Server 2008 R2

原始郵件上使用Rails 3.1.0和1.9.2的紅寶石 我是Ruby,rails和編程的新手。我有一個應用程序必須對我們的Active Directory服務器進行身份驗證,同時保留與AD不同的用戶列表。

我正在嘗試使用net-ldap建立連接,搜索AD並加載用戶,但每次嘗試運行時都會得到0個結果。

我已經根據我見過的樣品將它放在一起,但是當我將它定製到我的公司時,它似乎不起作用。任何想法/評論都是非常受歡迎的。

謝謝!

我已經將這個選項設爲我的User類模型的方法:

class User < ActiveRecord::Base 
    attr_accessible :username, :name, :email, :team, :office, :points_attributes 
    validates_presence_of :username, :name, :email 
    validates_uniqueness_of :username, :email 
    has_one :points 
    accepts_nested_attributes_for :points 

    def self.import_all 
    # initialization stuff. set bind_dn, bind_pass, ldap_host, base_dn and filter 

    ldap = Net::LDAP.new(:host => "dc.mycompany.com", :port => 389) 
    if ldap.bind(:method => :simple, :username => "[email protected]", :password => "secret") 
    else 
    p ldap.get_operation_result 
    end 

    begin 
    # Build the list 
    filter = Net::LDAP::Filter.eq("displayName", "J*") 
    attrs = ["givenName", "sn", "physicalDeliveryOfficeName", "sAMAccountName"] 
    records = new_records = 0 
    ldap.search(:base => "DC=mycompany,DC=com", :attributes => attrs, :filter => filter, :return_result => false) do |entry| 
    name = entry.givenName.to_s.strip + " " + entry.sn.to_s.strip 
    username = entry.sAMAccountName.to_s.strip 
    email = entry.sAMAccountName.to_s.strip + "@mycompany.com" 
    office = entry.physicalDeliveryOfficeName.to_s.strip 
    user = User.find_or_initialize_by_username :name => name, :username => username, :email => email, :office => office 
    if user.new_record? 
     user.save 
     Points.find_or_create_by_user_id(user.id) 
     new_records = new_records + 1 
    else 
     user.touch 
    end 
    records = records + 1 
    end 
    p ldap.get_operation_result 

    logger.info("LDAP Import Complete: " + Time.now.to_s) 
    logger.info("Total Records Processed: " + records.to_s) 
    logger.info("New Records: " + new_records.to_s) 

    end 

    end 
end 
+0

您正在使用哪個版本的ruby-net-ldap? – Nick

+0

我使用的是gem net-ldap 0.2.2,它明顯不同於ruby-net-ldap。 – jgifford78

回答

0

事實證明,我得到的錯誤是由於一些我在尋找不存在的屬性我正在查看的樹下的所有用戶。

感謝任何看着這個,但我相信我可以繼續解決如何處理沒有這些屬性的條目。