2002-01-10 03:00:51 -05:00
|
|
|
#
|
2004-03-24 06:53:31 -05:00
|
|
|
# = uri/ldap.rb
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# Author::
|
2004-03-24 06:53:31 -05:00
|
|
|
# Takaaki Tateishi <ttate@jaist.ac.jp>
|
|
|
|
# Akira Yamada <akira@ruby-lang.org>
|
2009-03-05 22:56:38 -05:00
|
|
|
# License::
|
2004-03-24 06:53:31 -05:00
|
|
|
# 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.
|
|
|
|
# Revision:: $Id$
|
2002-01-10 03:00:51 -05:00
|
|
|
#
|
|
|
|
|
|
|
|
require 'uri/generic'
|
|
|
|
|
|
|
|
module URI
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
# LDAP URI SCHEMA (described in RFC2255)
|
|
|
|
# ldap://<host>/<dn>[?<attrs>[?<scope>[?<filter>[?<extensions>]]]]
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
class LDAP < Generic
|
|
|
|
|
|
|
|
DEFAULT_PORT = 389
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2002-01-10 03:00:51 -05:00
|
|
|
COMPONENT = [
|
|
|
|
:scheme,
|
|
|
|
:host, :port,
|
|
|
|
:dn,
|
|
|
|
:attributes,
|
|
|
|
:scope,
|
|
|
|
:filter,
|
|
|
|
:extensions,
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
SCOPE = [
|
|
|
|
SCOPE_ONE = 'one',
|
|
|
|
SCOPE_SUB = 'sub',
|
|
|
|
SCOPE_BASE = 'base',
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
def self.build(args)
|
|
|
|
tmp = Util::make_components_hash(self, args)
|
|
|
|
|
|
|
|
if tmp[:dn]
|
2004-03-24 06:53:31 -05:00
|
|
|
tmp[:path] = tmp[:dn]
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
query = []
|
|
|
|
[:extensions, :filter, :scope, :attributes].collect do |x|
|
2004-03-24 06:53:31 -05:00
|
|
|
next if !tmp[x] && query.size == 0
|
|
|
|
query.unshift(tmp[x])
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
tmp[:query] = query.join('?')
|
|
|
|
|
|
|
|
return super(tmp)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(*arg)
|
|
|
|
super(*arg)
|
|
|
|
|
|
|
|
if @fragment
|
2004-03-24 06:53:31 -05:00
|
|
|
raise InvalidURIError, 'bad LDAP URL'
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
parse_dn
|
|
|
|
parse_query
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_dn
|
|
|
|
@dn = @path[1..-1]
|
|
|
|
end
|
|
|
|
private :parse_dn
|
|
|
|
|
|
|
|
def parse_query
|
|
|
|
@attributes = nil
|
|
|
|
@scope = nil
|
|
|
|
@filter = nil
|
|
|
|
@extensions = nil
|
|
|
|
|
|
|
|
if @query
|
2004-03-24 06:53:31 -05:00
|
|
|
attrs, scope, filter, extensions = @query.split('?')
|
2002-01-10 03:00:51 -05:00
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
@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
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
private :parse_query
|
|
|
|
|
|
|
|
def build_path_query
|
|
|
|
@path = '/' + @dn
|
|
|
|
|
|
|
|
query = []
|
|
|
|
[@extensions, @filter, @scope, @attributes].each do |x|
|
2004-03-24 06:53:31 -05:00
|
|
|
next if !x && query.size == 0
|
|
|
|
query.unshift(x)
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
@query = query.join('?')
|
|
|
|
end
|
|
|
|
private :build_path_query
|
|
|
|
|
|
|
|
def dn
|
|
|
|
@dn
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_dn(val)
|
|
|
|
@dn = val
|
|
|
|
build_path_query
|
2002-10-04 02:26:45 -04:00
|
|
|
@dn
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
protected :set_dn
|
|
|
|
|
|
|
|
def dn=(val)
|
|
|
|
set_dn(val)
|
2003-02-14 00:32:48 -05:00
|
|
|
val
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
@attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_attributes(val)
|
|
|
|
@attributes = val
|
|
|
|
build_path_query
|
2002-10-04 02:26:45 -04:00
|
|
|
@attributes
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
protected :set_attributes
|
|
|
|
|
|
|
|
def attributes=(val)
|
|
|
|
set_attributes(val)
|
2003-02-14 00:32:48 -05:00
|
|
|
val
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def scope
|
|
|
|
@scope
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_scope(val)
|
|
|
|
@scope = val
|
|
|
|
build_path_query
|
2002-10-04 02:26:45 -04:00
|
|
|
@scope
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
protected :set_scope
|
|
|
|
|
|
|
|
def scope=(val)
|
|
|
|
set_scope(val)
|
2003-02-14 00:32:48 -05:00
|
|
|
val
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter
|
|
|
|
@filter
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_filter(val)
|
|
|
|
@filter = val
|
|
|
|
build_path_query
|
2002-10-04 02:26:45 -04:00
|
|
|
@filter
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
protected :set_filter
|
|
|
|
|
|
|
|
def filter=(val)
|
|
|
|
set_filter(val)
|
2003-02-14 00:32:48 -05:00
|
|
|
val
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def extensions
|
|
|
|
@extensions
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_extensions(val)
|
|
|
|
@extensions = val
|
|
|
|
build_path_query
|
2002-10-04 02:26:45 -04:00
|
|
|
@extensions
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
protected :set_extensions
|
|
|
|
|
|
|
|
def extensions=(val)
|
|
|
|
set_extensions(val)
|
2003-02-14 00:32:48 -05:00
|
|
|
val
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
2004-07-17 09:14:35 -04:00
|
|
|
def hierarchical?
|
|
|
|
false
|
|
|
|
end
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@@schemes['LDAP'] = LDAP
|
|
|
|
end
|