Ruby on rails 使用net/LDAP登录Rails LDAP

Ruby on rails 使用net/LDAP登录Rails LDAP,ruby-on-rails,authentication,ldap,Ruby On Rails,Authentication,Ldap,我正在尝试让LDAP身份验证在Rails下工作。 我选择了net/ldap,因为它是一个原生Ruby ldap库 我已经尝试了所有可能的东西,特别是例子,但仍然无法得到它的工作。 有什么想法吗?我正在为Rails 3开发一个插件,该插件使用LDAP进行身份验证,您可以查看源代码以获得一些想法,它目前使用的是net LDAP 0.1.1: 到LDAP服务器的实际连接和身份验证在以下位置完成: 最后,您可以查看我用来运行测试的LDAP服务器配置和Rails 3应用程序示例: 应用程序: 服务器:

我正在尝试让LDAP身份验证在Rails下工作。 我选择了net/ldap,因为它是一个原生Ruby ldap库

我已经尝试了所有可能的东西,特别是例子,但仍然无法得到它的工作。 有什么想法吗?

我正在为Rails 3开发一个插件,该插件使用LDAP进行身份验证,您可以查看源代码以获得一些想法,它目前使用的是net LDAP 0.1.1:

到LDAP服务器的实际连接和身份验证在以下位置完成:

最后,您可以查看我用来运行测试的LDAP服务器配置和Rails 3应用程序示例:

应用程序:


服务器:

我设法找到的最佳解决方案是具有以下功能的型号:

require 'net/ldap'

class User < ActiveRecord::Base

  def after_initialize
    @config = YAML.load(ERB.new(File.read("#{Rails.root}/config/ldap.yml")).result)[Rails.env]
  end

  def ldap_auth(user, pass)
    ldap = initialize_ldap_con
    result = ldap.bind_as(
      :base => @config['base_dn'],
      :filter => "(#{@config['attributes']['id']}=#{user})",
      :password => pass
    )
    if result
      # fetch user DN
      get_user_dn user
      sync_ldap_with_db user
    end
    nil
  end

  private
  def initialize_ldap_con
    options = { :host => @config['host'],
                :port => @config['port'],
                :encryption => (@config['tls'] ? :simple_tls : nil),
                :auth => { 
                  :method => :simple,
                  :username => @config['ldap_user'],
                  :password => @config['ldap_password']
                }
              }
    Net::LDAP.new options
  end

  def get_user_dn(user)
    ldap = initialize_ldap_con
    login_filter = Net::LDAP::Filter.eq @config['attributes']['id'], "#{user}"
    object_filter = Net::LDAP::Filter.eq "objectClass", "*" 

    ldap.search :base => @config['base_dn'],
                :filter => object_filter & login_filter,
                :attributes => ['dn', @config['attributes']['first_name'], @config['attributes']['last_name'], @config['attributes']['mail']] do |entry|
      logger.debug "DN: #{entry.dn}"
      entry.each do |attr, values|
        values.each do |value|
          logger.debug "#{attr} = #{value}"
        end
      end
    end
  end
end
需要“net/ldap”
类用户@config['base\u dn'],
:filter=>“(#{@config['attributes']['id']}=#{user})”,
:password=>pass
)
如果结果
#获取用户DN
获取\u用户\u dn用户
与数据库用户同步\u ldap\u
结束
无
结束
私有的
def初始化\u ldap\u con
选项={:host=>@config['host'],
:port=>@config['port'],
:encryption=>(@config['tls']?:simple_tls:nil),
:auth=>{
:method=>:简单,
:username=>@config['ldap\u user'],
:password=>@config['ldap\u password']
}
}
Net::LDAP.new选项
结束
def get_user_dn(用户)
ldap=初始化\u ldap\u con
login_filter=Net::LDAP::filter.eq@config['attributes']['id'],“#{user}”
object_filter=Net::LDAP::filter.eq“objectClass”,“*”
ldap.search:base=>@config['base\u dn'],
:filter=>object\u filter和login\u filter,
:attributes=>['dn',@config['attributes']['first_name'],@config['attributes']['last_name'],@config['attributes']['mail']]do |条目|
logger.debug“DN:#{entry.DN}”
entry.each do|attr,值|
值。每个do |值|
logger.debug“#{attr}=#{value}”
结束
结束
结束
结束
结束

第一个想法:粘贴一些代码:-),而且代码的功能也很好。是否将ldap与db同步?