简体   繁体   English

将用户从Active Directory加载到Rails 3.1 Active Record数据库中

[英]Loading users from Active Directory into a Rails 3.1 Active Record database

Update 11/30/11 I made some changes in the code snippet where I found errors. 更新11/30/11我在发现错误的代码段中做了一些更改。 I am now successfully authenticating for sure, but am getting this error after attempting the ldap.search call: 我现在成功进行了身份验证,但在尝试ldap.search调用后出现此错误:

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

Using Rails 3.1.0 and ruby 1.9.2 on Windows Server 2008 R2 在Windows Server 2008 R2上使用Rails 3.1.0和ruby 1.9.2

Original Message I'm brand new to Ruby, rails and programming. 原始消息我是Ruby,rails和编程的新手。 I have an application that will have to authenticate to our Active Directory server while maintaining a list of users separate from AD. 我有一个应用程序,必须对我们的Active Directory服务器进行身份验证,同时保持与AD分开的用户列表。

I'm attempting to use net-ldap to establish the connection, search AD and load the users, but I get 0 results with each attempt to run. 我正在尝试使用net-ldap建立连接,搜索AD并加载用户,但每次尝试运行时我得到0结果。

I've put this together based on samples I've seen, but when I customize it to my company, it doesn't seem to work. 我根据我见过的样品将它们放在一起,但是当我将它定制到我的公司时,它似乎不起作用。 Any ideas/critiques are most welcome. 任何想法/批评都是最受欢迎的。

thanks! 谢谢!

I've set this as a method in my User class model: 我已将此设置为我的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 => "username@mycompany.com", :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

It turns out that the error I'm getting is due to some of the attributes I'm searching for not existing on all the users under the tree I'm looking at. 事实证明,我得到的错误是由于我正在搜索的一些属性不存在于我正在查看的树下的所有用户身上。

Thanks to any that looked at this, but I believe I can move on to resolving how to handle entries without those attributes. 感谢任何看过这个的人,但我相信我可以继续解决如何处理没有这些属性的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM