Class: ActiveLdap::Adapter::Base
- Inherits:
-
Object
- Object
- ActiveLdap::Adapter::Base
- Includes:
- GetTextSupport
- Defined in:
- lib/active_ldap/adapter/base.rb,
lib/active_ldap/adapter/jndi.rb,
lib/active_ldap/adapter/ldap.rb,
lib/active_ldap/adapter/net_ldap.rb
Constant Summary collapse
- VALID_ADAPTER_CONFIGURATION_KEYS =
[:host, :port, :method, :timeout, :retry_on_timeout, :retry_limit, :retry_wait, :bind_dn, :password, :password_block, :try_sasl, :sasl_mechanisms, :sasl_quiet, :allow_anonymous, :store_password, :scope, :sasl_options]
- @@row_even =
true
Instance Attribute Summary collapse
-
#runtime ⇒ Object
readonly
Returns the value of attribute runtime.
Class Method Summary collapse
- .jndi_connection(options) ⇒ Object
- .ldap_connection(options) ⇒ Object
- .net_ldap_connection(options) ⇒ Object
Instance Method Summary collapse
- #add(dn, entries, options = {}) ⇒ Object
- #bind(options = {}) ⇒ Object
- #bind_as_anonymous(options = {}) ⇒ Object
- #bound? ⇒ Boolean
- #connect(options = {}) ⇒ Object
- #connecting? ⇒ Boolean
- #delete(targets, options = {}) ⇒ Object
- #disconnect!(options = {}) ⇒ Object
- #entry_attribute(object_classes) ⇒ Object
-
#initialize(configuration = {}) ⇒ Base
constructor
A new instance of Base.
- #log_info(name, runtime_in_seconds, info = nil) ⇒ Object
- #modify(dn, entries, options = {}) ⇒ Object
- #modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options = {}) ⇒ Object
- #naming_contexts ⇒ Object
- #rebind(options = {}) ⇒ Object
- #reset_runtime ⇒ Object
- #schema(options = {}) ⇒ Object
- #search(options = {}) ⇒ Object
- #unbind(options = {}) ⇒ Object
Methods included from GetTextSupport
Constructor Details
#initialize(configuration = {}) ⇒ Base
Returns a new instance of Base.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_ldap/adapter/base.rb', line 23 def initialize(configuration={}) @runtime = 0 @connection = nil @disconnected = false @bound = false @bind_tried = false @entry_attributes = {} @configuration = configuration.dup @logger = @configuration.delete(:logger) @configuration.assert_valid_keys(VALID_ADAPTER_CONFIGURATION_KEYS) VALID_ADAPTER_CONFIGURATION_KEYS.each do |name| instance_variable_set("@#{name}", configuration[name]) end @instrumenter = ActiveSupport::Notifications.instrumenter end |
Instance Attribute Details
#runtime ⇒ Object (readonly)
Returns the value of attribute runtime.
22 23 24 |
# File 'lib/active_ldap/adapter/base.rb', line 22 def runtime @runtime end |
Class Method Details
.jndi_connection(options) ⇒ Object
7 8 9 10 |
# File 'lib/active_ldap/adapter/jndi.rb', line 7 def jndi_connection() require 'active_ldap/adapter/jndi_connection' Jndi.new() end |
Instance Method Details
#add(dn, entries, options = {}) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/active_ldap/adapter/base.rb', line 198 def add(dn, entries, ={}) dn = ensure_dn_string(dn) begin operation() do yield(dn, entries) end rescue LdapError::NoSuchObject raise EntryNotFound, _("No such entry: %s") % dn rescue LdapError::InvalidDnSyntax raise DistinguishedNameInvalid.new(dn) rescue LdapError::AlreadyExists raise EntryAlreadyExist, _("%s: %s") % [$!., dn] rescue LdapError::StrongAuthRequired raise StrongAuthenticationRequired, _("%s: %s") % [$!., dn] rescue LdapError::ObjectClassViolation raise RequiredAttributeMissed, _("%s: %s") % [$!., dn] rescue LdapError::UnwillingToPerform raise OperationNotPermitted, _("%s: %s") % [$!., dn] end end |
#bind(options = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/active_ldap/adapter/base.rb', line 68 def bind(={}) @bind_tried = true bind_dn = ensure_dn_string([:bind_dn] || @bind_dn) try_sasl = .has_key?(:try_sasl) ? [:try_sasl] : @try_sasl if .has_key?(:allow_anonymous) allow_anonymous = [:allow_anonymous] else allow_anonymous = @allow_anonymous end = .merge(:allow_anonymous => allow_anonymous) # Rough bind loop: # Attempt 1: SASL if available # Attempt 2: SIMPLE with credentials if password block # Attempt 3: SIMPLE ANONYMOUS if 1 and 2 fail (or pwblock returns '') if try_sasl and sasl_bind(bind_dn, ) @logger.info {_('Bound to %s by SASL as %s') % [target, bind_dn]} elsif simple_bind(bind_dn, ) @logger.info {_('Bound to %s by simple as %s') % [target, bind_dn]} elsif allow_anonymous and bind_as_anonymous() @logger.info {_('Bound to %s as anonymous') % target} else = yield if block_given? ||= _('All authentication methods for %s exhausted.') % target raise AuthenticationError, end @bound = true @bound end |
#bind_as_anonymous(options = {}) ⇒ Object
105 106 107 |
# File 'lib/active_ldap/adapter/base.rb', line 105 def bind_as_anonymous(={}) yield end |
#bound? ⇒ Boolean
113 114 115 |
# File 'lib/active_ldap/adapter/base.rb', line 113 def bound? connecting? and @bound end |
#connect(options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/active_ldap/adapter/base.rb', line 44 def connect(={}) host = [:host] || @host method = [:method] || @method || :plain port = [:port] || @port || ensure_port(method) method = ensure_method(method) @disconnected = false @bound = false @bind_tried = false @connection, @uri, @with_start_tls = yield(host, port, method) prepare_connection() bind() end |
#connecting? ⇒ Boolean
109 110 111 |
# File 'lib/active_ldap/adapter/base.rb', line 109 def connecting? !@connection.nil? and !@disconnected end |
#delete(targets, options = {}) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/active_ldap/adapter/base.rb', line 179 def delete(targets, ={}) targets = [targets] unless targets.is_a?(Array) return if targets.empty? begin operation() do targets.each do |target| target = ensure_dn_string(target) begin yield(target) rescue LdapError::UnwillingToPerform, LdapError::InsufficientAccess raise OperationNotPermitted, _("%s: %s") % [$!., target] end end end rescue LdapError::NoSuchObject raise EntryNotFound, _("No such entry: %s") % target end end |
#disconnect!(options = {}) ⇒ Object
57 58 59 60 61 |
# File 'lib/active_ldap/adapter/base.rb', line 57 def disconnect!(={}) unbind() @connection = @uri = @with_start_tls = nil @disconnected = true end |
#entry_attribute(object_classes) ⇒ Object
151 152 153 154 |
# File 'lib/active_ldap/adapter/base.rb', line 151 def entry_attribute(object_classes) @entry_attributes[object_classes.uniq.sort] ||= EntryAttribute.new(schema, object_classes) end |
#log_info(name, runtime_in_seconds, info = nil) ⇒ Object
243 244 245 246 247 248 |
# File 'lib/active_ldap/adapter/base.rb', line 243 def log_info(name, runtime_in_seconds, info=nil) return unless @logger return unless @logger.debug? = "LDAP: #{name} (#{'%.1f' % (runtime_in_seconds * 1000)}ms)" @logger.debug(format_log_entry(, info)) end |
#modify(dn, entries, options = {}) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/active_ldap/adapter/base.rb', line 219 def modify(dn, entries, ={}) dn = ensure_dn_string(dn) begin operation() do begin yield(dn, entries) rescue LdapError::UnwillingToPerform, LdapError::InsufficientAccess raise OperationNotPermitted, _("%s: %s") % [$!., target] end end rescue LdapError::UndefinedType raise rescue LdapError::ObjectClassViolation raise RequiredAttributeMissed, _("%s: %s") % [$!., dn] end end |
#modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options = {}) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/active_ldap/adapter/base.rb', line 236 def modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, ={}) dn = ensure_dn_string(dn) operation() do yield(dn, new_rdn, delete_old_rdn, new_superior) end end |
#naming_contexts ⇒ Object
147 148 149 |
# File 'lib/active_ldap/adapter/base.rb', line 147 def naming_contexts root_dse_values('namingContexts') end |
#rebind(options = {}) ⇒ Object
63 64 65 66 |
# File 'lib/active_ldap/adapter/base.rb', line 63 def rebind(={}) unbind() if bound? connect() end |
#reset_runtime ⇒ Object
39 40 41 42 |
# File 'lib/active_ldap/adapter/base.rb', line 39 def reset_runtime runtime, @runtime = @runtime, 0 runtime end |
#schema(options = {}) ⇒ Object
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 |
# File 'lib/active_ldap/adapter/base.rb', line 117 def schema(={}) @schema ||= operation() do base = [:base] attrs = [:attributes] attrs ||= [ 'objectClasses', 'attributeTypes', 'matchingRules', 'matchingRuleUse', 'dITStructureRules', 'dITContentRules', 'nameForms', 'ldapSyntaxes', #'extendedAttributeInfo', # if we need RANGE-LOWER/UPPER. ] base ||= root_dse_values('subschemaSubentry', )[0] base ||= 'cn=schema' schema = nil search(:base => base, :scope => :base, :filter => '(objectClass=subschema)', :attributes => attrs, :limit => 1) do |dn, attributes| schema = Schema.new(attributes) end schema || Schema.new([]) end end |
#search(options = {}) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/active_ldap/adapter/base.rb', line 156 def search(={}) filter = parse_filter([:filter]) || 'objectClass=*' attrs = [:attributes] || [] scope = ensure_scope([:scope] || @scope) base = [:base] limit = [:limit] || 0 limit = nil if limit <= 0 attrs = attrs.to_a # just in case base = ensure_dn_string(base) begin operation() do yield(base, scope, filter, attrs, limit) end rescue LdapError::NoSuchObject, LdapError::InvalidDnSyntax # Do nothing on failure @logger.info do args = [$!.class, $!., filter, attrs.inspect] _("Ignore error %s(%s): filter %s: attributes: %s") % args end end end |
#unbind(options = {}) ⇒ Object
100 101 102 103 |
# File 'lib/active_ldap/adapter/base.rb', line 100 def unbind(={}) yield if @connection and (@bind_tried or bound?) @bind_tried = @bound = false end |