Class: ActiveLdap::Adapter::JndiConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_ldap/adapter/jndi_connection.rb

Defined Under Namespace

Modules: Scope Classes: ModifyRecord

Constant Summary collapse

HashTable =
java.util.Hashtable
InitialDirContext =
directory.InitialDirContext
InitialLdapContext =
ldap.InitialLdapContext
SearchControls =
directory.SearchControls
ModificationItem =
directory.ModificationItem
BasicAttributes =
directory.BasicAttributes
Context =
naming.Context
StartTlsRequest =
ldap.StartTlsRequest
Control =
ldap.Control
PagedResultsControl =
ldap.PagedResultsControl
PagedResultsResponseControl =
ldap.PagedResultsResponseControl
CommunicationException =
naming.CommunicationException
ServiceUnavailableException =
naming.ServiceUnavailableException
NamingException =
naming.NamingException
NameNotFoundException =
naming.NameNotFoundException

Instance Method Summary collapse

Constructor Details

#initialize(host, port, method, timeout, follow_referrals) ⇒ JndiConnection

Returns a new instance of JndiConnection.



78
79
80
81
82
83
84
85
86
# 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

Instance Method Details

#add(dn, records) ⇒ Object



166
167
168
169
170
171
172
173
# 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_anonymousObject



109
110
111
112
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 109

def bind_as_anonymous
  setup_context(nil, nil, "none")
  bound?
end

#bound?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 95

def bound?
  not @context.nil?
end

#delete(dn) ⇒ Object



193
194
195
196
197
# 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) ⇒ Object



175
176
177
178
179
# 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) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
# 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) ⇒ Object



99
100
101
102
# 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

#search(options) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 114

def search(options)
  base = options[:base]
  scope = options[:scope]
  filter = options[:filter]
  attributes = options[:attributes]
  limit = options[:limit]
  use_paged_results = options[:use_paged_results]
  page_size = options[:page_size]

  controls = SearchControls.new
  controls.search_scope = scope

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

  page_cookie = nil
  if use_paged_results
    # https://devdocs.io/openjdk~8/javax/naming/ldap/pagedresultscontrol
    @context.set_request_controls([build_paged_results_control(page_size)])
  else
    @context.set_request_controls([])
  end

  escaped_base = escape_dn(base)

  loop do
    @context.search(escaped_base, filter, controls).each do |search_result|
      yield(build_raw_search_result(search_result))
    end

    break unless use_paged_results

    # Find the paged search cookie
    response_controls = @context.get_response_controls
    break unless response_controls
    response_controls.each do |response_control|
      next unless response_control.is_a?(PagedResultsResponseControl)
      page_cookie = response_control.get_cookie
      break
    end

    break unless page_cookie

    # Set paged results control so we can keep getting results.
    paged_results_control =
      build_paged_results_control(page_size, page_cookie)
    @context.set_request_controls([paged_results_control])
  end
end

#simple_bind(bind_dn, password) ⇒ Object



104
105
106
107
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 104

def simple_bind(bind_dn, password)
  setup_context(bind_dn, password, "simple")
  bound?
end

#unbindObject



88
89
90
91
92
93
# 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