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