lib/uri/ldap.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  # $Id: ldap.rb,v 1.1 2002/01/10 08:00:51 akira Exp $
   3  #
   4  
   5  require 'uri/generic'
   6  
   7  module URI
   8  
   9  =begin
  10  
  11  == URI::LDAP
  12  
  13  URI::LDAP is copyrighted free software by Takaaki Tateishi and akira yamada.
  14  
  15    Copyright (c) 2001 Takaaki Tateishi <ttate@jaist.ac.jp> and 
  16    akira yamada <akira@ruby-lang.org>.
  17    You can redistribute it and/or modify it under the same term as Ruby.
  18  
  19  === Super Class
  20  
  21  ((<URI::Generic>))
  22  
  23  =end
  24  
  25    # LDAP URI SCHEMA (described in RFC2255)
  26    # ldap://<host>/<dn>[?<attrs>[?<scope>[?<filter>[?<extensions>]]]]
  27    class LDAP < Generic
  28  
  29      DEFAULT_PORT = 389
  30      
  31      COMPONENT = [
  32        :scheme,
  33        :host, :port,
  34        :dn,
  35        :attributes,
  36        :scope,
  37        :filter,
  38        :extensions,
  39      ].freeze
  40  
  41      SCOPE = [
  42        SCOPE_ONE = 'one',
  43        SCOPE_SUB = 'sub',
  44        SCOPE_BASE = 'base',
  45      ].freeze
  46  
  47  =begin
  48  
  49  === Class Methods
  50  
  51  --- URI::LDAP::build
  52  
  53  --- URI::LDAP::new
  54  
  55  =end
  56  
  57      def self.build(args)
  58        tmp = Util::make_components_hash(self, args)
  59  
  60        if tmp[:dn]
  61          tmp[:path] = tmp[:dn]
  62        end
  63  
  64        query = []
  65        [:extensions, :filter, :scope, :attributes].collect do |x|
  66          next if !tmp[x] && query.size == 0
  67          query.unshift(tmp[x])
  68        end
  69  
  70        tmp[:query] = query.join('?')
  71  
  72        return super(tmp)
  73      end
  74  
  75      def initialize(*arg)
  76        super(*arg)
  77  
  78        if @fragment
  79          raise InvalidURIError, 'bad LDAP URL'
  80        end
  81  
  82        parse_dn
  83        parse_query
  84      end
  85  
  86      def parse_dn
  87        @dn = @path[1..-1]
  88      end
  89      private :parse_dn
  90  
  91      def parse_query
  92        @attributes = nil
  93        @scope      = nil
  94        @filter     = nil
  95        @extensions = nil
  96  
  97        if @query
  98          attrs, scope, filter, extensions = @query.split('?')
  99  
 100          @attributes = attrs if attrs && attrs.size > 0
 101          @scope      = scope if scope && scope.size > 0
 102          @filter     = filter if filter && filter.size > 0
 103          @extensions = extensions if extensions && extensions.size > 0
 104        end
 105      end
 106      private :parse_query
 107  
 108      def build_path_query
 109        @path = '/' + @dn
 110  
 111        query = []
 112        [@extensions, @filter, @scope, @attributes].each do |x|
 113          next if !x && query.size == 0
 114          query.unshift(x)
 115        end
 116        @query = query.join('?')
 117      end
 118      private :build_path_query
 119  
 120  =begin
 121  
 122  === Instance Methods
 123  
 124  --- URI::LDAP#dn
 125  
 126  --- URI::LDAP#dn=(v)
 127  
 128  =end
 129  
 130      def dn
 131        @dn
 132      end
 133  
 134      def set_dn(val)
 135        @dn = val
 136        build_path_query
 137      end
 138      protected :set_dn
 139  
 140      def dn=(val)
 141        set_dn(val)
 142      end
 143  
 144  =begin
 145  
 146  --- URI::LDAP#attributes
 147  
 148  --- URI::LDAP#attributes=(v)
 149  
 150  =end
 151  
 152      def attributes
 153        @attributes
 154      end
 155  
 156      def set_attributes(val)
 157        @attributes = val
 158        build_path_query
 159      end
 160      protected :set_attributes
 161  
 162      def attributes=(val)
 163        set_attributes(val)
 164      end
 165  
 166  =begin
 167  
 168  --- URI::LDAP#scope
 169  
 170  --- URI::LDAP#scope=(v)
 171  
 172  =end
 173  
 174      def scope
 175        @scope
 176      end
 177  
 178      def set_scope(val)
 179        @scope = val
 180        build_path_query
 181      end
 182      protected :set_scope
 183  
 184      def scope=(val)
 185        set_scope(val)
 186      end
 187  
 188  =begin
 189  
 190  --- URI::LDAP#filter
 191  
 192  --- URI::LDAP#filter=(v)
 193  
 194  =end
 195  
 196      def filter
 197        @filter
 198      end
 199  
 200      def set_filter(val)
 201        @filter = val
 202        build_path_query
 203      end
 204      protected :set_filter
 205  
 206      def filter=(val)
 207        set_filter(val)
 208      end
 209  
 210  =begin
 211  
 212  --- URI::LDAP#extensions
 213  
 214  --- URI::LDAP#extensions=(v)
 215  
 216  =end
 217  
 218      def extensions
 219        @extensions
 220      end
 221  
 222      def set_extensions(val)
 223        @extensions = val
 224        build_path_query
 225      end
 226      protected :set_extensions
 227  
 228      def extensions=(val)
 229        set_extensions(val)
 230      end
 231    end
 232  
 233    def hierarchical?
 234      false
 235    end
 236  
 237    @@schemes['LDAP'] = LDAP
 238  end