comparison vendor/plugins/ruby-net-ldap-0.0.4/lib/net/ldap/entry.rb @ 0:513646585e45

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