将用户从Active Directory加载到Rails 3.1活动记录数据库中
-
25-10-2019 - |
题
更新11/30/11 我在发现错误的代码段上进行了一些更改。我现在可以肯定地成功验证,但是在尝试LDAP之后会遇到此错误。搜索呼叫:
<OpenStruct code = 1, message="Operations Error">
在Windows Server 2008 R2上使用Rails 3.1.0和Ruby 1.9.2
原始信息我是Ruby,Rails和编程的全新。我有一个应用程序,该应用程序必须对我们的Active Directory Server进行身份验证,同时维护与AD分开的用户列表。
我正在尝试使用Net-LDAP来建立连接,搜索广告并加载用户,但是每次尝试运行时,我都会获得0个结果。
我已经根据我看到的样本将其放在一起,但是当我将其自定义给公司时,它似乎不起作用。任何想法/批评都是最欢迎的。
谢谢!
我将其设置为用户类模型中的方法:
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
解决方案
事实证明,我遇到的错误是由于我正在搜索的某些属性在我所看的树下的所有用户上都不存在。
多亏了任何对此的看法,但我相信我可以继续解决如何无需这些属性处理条目。
不隶属于 StackOverflow