2

PHP/MySQL で作成された古いアプリを書き直そうとしています。使用される認証システムには、データベースにユーザー名、電子メールなどを格納するユーザー テーブルがありますが、パスワードは格納されません。

ユーザーがログインするたびに、最初にデータベースをチェックして、ユーザーが存在するかどうかを確認し、存在しない場合はログインエラーを返します。ユーザーがローカル データベースに存在する場合、ユーザーが入力したユーザー名とパスワードの組み合わせを使用して Active Directory へのバインドを試み、成功した場合はセッションを作成します。

Railsを使用してこれを達成する最良の方法は何ですか?

4

2 に答える 2

3

Ruby の Net::LDAP ライブラリはかなり優れています。

これは、私が何年も使用してきたものの簡略化されたバージョンです。

# sessions_controller.rb
def create
  user = User.find_by_login(params[:login])
  if user && Ldap.authenticate(params[:login], params[:password])
    self.current_user = user
    Rails.logger.info "Logged in #{user.name}"
    flash[:notice] = "Successfully Logged In!"
    redirect_back_or_default root_url
  else
    flash[:alert] = "Invalid User credentials"
    render :new
  end
end

# lib/ldap.rb
# Ldap.authenticate('user','password')
# Returns true if validated
# Returns false if invalidated
# Returns nil if LDAP unavailable
require 'net/ldap'
class Ldap
  def self.config
    # this is actually loaded from a yaml config file
    {
      :domain => 'YOURDOMAIN',
      :host   => '10.10.10.100'
    }
  end

  def self.authenticate(login, password)
    conn = Net::LDAP.new(
      :host       => config[:host],
      :port       => 636,
      :base       => "dc=#{config[:domain]}, dc=local",
      :encryption => :simple_tls,
      :auth       => {
        :username => "#{login}@#{config[:domain]}.local",
        :password => password,
        :method   => :simple
      }
    )
    Timeout::timeout(15) do
      return conn.bind ? true : false
    end
  rescue Net::LDAP::LdapError => e
    notify_ldap_admin(config[:host],'Error',e)
    nil
  rescue Timeout::Error => e
    notify_ldap_admin(config[:host],'Timeout',e)
    nil
  end

  def self.notify_ldap_admin(host,error_type,error)
    msg = "LDAP #{error_type} on #{host}"
    RAILS_DEFAULT_LOGGER.debug(msg)
    DeveloperMailer.deliver_ldap_failure_msg(msg,error)
  end
end
于 2012-11-13T21:51:33.880 に答える
0

deviseおよびdevise_ldap_authenticatableライブラリを確認してください。

于 2012-11-13T20:44:22.163 に答える