[18425] | 1 | #
|
---|
| 2 | # = uri/ldap.rb
|
---|
| 3 | #
|
---|
| 4 | # Author::
|
---|
| 5 | # Takaaki Tateishi <[email protected]>
|
---|
| 6 | # Akira Yamada <[email protected]>
|
---|
| 7 | # License::
|
---|
| 8 | # URI::LDAP is copyrighted free software by Takaaki Tateishi and Akira Yamada.
|
---|
| 9 | # You can redistribute it and/or modify it under the same term as Ruby.
|
---|
| 10 | # Revision:: $Id: ldap.rb 11708 2007-02-12 23:01:19Z shyouhei $
|
---|
| 11 | #
|
---|
| 12 |
|
---|
| 13 | require 'uri/generic'
|
---|
| 14 |
|
---|
| 15 | module URI
|
---|
| 16 |
|
---|
| 17 | #
|
---|
| 18 | # LDAP URI SCHEMA (described in RFC2255)
|
---|
| 19 | # ldap://<host>/<dn>[?<attrs>[?<scope>[?<filter>[?<extensions>]]]]
|
---|
| 20 | #
|
---|
| 21 | class LDAP < Generic
|
---|
| 22 |
|
---|
| 23 | DEFAULT_PORT = 389
|
---|
| 24 |
|
---|
| 25 | COMPONENT = [
|
---|
| 26 | :scheme,
|
---|
| 27 | :host, :port,
|
---|
| 28 | :dn,
|
---|
| 29 | :attributes,
|
---|
| 30 | :scope,
|
---|
| 31 | :filter,
|
---|
| 32 | :extensions,
|
---|
| 33 | ].freeze
|
---|
| 34 |
|
---|
| 35 | SCOPE = [
|
---|
| 36 | SCOPE_ONE = 'one',
|
---|
| 37 | SCOPE_SUB = 'sub',
|
---|
| 38 | SCOPE_BASE = 'base',
|
---|
| 39 | ].freeze
|
---|
| 40 |
|
---|
| 41 | def self.build(args)
|
---|
| 42 | tmp = Util::make_components_hash(self, args)
|
---|
| 43 |
|
---|
| 44 | if tmp[:dn]
|
---|
| 45 | tmp[:path] = tmp[:dn]
|
---|
| 46 | end
|
---|
| 47 |
|
---|
| 48 | query = []
|
---|
| 49 | [:extensions, :filter, :scope, :attributes].collect do |x|
|
---|
| 50 | next if !tmp[x] && query.size == 0
|
---|
| 51 | query.unshift(tmp[x])
|
---|
| 52 | end
|
---|
| 53 |
|
---|
| 54 | tmp[:query] = query.join('?')
|
---|
| 55 |
|
---|
| 56 | return super(tmp)
|
---|
| 57 | end
|
---|
| 58 |
|
---|
| 59 | def initialize(*arg)
|
---|
| 60 | super(*arg)
|
---|
| 61 |
|
---|
| 62 | if @fragment
|
---|
| 63 | raise InvalidURIError, 'bad LDAP URL'
|
---|
| 64 | end
|
---|
| 65 |
|
---|
| 66 | parse_dn
|
---|
| 67 | parse_query
|
---|
| 68 | end
|
---|
| 69 |
|
---|
| 70 | def parse_dn
|
---|
| 71 | @dn = @path[1..-1]
|
---|
| 72 | end
|
---|
| 73 | private :parse_dn
|
---|
| 74 |
|
---|
| 75 | def parse_query
|
---|
| 76 | @attributes = nil
|
---|
| 77 | @scope = nil
|
---|
| 78 | @filter = nil
|
---|
| 79 | @extensions = nil
|
---|
| 80 |
|
---|
| 81 | if @query
|
---|
| 82 | attrs, scope, filter, extensions = @query.split('?')
|
---|
| 83 |
|
---|
| 84 | @attributes = attrs if attrs && attrs.size > 0
|
---|
| 85 | @scope = scope if scope && scope.size > 0
|
---|
| 86 | @filter = filter if filter && filter.size > 0
|
---|
| 87 | @extensions = extensions if extensions && extensions.size > 0
|
---|
| 88 | end
|
---|
| 89 | end
|
---|
| 90 | private :parse_query
|
---|
| 91 |
|
---|
| 92 | def build_path_query
|
---|
| 93 | @path = '/' + @dn
|
---|
| 94 |
|
---|
| 95 | query = []
|
---|
| 96 | [@extensions, @filter, @scope, @attributes].each do |x|
|
---|
| 97 | next if !x && query.size == 0
|
---|
| 98 | query.unshift(x)
|
---|
| 99 | end
|
---|
| 100 | @query = query.join('?')
|
---|
| 101 | end
|
---|
| 102 | private :build_path_query
|
---|
| 103 |
|
---|
| 104 | def dn
|
---|
| 105 | @dn
|
---|
| 106 | end
|
---|
| 107 |
|
---|
| 108 | def set_dn(val)
|
---|
| 109 | @dn = val
|
---|
| 110 | build_path_query
|
---|
| 111 | @dn
|
---|
| 112 | end
|
---|
| 113 | protected :set_dn
|
---|
| 114 |
|
---|
| 115 | def dn=(val)
|
---|
| 116 | set_dn(val)
|
---|
| 117 | val
|
---|
| 118 | end
|
---|
| 119 |
|
---|
| 120 | def attributes
|
---|
| 121 | @attributes
|
---|
| 122 | end
|
---|
| 123 |
|
---|
| 124 | def set_attributes(val)
|
---|
| 125 | @attributes = val
|
---|
| 126 | build_path_query
|
---|
| 127 | @attributes
|
---|
| 128 | end
|
---|
| 129 | protected :set_attributes
|
---|
| 130 |
|
---|
| 131 | def attributes=(val)
|
---|
| 132 | set_attributes(val)
|
---|
| 133 | val
|
---|
| 134 | end
|
---|
| 135 |
|
---|
| 136 | def scope
|
---|
| 137 | @scope
|
---|
| 138 | end
|
---|
| 139 |
|
---|
| 140 | def set_scope(val)
|
---|
| 141 | @scope = val
|
---|
| 142 | build_path_query
|
---|
| 143 | @scope
|
---|
| 144 | end
|
---|
| 145 | protected :set_scope
|
---|
| 146 |
|
---|
| 147 | def scope=(val)
|
---|
| 148 | set_scope(val)
|
---|
| 149 | val
|
---|
| 150 | end
|
---|
| 151 |
|
---|
| 152 | def filter
|
---|
| 153 | @filter
|
---|
| 154 | end
|
---|
| 155 |
|
---|
| 156 | def set_filter(val)
|
---|
| 157 | @filter = val
|
---|
| 158 | build_path_query
|
---|
| 159 | @filter
|
---|
| 160 | end
|
---|
| 161 | protected :set_filter
|
---|
| 162 |
|
---|
| 163 | def filter=(val)
|
---|
| 164 | set_filter(val)
|
---|
| 165 | val
|
---|
| 166 | end
|
---|
| 167 |
|
---|
| 168 | def extensions
|
---|
| 169 | @extensions
|
---|
| 170 | end
|
---|
| 171 |
|
---|
| 172 | def set_extensions(val)
|
---|
| 173 | @extensions = val
|
---|
| 174 | build_path_query
|
---|
| 175 | @extensions
|
---|
| 176 | end
|
---|
| 177 | protected :set_extensions
|
---|
| 178 |
|
---|
| 179 | def extensions=(val)
|
---|
| 180 | set_extensions(val)
|
---|
| 181 | val
|
---|
| 182 | end
|
---|
| 183 |
|
---|
| 184 | def hierarchical?
|
---|
| 185 | false
|
---|
| 186 | end
|
---|
| 187 | end
|
---|
| 188 |
|
---|
| 189 | @@schemes['LDAP'] = LDAP
|
---|
| 190 | end
|
---|