2014-01-22 4 views
1

devise를 사용하여 LDAP 연결을 확인하는 기존 레일스 응용 프로그램을 수정하려고합니다. 여러 LDAP 연결에 대해 확인해야합니다. 기본적으로 내 사용자 기반 2 또는 3 다른 활성 디렉터리 사이에 분할됩니다 및 연결 정보 개체 배열을 제공하고 응답을 가져 오거나 실패 할 때까지 연결을 통해 실행할 수 싶습니다. 이것이 가능한가?Devise로 여러 LDAP 연결

답변

3

입니다. 거의. 나는 최근에 해결책을 함께 해킹했는데, 그것이 지금 당신에게 많은 도움이 될지 확실하지 않습니다.

먼저 devise_ldap_authenticatable을 사용해야합니다. 이 파일을 설치하고 나면이 젬의 connection.rb 파일에서 initialize 메소드를 업데이트하여 하나의 구성 또는 여러 구성을 허용 할 수 있습니다.

def initialize(params = {}) 
    ldap_configs = YAML.load(ERB.new(File.read(::Devise.ldap_config || "#{Rails.root}/config/ldap.yml")).result)[Rails.env] 
    ldap_configs = ldap_configs.is_a?(Hash) ? [ldap_configs] : ldap_configs 

다음 부분은 귀하에게 달려 있습니다. 내 제약 조건 (두 디렉토리에있는 사용자 이름)으로 인해 연결을 반복하기 전에 유효한 도메인을 입력해야했습니다. 이 제약 조건이 없을 수도 있습니다. 두 경우 모두 configs를 반복하면됩니다. 성공적으로 바인드하면 루프를 해제하십시오. 는 config 값이 여기에 초기화됩니다 @ldap 변수에 저장됩니다 -

ldap_configs.each do |ldap_config| 
    #Maybe not needed if you don't have usernames in each directory. @domain is a user-entered value  
    if @domain == ldap_config["domain"] 
     #This should all stay the same, until you check the bindings 

      if @ldap.bind 
      #If it binds, break the loop. the values in @ldap will be stored 
      break 
      else 
      #keep looping 
      end 
    end 
end 

다음, 필요한 경우 도메인을 포함하여 연결의 모든 구성되어 생성 devise_ldap_authenticatable 확인 ldap.yml 파일을 -

## Environment 

development: 
- 
    host: "localhost1.com" 
    port: "389" 
    attribute: uid 
    base: dc=my-domain,dc=com 
    admin_user: cn=admin,dc=my-domain,dc=com 
    admin_password: admin_password 
    ssl: false 
    domain: "FIRST" 
- 
    host: "localhost2.com" 
    port: "389" 
    attribute: uid 
    base: dc=my-domain,dc=com 
    admin_user: cn=admin,dc=my-domain,dc=com 
    admin_password: admin_password 
    ssl: false 
    domain: "SECOND" 
+0

그것이 내가 여러 도메인에 대한 하나 개의 호스트 (실행 글로벌 카탈로그) 여러 기본 CONFIGS이있는 경우이 솔루션을 단순화 할 수 있습니다 : 여기

module Devise module LDAP module ConnectionExtensions def initialize(params = {}) super ldap_config = YAML.load(File.read("#{Rails.root}/config/ldap.yml"))[Rails.env] ldap_config["hosts"]&.each do |host| begin @ldap.host = host break if @ldap.bind rescue Net::LDAP::Error => e DeviseLdapAuthenticatable::Logger.send(e) next end end end end class Connection prepend ConnectionExtensions end end end 

그리고

샘플 YAML 파일입니다? –

0

나는 다음과 같이 Steve의 답을 바탕으로 잘 만들어진 것으로 보입니다. 이점은 원래 코드를 래핑하고 기능을 추가한다는 것입니다. ldap.yml 파일을 동일하게 유지하고 YAML에 호스트 배열이있는 hosts 키를 추가하여이를 연습 할 수 있습니다.

루프에서 연결 오류를 해결합니다. 라이브러리가 이미 시도하려고하는 연결을 다시 시도 할 때 여전히 버려집니다.

development: 
    host: localhost1.com 
    hosts: 
    - localhost1.com 
    - localhost2.com 
    port: 389 
    attribute: uid 
    base: dc=my-domain,dc=com 
    admin_user: cn=admin,dc=my-domain,dc=com 
    admin_password: admin_password 
    ssl: false