annotate .svn/pristine/00/00c4bd6691f32db8720a826db98aecc930f926d9.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # $Id: entry.rb 123 2006-05-18 03:52:38Z blackhedd $
Chris@909 2 #
Chris@909 3 # LDAP Entry (search-result) support classes
Chris@909 4 #
Chris@909 5 #
Chris@909 6 #----------------------------------------------------------------------------
Chris@909 7 #
Chris@909 8 # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
Chris@909 9 #
Chris@909 10 # Gmail: garbagecat10
Chris@909 11 #
Chris@909 12 # This program is free software; you can redistribute it and/or modify
Chris@909 13 # it under the terms of the GNU General Public License as published by
Chris@909 14 # the Free Software Foundation; either version 2 of the License, or
Chris@909 15 # (at your option) any later version.
Chris@909 16 #
Chris@909 17 # This program is distributed in the hope that it will be useful,
Chris@909 18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 20 # GNU General Public License for more details.
Chris@909 21 #
Chris@909 22 # You should have received a copy of the GNU General Public License
Chris@909 23 # along with this program; if not, write to the Free Software
Chris@909 24 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Chris@909 25 #
Chris@909 26 #---------------------------------------------------------------------------
Chris@909 27 #
Chris@909 28
Chris@909 29
Chris@909 30
Chris@909 31
Chris@909 32 module Net
Chris@909 33 class LDAP
Chris@909 34
Chris@909 35
Chris@909 36 # Objects of this class represent individual entries in an LDAP
Chris@909 37 # directory. User code generally does not instantiate this class.
Chris@909 38 # Net::LDAP#search provides objects of this class to user code,
Chris@909 39 # either as block parameters or as return values.
Chris@909 40 #
Chris@909 41 # In LDAP-land, an "entry" is a collection of attributes that are
Chris@909 42 # uniquely and globally identified by a DN ("Distinguished Name").
Chris@909 43 # Attributes are identified by short, descriptive words or phrases.
Chris@909 44 # Although a directory is
Chris@909 45 # free to implement any attribute name, most of them follow rigorous
Chris@909 46 # standards so that the range of commonly-encountered attribute
Chris@909 47 # names is not large.
Chris@909 48 #
Chris@909 49 # An attribute name is case-insensitive. Most directories also
Chris@909 50 # restrict the range of characters allowed in attribute names.
Chris@909 51 # To simplify handling attribute names, Net::LDAP::Entry
Chris@909 52 # internally converts them to a standard format. Therefore, the
Chris@909 53 # methods which take attribute names can take Strings or Symbols,
Chris@909 54 # and work correctly regardless of case or capitalization.
Chris@909 55 #
Chris@909 56 # An attribute consists of zero or more data items called
Chris@909 57 # <i>values.</i> An entry is the combination of a unique DN, a set of attribute
Chris@909 58 # names, and a (possibly-empty) array of values for each attribute.
Chris@909 59 #
Chris@909 60 # Class Net::LDAP::Entry provides convenience methods for dealing
Chris@909 61 # with LDAP entries.
Chris@909 62 # In addition to the methods documented below, you may access individual
Chris@909 63 # attributes of an entry simply by giving the attribute name as
Chris@909 64 # the name of a method call. For example:
Chris@909 65 # ldap.search( ... ) do |entry|
Chris@909 66 # puts "Common name: #{entry.cn}"
Chris@909 67 # puts "Email addresses:"
Chris@909 68 # entry.mail.each {|ma| puts ma}
Chris@909 69 # end
Chris@909 70 # If you use this technique to access an attribute that is not present
Chris@909 71 # in a particular Entry object, a NoMethodError exception will be raised.
Chris@909 72 #
Chris@909 73 #--
Chris@909 74 # Ugly problem to fix someday: We key off the internal hash with
Chris@909 75 # a canonical form of the attribute name: convert to a string,
Chris@909 76 # downcase, then take the symbol. Unfortunately we do this in
Chris@909 77 # at least three places. Should do it in ONE place.
Chris@909 78 class Entry
Chris@909 79
Chris@909 80 # This constructor is not generally called by user code.
Chris@909 81 def initialize dn = nil # :nodoc:
Chris@909 82 @myhash = Hash.new {|k,v| k[v] = [] }
Chris@909 83 @myhash[:dn] = [dn]
Chris@909 84 end
Chris@909 85
Chris@909 86
Chris@909 87 def []= name, value # :nodoc:
Chris@909 88 sym = name.to_s.downcase.intern
Chris@909 89 @myhash[sym] = value
Chris@909 90 end
Chris@909 91
Chris@909 92
Chris@909 93 #--
Chris@909 94 # We have to deal with this one as we do with []=
Chris@909 95 # because this one and not the other one gets called
Chris@909 96 # in formulations like entry["CN"] << cn.
Chris@909 97 #
Chris@909 98 def [] name # :nodoc:
Chris@909 99 name = name.to_s.downcase.intern unless name.is_a?(Symbol)
Chris@909 100 @myhash[name]
Chris@909 101 end
Chris@909 102
Chris@909 103 # Returns the dn of the Entry as a String.
Chris@909 104 def dn
Chris@909 105 self[:dn][0]
Chris@909 106 end
Chris@909 107
Chris@909 108 # Returns an array of the attribute names present in the Entry.
Chris@909 109 def attribute_names
Chris@909 110 @myhash.keys
Chris@909 111 end
Chris@909 112
Chris@909 113 # Accesses each of the attributes present in the Entry.
Chris@909 114 # Calls a user-supplied block with each attribute in turn,
Chris@909 115 # passing two arguments to the block: a Symbol giving
Chris@909 116 # the name of the attribute, and a (possibly empty)
Chris@909 117 # Array of data values.
Chris@909 118 #
Chris@909 119 def each
Chris@909 120 if block_given?
Chris@909 121 attribute_names.each {|a|
Chris@909 122 attr_name,values = a,self[a]
Chris@909 123 yield attr_name, values
Chris@909 124 }
Chris@909 125 end
Chris@909 126 end
Chris@909 127
Chris@909 128 alias_method :each_attribute, :each
Chris@909 129
Chris@909 130
Chris@909 131 #--
Chris@909 132 # Convenience method to convert unknown method names
Chris@909 133 # to attribute references. Of course the method name
Chris@909 134 # comes to us as a symbol, so let's save a little time
Chris@909 135 # and not bother with the to_s.downcase two-step.
Chris@909 136 # Of course that means that a method name like mAIL
Chris@909 137 # won't work, but we shouldn't be encouraging that
Chris@909 138 # kind of bad behavior in the first place.
Chris@909 139 # Maybe we should thow something if the caller sends
Chris@909 140 # arguments or a block...
Chris@909 141 #
Chris@909 142 def method_missing *args, &block # :nodoc:
Chris@909 143 s = args[0].to_s.downcase.intern
Chris@909 144 if attribute_names.include?(s)
Chris@909 145 self[s]
Chris@909 146 elsif s.to_s[-1] == 61 and s.to_s.length > 1
Chris@909 147 value = args[1] or raise RuntimeError.new( "unable to set value" )
Chris@909 148 value = [value] unless value.is_a?(Array)
Chris@909 149 name = s.to_s[0..-2].intern
Chris@909 150 self[name] = value
Chris@909 151 else
Chris@909 152 raise NoMethodError.new( "undefined method '#{s}'" )
Chris@909 153 end
Chris@909 154 end
Chris@909 155
Chris@909 156 def write
Chris@909 157 end
Chris@909 158
Chris@909 159 end # class Entry
Chris@909 160
Chris@909 161
Chris@909 162 end # class LDAP
Chris@909 163 end # module Net
Chris@909 164
Chris@909 165