# class ActiveLdap::Adapter::JndiConnection

### Constants

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

### Public Class Methods

new(host, port, method, timeout) click to toggle source

    # File lib/active_ldap/adapter/jndi_connection.rb, line 76
    def initialize(host, port, method, timeout)
      @host = host
      @port = port
      @method = method
      @timeout = timeout
      @context = nil
      @tls = nil
    end

### Public Instance Methods

add(dn, records) click to toggle source

    # File lib/active_ldap/adapter/jndi_connection.rb, line 131
    def add(dn, records)
      attributes = BasicAttributes.new
      records.each do |record|
        attributes.put(record.to_java_attribute)
      end
      @context.create_subcontext(dn, attributes)
    end

bind_as_anonymous() click to toggle source

    # File lib/active_ldap/adapter/jndi_connection.rb, line 106
    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 92
    def bound?
      not @context.nil?
    end

delete(dn) click to toggle source

    # File lib/active_ldap/adapter/jndi_connection.rb, line 153
    def delete(dn)
      @context.destroy_subcontext(dn)
    end

modify(dn, records) click to toggle source

    # File lib/active_ldap/adapter/jndi_connection.rb, line 139
    def modify(dn, records)
      items = records.collect(&:to_java_modification_item)
      @context.modify_attributes(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 144
    def modify_rdn(dn, new_rdn, delete_old_rdn)
      # should use mutex
      delete_rdn_key = "java.naming.ldap.deleteRDN"
      @context.add_to_environment(delete_rdn_key, delete_old_rdn.to_s)
      @context.rename(dn, new_rdn)
    ensure
      @context.remove_from_environment(delete_rdn_key)
    end

sasl_bind(bind_dn, mechanism, quiet) click to toggle source

    # File lib/active_ldap/adapter/jndi_connection.rb, line 96
    def sasl_bind(bind_dn, mechanism, quiet)
      setup_context(bind_dn, password, mechanism)
      bound?
    end

search(base, scope, filter, attrs, limit) { |name_in_namespace, attributes| ... } click to toggle source

    # File lib/active_ldap/adapter/jndi_connection.rb, line 111
    def search(base, scope, filter, attrs, limit)
      controls = SearchControls.new
      controls.search_scope = scope

      controls.count_limit = limit if limit
      unless attrs.blank?
        controls.returning_attributes = attrs.to_java(:string)
      end

      @context.search(base, filter, controls).each do |result|
        attributes = {}
        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
        yield([result.name_in_namespace, attributes])
      end
    end

simple_bind(bind_dn, password) click to toggle source

    # File lib/active_ldap/adapter/jndi_connection.rb, line 101
    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 85
    def unbind
      @tls.close if @tls
      @tls = nil
      @context.close if @context
      @context = nil
    end

### Private Instance Methods

ldap_uri() click to toggle source

    # File lib/active_ldap/adapter/jndi_connection.rb, line 184
    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 158
    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,
      }
      environment = HashTable.new(environment)
      context = InitialLdapContext.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