# frozen_string_literal: false
# Takaaki Tateishi <ttate@jaist.ac.jp>
# Akira Yamada <akira@ruby-lang.org>
# URI::LDAP is copyrighted free software by Takaaki Tateishi and Akira Yamada.
# You can redistribute it and/or modify it under the same term as Ruby.
# See URI for general documentation
require_relative 'generic'
# LDAP URI SCHEMA (described in RFC2255).
# ldap://<host>/<dn>[?<attrs>[?<scope>[?<filter>[?<extensions>]]]]
# A Default port of 389 for URI::LDAP.
# An Array of the available components for URI::LDAP.
# Scopes available for the starting point.
# * SCOPE_BASE - the Base DN
# * SCOPE_ONE - one level under the Base DN, not including the base DN and
# not including any entries under this
# * SCOPE_SUB - subtrees, all entries at all levels
# Creates a new URI::LDAP object from components, with syntax checking.
# The components accepted are host, port, dn, attributes,
# scope, filter, and extensions.
# The components should be provided either as an Array, or as a Hash
# with keys formed by preceding the component names with a colon.
# If an Array is used, the components must be passed in the
# order <code>[host, port, dn, attributes, scope, filter, extensions]</code>.
# uri = URI::LDAP.build({:host => 'ldap.example.com',
# uri = URI::LDAP.build(["ldap.example.com", nil,
# "/dc=example;dc=com", "query", nil, nil, nil])
tmp = Util::make_components_hash(self, args)
[:extensions, :filter, :scope, :attributes].collect do |x|
next if !tmp[x] && query.size == 0
tmp[:query] = query.join('?')
# Creates a new URI::LDAP object from generic URI components as per
# RFC 2396. No LDAP-specific syntax checking is performed.
# Arguments are +scheme+, +userinfo+, +host+, +port+, +registry+, +path+,
# +opaque+, +query+, and +fragment+, in that order.
# uri = URI::LDAP.new("ldap", nil, "ldap.example.com", nil, nil,
# "/dc=example;dc=com", nil, "query", nil)
# See also URI::Generic.new.
raise InvalidURIError, 'bad LDAP URL'
# Private method to cleanup +dn+ from using the +path+ component attribute.
raise InvalidURIError, 'bad LDAP URL' unless @path
# Private method to cleanup +attributes+, +scope+, +filter+, and +extensions+
# from using the +query+ component attribute.
attrs, scope, filter, extensions = @query.split('?')
@attributes = attrs if attrs && attrs.size > 0
@scope = scope if scope && scope.size > 0
@filter = filter if filter && filter.size > 0
@extensions = extensions if extensions && extensions.size > 0
# Private method to assemble +query+ from +attributes+, +scope+, +filter+, and +extensions+.
[@extensions, @filter, @scope, @attributes].each do |x|
next if !x && query.size == 0
private :build_path_query
# Private setter for dn +val+.
# Private setter for attributes +val+.
protected :set_attributes
# Setter for attributes +val+.
# Private setter for scope +val+.
# Setter for scope +val+.
# Private setter for filter +val+.
# Setter for filter +val+.
# Private setter for extensions +val+.
protected :set_extensions
# Setter for extensions +val+.
# Checks if URI has a path.
# For URI::LDAP this will return +false+.