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) ⇒ JndiConnection

Returns a new instance of JndiConnection.



78
79
80
81
82
83
84
85
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 78

def initialize(host, port, method, timeout)
  @host = host
  @port = port
  @method = method
  @timeout = timeout
  @context = nil
  @tls = nil
end

Instance Method Details

#add(dn, records) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 164

def add(dn, records)
  attributes = BasicAttributes.new
  records.each do |record|
    attributes.put(record.to_java_attribute)
  end
  escaped_dn = escape_dn(dn)
  @context.set_request_controls([])
  @context.create_subcontext(escaped_dn, attributes)
end

#bind_as_anonymousObject



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

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

#bound?Boolean

Returns:

  • (Boolean)


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

def bound?
  not @context.nil?
end

#delete(dn) ⇒ Object



194
195
196
197
198
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 194

def delete(dn)
  escaped_dn = escape_dn(dn)
  @context.set_request_controls([])
  @context.destroy_subcontext(escaped_dn)
end

#modify(dn, records) ⇒ Object



174
175
176
177
178
179
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 174

def modify(dn, records)
  escaped_dn = escape_dn(dn)
  items = records.collect(&:to_java_modification_item)
  @context.set_request_controls([])
  @context.modify_attributes(escaped_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
192
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 181

def modify_rdn(dn, new_rdn, delete_old_rdn)
  escaped_dn = escape_dn(dn)
  # 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(escaped_dn, new_rdn)
  ensure
    @context.remove_from_environment(delete_rdn_key)
  end
end

#sasl_bind(bind_dn, mechanism, quiet) ⇒ Object



98
99
100
101
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 98

def sasl_bind(bind_dn, mechanism, quiet)
  setup_context(bind_dn, password, mechanism)
  bound?
end

#search(options) ⇒ Object



113
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
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 113

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



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

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

#unbindObject



87
88
89
90
91
92
# File 'lib/active_ldap/adapter/jndi_connection.rb', line 87

def unbind
  @tls.close if @tls
  @tls = nil
  @context.close if @context
  @context = nil
end