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