class ActiveLdap::Adapter::JndiConnection

Constants

BasicAttributes
CommunicationException
Context
Control
HashTable
InitialDirContext
InitialLdapContext
ModificationItem
NameNotFoundException
NamingException
PagedResultsControl
PagedResultsResponseControl
SearchControls
ServiceUnavailableException
StartTlsRequest

Public Class Methods

new(host, port, method, timeout, follow_referrals) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 78
def initialize(host, port, method, timeout, follow_referrals)
  @host = host
  @port = port
  @method = method
  @timeout = timeout
  @context = nil
  @tls = nil
  @follow_referrals = follow_referrals
end

Public Instance Methods

add(dn, records) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 166
def add(dn, records)
  attributes = BasicAttributes.new
  records.each do |record|
    attributes.put(record.to_java_attribute)
  end
  @context.set_request_controls([])
  @context.create_subcontext(escape_dn(dn), attributes)
end
bind_as_anonymous() click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 109
def bind_as_anonymous
  setup_context(nil, nil, "none")
  bound?
end
bound?() click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 95
def bound?
  not @context.nil?
end
delete(dn) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 193
def delete(dn)
  escaped_dn = escape_dn(dn)
  @context.set_request_controls([])
  @context.destroy_subcontext(escaped_dn)
end
modify(dn, records) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 175
def modify(dn, records)
  items = records.collect(&:to_java_modification_item)
  @context.set_request_controls([])
  @context.modify_attributes(escape_dn(dn), items.to_java(ModificationItem))
end
modify_rdn(dn, new_rdn, delete_old_rdn) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 181
def modify_rdn(dn, new_rdn, delete_old_rdn)
  # should use mutex
  delete_rdn_key = "java.naming.ldap.deleteRDN"
  @context.set_request_controls([])
  begin
    @context.add_to_environment(delete_rdn_key, delete_old_rdn.to_s)
    @context.rename(escape_dn(dn), escape_dn(new_rdn))
  ensure
    @context.remove_from_environment(delete_rdn_key)
  end
end
sasl_bind(bind_dn, mechanism, quiet) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 99
def sasl_bind(bind_dn, mechanism, quiet)
  setup_context(bind_dn, password, mechanism)
  bound?
end
simple_bind(bind_dn, password) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 104
def simple_bind(bind_dn, password)
  setup_context(bind_dn, password, "simple")
  bound?
end
unbind() click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 88
def unbind
  @tls.close if @tls
  @tls = nil
  @context.close if @context
  @context = nil
end

Private Instance Methods

build_paged_results_control(page_size, page_cookie=nil) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 238
def build_paged_results_control(page_size, page_cookie=nil)
  PagedResultsControl.new(page_size, page_cookie, Control::CRITICAL)
end
build_raw_search_result(search_result) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 242
def build_raw_search_result(search_result)
  attributes = {}
  search_result.attributes.get_all.each do |attribute|
    attributes[attribute.get_id] = attribute.get_all.collect do |value|
      value.is_a?(String) ? value : String.from_java_bytes(value)
    end
  end
  [search_result.name_in_namespace, attributes]
end
escape_dn(dn) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 232
def escape_dn(dn)
  javax.naming.ldap.LdapName.new(dn)
rescue Java::JavaLang::IllegalArgumentException, Java::JavaxNaming::InvalidNameException
  dn
end
ldap_uri() click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 227
def ldap_uri
  protocol = @method == :ssl ? "ldaps" : "ldap"
  "#{protocol}://#{@host}:#{@port}/"
end
setup_context(bind_dn, password, authentication) click to toggle source
# File lib/active_ldap/adapter/jndi_connection.rb, line 200
def setup_context(bind_dn, password, authentication)
  unbind
  environment = {
    Context::INITIAL_CONTEXT_FACTORY => "com.sun.jndi.ldap.LdapCtxFactory",
    Context::PROVIDER_URL => ldap_uri,
    'com.sun.jndi.ldap.connect.timeout' => (@timeout * 1000).to_i.to_s,
    'com.sun.jndi.ldap.read.timeout' => (@timeout * 1000).to_i.to_s,
    'java.naming.ldap.derefAliases' => 'never',
    'java.naming.referral' => @follow_referrals ? 'follow' : 'ignore',
  }
  context = InitialLdapContext.new(HashTable.new(environment), nil)
  if @method == :start_tls
    @tls = context.extended_operation(StartTlsRequest.new)
    @tls.negotiate
  end
  context.add_to_environment(Context::SECURITY_AUTHENTICATION,
                             authentication)
  if bind_dn
    context.add_to_environment(Context::SECURITY_PRINCIPAL, bind_dn)
  end
  if password
    context.add_to_environment(Context::SECURITY_CREDENTIALS, password)
  end
  context.reconnect(nil)
  @context = context
end