Loading users from Active Directory into the Active Record Rails 3.1 database

Update 11/30/11 I made some changes to the code snippet where I found errors. Now I successfully authenticate myself, but I get this error after trying to call ldap.search:

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

Using Rails 3.1.0 and ruby ​​1.9.2 on Windows Server 2008 R2

Original post I am new to Ruby, rails and programming. I have an application that needs to authenticate with our Active Directory server, maintaining a list of users that are separable from AD.

I try to use net-ldap to establish a connection, search for AD, and load users, but I get 0 results every time I try to start.

I put it together on the samples I saw, but when I set it up for my company, it doesn't seem to work. Any ideas / criticisms are welcome.

thanks!

I installed this as a method in my model of the User class:

 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 
+4
source share
1 answer

It turned out that the error I am getting is related to some of the attributes that I am looking for that do not exist for all users under the tree that I am looking for.

Thank you for looking at this, but I believe that I can move on to deciding how to process records without these attributes.

0
source

Source: https://habr.com/ru/post/1383770/


All Articles