add(dn, entries, options={}) { |dn, entries| ... }
click to toggle source
def add(dn, entries, options={})
dn = ensure_dn_string(dn)
begin
operation(options) 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") % [$!.message, dn]
rescue LdapError::StrongAuthRequired
raise StrongAuthenticationRequired, _("%s: %s") % [$!.message, dn]
rescue LdapError::ObjectClassViolation
raise RequiredAttributeMissed, _("%s: %s") % [$!.message, dn]
rescue LdapError::UnwillingToPerform
raise OperationNotPermitted, _("%s: %s") % [$!.message, dn]
end
end
bind(options={}) { || ... }
click to toggle source
def bind(options={})
@bind_tried = true
bind_dn = ensure_dn_string(options[:bind_dn] || @bind_dn)
try_sasl = options.has_key?(:try_sasl) ? options[:try_sasl] : @try_sasl
if options.has_key?(:allow_anonymous)
allow_anonymous = options[:allow_anonymous]
else
allow_anonymous = @allow_anonymous
end
options = options.merge(:allow_anonymous => allow_anonymous)
if try_sasl and sasl_bind(bind_dn, options)
@logger.info {_('Bound to %s by SASL as %s') % [target, bind_dn]}
elsif simple_bind(bind_dn, options)
@logger.info {_('Bound to %s by simple as %s') % [target, bind_dn]}
elsif allow_anonymous and bind_as_anonymous(options)
@logger.info {_('Bound to %s as anonymous') % target}
else
message = yield if block_given?
message ||= _('All authentication methods for %s exhausted.') % target
raise AuthenticationError, message
end
@bound = true
@bound
end
bind_as_anonymous(options={}) { || ... }
click to toggle source
def bind_as_anonymous(options={})
yield
end
bound?()
click to toggle source
def bound?
connecting? and @bound
end
connect(options={}) { |host, port, method| ... }
click to toggle source
def connect(options={})
host = options[:host] || @host
method = options[:method] || @method || :plain
port = options[: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(options)
bind(options)
end
connecting?()
click to toggle source
def connecting?
!@connection.nil? and !@disconnected
end
delete(targets, options={}) { |target| ... }
click to toggle source
def delete(targets, options={})
targets = [targets] unless targets.is_a?(Array)
return if targets.empty?
begin
operation(options) do
targets.each do |target|
target = ensure_dn_string(target)
begin
yield(target)
rescue LdapError::UnwillingToPerform, LdapError::InsufficientAccess
raise OperationNotPermitted, _("%s: %s") % [$!.message, target]
end
end
end
rescue LdapError::NoSuchObject
raise EntryNotFound, _("No such entry: %s") % target
end
end
disconnect!(options={})
click to toggle source
def disconnect!(options={})
unbind(options)
@connection = @uri = @with_start_tls = nil
@disconnected = true
end
entry_attribute(object_classes)
click to toggle source
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)
click to toggle source
def log_info(name, runtime_in_seconds, info=nil)
return unless @logger
return unless @logger.debug?
message = "LDAP: #{name} (#{'%.1f' % (runtime_in_seconds * 1000)}ms)"
@logger.debug(format_log_entry(message, info))
end
modify(dn, entries, options={}) { |dn, entries| ... }
click to toggle source
def modify(dn, entries, options={})
dn = ensure_dn_string(dn)
begin
operation(options) do
begin
yield(dn, entries)
rescue LdapError::UnwillingToPerform, LdapError::InsufficientAccess
raise OperationNotPermitted, _("%s: %s") % [$!.message, target]
end
end
rescue LdapError::UndefinedType
raise
rescue LdapError::ObjectClassViolation
raise RequiredAttributeMissed, _("%s: %s") % [$!.message, dn]
end
end
modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options={}) { |dn, new_rdn, delete_old_rdn, new_superior| ... }
click to toggle source
def modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options={})
dn = ensure_dn_string(dn)
operation(options) do
yield(dn, new_rdn, delete_old_rdn, new_superior)
end
end
naming_contexts()
click to toggle source
def naming_contexts
root_dse_values('namingContexts')
end
rebind(options={})
click to toggle source
def rebind(options={})
unbind(options) if bound?
connect(options)
end
reset_runtime()
click to toggle source
def reset_runtime
runtime, @runtime = @runtime, 0
runtime
end
schema(options={})
click to toggle source
def schema(options={})
@schema ||= operation(options) do
base = options[:base]
attrs = options[:attributes]
attrs ||= [
'objectClasses',
'attributeTypes',
'matchingRules',
'matchingRuleUse',
'dITStructureRules',
'dITContentRules',
'nameForms',
'ldapSyntaxes',
]
base ||= root_dse_values('subschemaSubentry', options)[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={}) { |base, scope, filter, attrs, limit| ... }
click to toggle source
def search(options={})
filter = parse_filter(options[:filter]) || 'objectClass=*'
attrs = options[:attributes] || []
scope = ensure_scope(options[:scope] || @scope)
base = options[:base]
limit = options[:limit] || 0
limit = nil if limit <= 0
attrs = attrs.to_a
base = ensure_dn_string(base)
begin
operation(options) do
yield(base, scope, filter, attrs, limit)
end
rescue LdapError::NoSuchObject, LdapError::InvalidDnSyntax
@logger.info do
args = [$!.class, $!.message, filter, attrs.inspect]
_("Ignore error %s(%s): filter %s: attributes: %s") % args
end
end
end
unbind(options={}) { || ... }
click to toggle source
def unbind(options={})
yield if @connection and (@bind_tried or bound?)
@bind_tried = @bound = false
end