Active DirectoryからRails 3.1 Active Recordデータベースにユーザーをロードする

StackOverflow https://stackoverflow.com/questions/8319414

質問

更新11/30/11 エラーが見つかったコードスニペットにいくつかの変更を加えました。私は確かに正常に認証されていますが、LDAP.Search Callを試みた後にこのエラーが発生しています。

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

Windows Server 2008 R2でRails 3.1.0とRuby 1.9.2の使用

オリジナルメッセージ私はRuby、Rails、プログラミングを真新しいです。 ADとは別のユーザーのリストを維持しながら、Active Directoryサーバーに認証する必要があるアプリケーションがあります。

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
役に立ちましたか?

解決

私が取得しているエラーは、私が見ているツリーのすべてのユーザーに存在しない属性のいくつかによるものであることがわかります。

これを見たものに感謝しますが、それらの属性なしでエントリを処理する方法を解決することに進むことができると思います。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top