Class | ActiveLdap::Adapter::JndiConnection |
In: |
lib/active_ldap/adapter/jndi_connection.rb
|
Parent: | Object |
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 |
NamingException | = | naming.NamingException |
NameNotFoundException | = | naming.NameNotFoundException |
# File lib/active_ldap/adapter/jndi_connection.rb, line 74 74: def initialize(host, port, method) 75: @host = host 76: @port = port 77: @method = method 78: @context = nil 79: @tls = nil 80: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 130 130: def add(dn, records) 131: attributes = BasicAttributes.new 132: records.each do |record| 133: attributes.put(record.to_java_attribute) 134: end 135: @context.create_subcontext(dn, attributes) 136: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 103 103: def bind_as_anonymous 104: setup_context(nil, nil, "none") 105: bound? 106: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 89 89: def bound? 90: not @context.nil? 91: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 152 152: def delete(dn) 153: @context.destroy_subcontext(dn) 154: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 138 138: def modify(dn, records) 139: items = records.collect(&:to_java_modification_item) 140: @context.modify_attributes(dn, items.to_java(ModificationItem)) 141: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 143 143: def modify_rdn(dn, new_rdn, delete_old_rdn) 144: # should use mutex 145: delete_rdn_key = "java.naming.ldap.deleteRDN" 146: @context.add_to_environment(delete_rdn_key, delete_old_rdn.to_s) 147: @context.rename(dn, new_rdn) 148: ensure 149: @context.remove_from_environment(delete_rdn_key) 150: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 93 93: def sasl_bind(bind_dn, mechanism, quiet) 94: setup_context(bind_dn, password, mechanism) 95: bound? 96: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 108 108: def search(base, scope, filter, attrs, limit, callback, &block) 109: controls = SearchControls.new 110: controls.search_scope = scope 111: 112: unless attrs.blank? 113: controls.returning_attributes = attrs.to_java(:string) 114: end 115: 116: i = 0 117: @context.search(base, filter, controls).each do |result| 118: i += 1 119: attributes = {} 120: result.attributes.get_all.each do |attribute| 121: attributes[attribute.get_id] = attribute.get_all.collect do |value| 122: value.is_a?(String) ? value : String.from_java_bytes(value) 123: end 124: end 125: callback.call([result.name_in_namespace, attributes], block) 126: break if limit and limit <= i 127: end 128: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 98 98: def simple_bind(bind_dn, password) 99: setup_context(bind_dn, password, "simple") 100: bound? 101: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 82 82: def unbind 83: @tls.close if @tls 84: @tls = nil 85: @context.close if @context 86: @context = nil 87: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 181 181: def ldap_uri 182: protocol = @method == :ssl ? "ldaps" : "ldap" 183: "#{protocol}://#{@host}:#{@port}/" 184: end
# File lib/active_ldap/adapter/jndi_connection.rb, line 157 157: def setup_context(bind_dn, password, authentication) 158: unbind 159: environment = { 160: Context::INITIAL_CONTEXT_FACTORY => "com.sun.jndi.ldap.LdapCtxFactory", 161: Context::PROVIDER_URL => ldap_uri, 162: } 163: environment = HashTable.new(environment) 164: context = InitialLdapContext.new(environment, nil) 165: if @method == :start_tls 166: @tls = context.extended_operation(StartTlsRequest.new) 167: @tls.negotiate 168: end 169: context.add_to_environment(Context::SECURITY_AUTHENTICATION, 170: authentication) 171: if bind_dn 172: context.add_to_environment(Context::SECURITY_PRINCIPAL, bind_dn) 173: end 174: if password 175: context.add_to_environment(Context::SECURITY_CREDENTIALS, password) 176: end 177: context.reconnect(nil) 178: @context = context 179: end