• Rails 肯定要知道相关密码 (也许是加密后的),然后传给 LDAP 作认证,通过了 rails 就可以认为 OK 了,创建好 session 的登录信息就可以了。 可以参考以下的 model:( 来自 stackoverflow )

    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
    
  • 一般来说是系统变量没有设置导致的。

  • 用 ruby-ldap 这个 gem

    然后,大概的代码如下,然后就跟密码认证差不多了:

    require 'ldap'
    
    conn = LDAP::Conn.new( '<domain-server>', 389 )
    conn.set_option( LDAP::LDAP_OPT_PROTOCOL_VERSION, 3 )
    conn.bind( '<domain>\<username>', '<password>' ) do |conn|
    
    base = 'ou=Users,ou=<container>,dc=<domain>,dc=local'
    
    results = conn.search2(base, LDAP::LDAP_SCOPE_SUBTREE, '(cn=*)')
    results.each { |entry| puts "#{entry['dn']}: #{entry['telephoneNumber']}" }
    
    end
    

    或者用这个 gem: net-ldap 参考: http://stackoverflow.com/questions/3539501/rails-ldap-login-using-net-ldap

  • Ruby 用在哪里? at February 12, 2012

    做自动化测试开发,华为,中兴,腾讯,百度都有。

  • thx,试试看

  • 致社区的两点建议 at February 10, 2012

    定期推送一些讨论贴子是可以的~

  • @cqpx 多谢你的解释。embeds_many 看来的场合更适合于解决关系数据库繁于处理的多层关联的问题。但又不是经常反复查询的数据. 不过,我想这正是 mongodb 对传统数据库的一个大的特点之一吧. 刚才的问题你的一些代码指引,我分析了觉得是一个 bug, 内部没有递归做 save 判定。给他们 mongoid 提一个 issues 先。

  • @Rei 不知道有人有这种用法没?我现在不太习惯 has_many, belongs_to 了,用文档设计模式的话这种应该较为常见吧。

  • @huacnlee 多谢。了解~

  • ri 跟 rdoc 在本地是用处太小了。没有智能解析. 实际上,安装了 rdoc 时,在本地使用 gem sever 可以在http://localhost:8808gem的api用浏览器查看相关,不过还是有些弱的。@fsword

  • 看看是不是可以直接用 rss

  • bug: 连按几次就黑了显示不了了。浏览器:chrome16

  • rubygems.org 惊现 500 Error at January 02, 2012

    恢复了不过非常慢。

  • 网站证书需要签名网址,自我颁发的话可以随便填写。反正浏览器会警告