2011-11-29 190 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

事实证明,我得到的错误是由于一些我在寻找不存在的属性我正在查看的树下的所有用户。

感谢任何看着这个,但我相信我可以继续解决如何处理没有这些属性的条目。