Class: Net::LDAP::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ldap/entry.rb

Overview

Objects of this class represent individual entries in an LDAP directory. User code generally does not instantiate this class. Net::LDAP#search provides objects of this class to user code, either as block parameters or as return values.

In LDAP-land, an “entry” is a collection of attributes that are uniquely and globally identified by a DN (“Distinguished Name”). Attributes are identified by short, descriptive words or phrases. Although a directory is free to implement any attribute name, most of them follow rigorous standards so that the range of commonly-encountered attribute names is not large.

An attribute name is case-insensitive. Most directories also restrict the range of characters allowed in attribute names. To simplify handling attribute names, Net::LDAP::Entry internally converts them to a standard format. Therefore, the methods which take attribute names can take Strings or Symbols, and work correctly regardless of case or capitalization.

An attribute consists of zero or more data items called values. An entry is the combination of a unique DN, a set of attribute names, and a (possibly-empty) array of values for each attribute.

Class Net::LDAP::Entry provides convenience methods for dealing with LDAP entries. In addition to the methods documented below, you may access individual attributes of an entry simply by giving the attribute name as the name of a method call. For example:

ldap.search( ... ) do |entry|
  puts "Common name: #{entry.cn}"
  puts "Email addresses:"
  entry.mail.each {|ma| puts ma}
end

If you use this technique to access an attribute that is not present in a particular Entry object, a NoMethodError exception will be raised.

– Ugly problem to fix someday: We key off the internal hash with a canonical form of the attribute name: convert to a string, downcase, then take the symbol. Unfortunately we do this in at least three places. Should do it in ONE place.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dn = nil) ⇒ Entry

This constructor is not generally called by user code.



70
71
72
73
# File 'lib/net/ldap/entry.rb', line 70

def initialize(dn = nil) #:nodoc:
  @myhash = {}
  @myhash[:dn] = [dn]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

:nodoc:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/net/ldap/entry.rb', line 178

def method_missing(sym, *args, &block) #:nodoc:
  name = self.class.attribute_name(sym)

  if valid_attribute?(name )
    if setter?(sym) && args.size == 1
      value = args.first
      value = Array(value)
      self[name]= value
      return value
    elsif args.empty?
      return self[name]
    end
  end

  super
end

Class Method Details

._load(entry) ⇒ Object

Use the LDIF format for Marshal serialization.



83
84
85
# File 'lib/net/ldap/entry.rb', line 83

def self._load(entry) #:nodoc:
  from_single_ldif_string(entry)
end

.attribute_name(name) ⇒ Object

Canonicalizes an LDAP attribute name as a Symbol. The name is lowercased and, if present, a trailing equals sign is removed.



108
109
110
111
112
# File 'lib/net/ldap/entry.rb', line 108

def attribute_name(name)
  name = name.to_s.downcase
  name = name[0..-2] if name[-1] == ?=
  name.to_sym
end

.from_single_ldif_string(ldif) ⇒ Object

Converts a single LDIF entry string into an Entry object. Useful for Marshal serialization. If a string with multiple LDIF entries is provided, an exception will be raised.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/net/ldap/entry.rb', line 92

def from_single_ldif_string(ldif)
  ds = Net::LDAP::Dataset.read_ldif(::StringIO.new(ldif))

  return nil if ds.empty?

  raise Net::LDAP::LdapError, "Too many LDIF entries" unless ds.size == 1

  entry = ds.to_entries.first

  return nil if entry.dn.nil?
  entry
end

Instance Method Details

#[](name) ⇒ Object

Reads the array of values for the provided attribute. The attribute name is canonicalized prior to reading. Returns an empty array if the attribute does not exist.



134
135
136
137
# File 'lib/net/ldap/entry.rb', line 134

def [](name)
  name = self.class.attribute_name(name)
  @myhash[name] || []
end

#[]=(name, value) ⇒ Object

Sets or replaces the array of values for the provided attribute. The attribute name is canonicalized prior to assignment.

When an attribute is set using this, that attribute is now made accessible through methods as well.

entry = Net::LDAP::Entry.new("dc=com")
entry.foo             # => NoMethodError
entry["foo"] = 12345  # => [12345]
entry.foo             # => [12345]


126
127
128
# File 'lib/net/ldap/entry.rb', line 126

def []=(name, value)
  @myhash[self.class.attribute_name(name)] = Kernel::Array(value)
end

#_dump(depth) ⇒ Object

Use the LDIF format for Marshal serialization.



77
78
79
# File 'lib/net/ldap/entry.rb', line 77

def _dump(depth) #:nodoc:
  to_ldif
end

#attribute_namesObject

Returns an array of the attribute names present in the Entry.



147
148
149
# File 'lib/net/ldap/entry.rb', line 147

def attribute_names
  @myhash.keys
end

#dnObject

Returns the first distinguished name (dn) of the Entry as a String.



141
142
143
# File 'lib/net/ldap/entry.rb', line 141

def dn
  self[:dn].first.to_s
end

#eachObject Also known as: each_attribute

Accesses each of the attributes present in the Entry.

Calls a user-supplied block with each attribute in turn, passing two arguments to the block: a Symbol giving the name of the attribute, and a (possibly empty) Array of data values.



157
158
159
160
161
162
163
164
# File 'lib/net/ldap/entry.rb', line 157

def each # :yields: attribute-name, data-values-array
  if block_given?
    attribute_names.each {|a|
      attr_name,values = a,self[a]
      yield attr_name, values
    }
  end
end

#respond_to?(sym) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


173
174
175
176
# File 'lib/net/ldap/entry.rb', line 173

def respond_to?(sym) #:nodoc:
  return true if valid_attribute?(self.class.attribute_name(sym))
  return super
end

#to_ldifObject

Converts the Entry to an LDIF-formatted String



169
170
171
# File 'lib/net/ldap/entry.rb', line 169

def to_ldif
  Net::LDAP::Dataset.from_entry(self).to_ldif_string
end