Class: Ldaptic::DN

Inherits:
String show all
Defined in:
lib/ldaptic/dn.rb

Overview

RFC4512 - Lightweight Directory Access Protocol (LDAP): Directory Information Models RFC4514 - Lightweight Directory Access Protocol (LDAP): String Representation of Distinguished Names

Constant Summary collapse

OID =
'1.3.6.1.4.1.1466.115.121.1.12'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from String

#ord

Methods included from Filter::Conversions

#to_ldap_filter

Constructor Details

#initialize(dn, source = nil) ⇒ DN

Create a new Ldaptic::DN object. dn can either be a string, or an array of pairs.

Ldaptic::DN([{:cn=>"Thomas, David"}, {:dc=>"pragprog"}, {:dc=>"com"}])
# => "CN=Thomas\\, David,DC=pragprog,DC=com"

The optional second object specifies either an LDAP::Conn object or a Ldaptic object to be used to find the DN with #find.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ldaptic/dn.rb', line 49

def initialize(dn, source = nil)
  @source = source
  dn = dn.dn if dn.respond_to?(:dn)
  if dn.respond_to?(:to_ary)
    dn = dn.map do |pair|
      if pair.kind_of?(Hash)
        Ldaptic::RDN(pair).to_str
      else
        pair
      end
    end * ','
  end
  if dn.include?(".") && !dn.include?("=")
    dn = dn.split(".").map {|dc| "DC=#{Ldaptic.escape(dc)}"} * ","
  end
  super(dn)
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



39
40
41
# File 'lib/ldaptic/dn.rb', line 39

def source
  @source
end

Class Method Details

.[](*args) ⇒ Object

Ldaptic::DN[=> ‘com’, => ‘amazon’]

=> "dc=amazon,dc=com"


35
36
37
# File 'lib/ldaptic/dn.rb', line 35

def self.[](*args)
  Ldaptic::DN(args.reverse)
end

Instance Method Details

#/(*args) ⇒ Object

Prepend an RDN to the DN.

Ldaptic::DN(:dc => "com")/{:dc => "foobar"} #=> "DC=foobar,DC=com"


171
172
173
# File 'lib/ldaptic/dn.rb', line 171

def /(*args)
  Ldaptic::DN(args.reverse + rdns, source)
end

#<<(arg) ⇒ Object

With a Hash (and only with a Hash), prepends a RDN to the DN, modifying the receiver in place. Otherwise, behaves like String#<<.



177
178
179
180
181
182
183
# File 'lib/ldaptic/dn.rb', line 177

def <<(arg)
  if arg.kind_of?(Hash)
    replace(self/arg)
  else
    super
  end
end

#==(other) ⇒ Object

TODO: investigate compliance with RFC4517 - Lightweight Directory Access Protocol (LDAP): Syntaxes and Matching Rules



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ldaptic/dn.rb', line 140

def ==(other)
  if other.respond_to?(:dn)
    other = Ldaptic::DN(other)
  end
  normalize = lambda do |hash|
    hash.inject({}) do |m, (k, v)|
      m[Ldaptic.encode(k).upcase] = v
      m
    end
  end
  if other.kind_of?(Ldaptic::DN)
    rdns == other.rdns
  else
    super
  end
end

#[](*args) ⇒ Object

Pass in one or more hashes to augment the DN. Otherwise, this behaves the same as String#[]



160
161
162
163
164
165
166
# File 'lib/ldaptic/dn.rb', line 160

def [](*args)
  if args.first.kind_of?(Hash) || args.first.kind_of?(Ldaptic::DN)
    send(:/, *args)
  else
    super
  end
end

#domainObject

Join all DC elements with periods.



117
118
119
120
# File 'lib/ldaptic/dn.rb', line 117

def domain
  components = rdns.map {|rdn| rdn[:dc]}.compact
  components.join('.') unless components.empty?
end

#find(source = @source) ⇒ Object

If a source object was given, it is used to search for the DN. Otherwise, an exception is raised.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ldaptic/dn.rb', line 73

def find(source = @source)
  scope = 0
  filter = "(objectClass=*)"
  if source.respond_to?(:search2_ext)
    source.search2(
      to_s,
      scope,
      filter
    )
  elsif source.respond_to?(:search)
    Array(source.search(
      :base => to_s,
      :scope => scope,
      :filter => filter,
      :limit => 1
    ))
  else
    raise RuntimeError, "missing or invalid source for LDAP search", caller
  end.first
end

#include?(arg) ⇒ Boolean

With a Hash, check for the presence of an RDN. Otherwise, behaves like String#include?

Returns:

  • (Boolean)


187
188
189
190
191
192
193
# File 'lib/ldaptic/dn.rb', line 187

def include?(arg)
  if arg.kind_of?(Hash)
    rdns.include?(arg)
  else
    super
  end
end

#normalizeObject



130
131
132
# File 'lib/ldaptic/dn.rb', line 130

def normalize
  Ldaptic::DN(rdns, source)
end

#normalize!Object



134
135
136
# File 'lib/ldaptic/dn.rb', line 134

def normalize!
  replace(normalize)
end

#parentObject



122
123
124
# File 'lib/ldaptic/dn.rb', line 122

def parent
  Ldaptic::DN(rdns[1..-1], source)
end

#rdnObject



126
127
128
# File 'lib/ldaptic/dn.rb', line 126

def rdn
  rdns.first
end

#rdn_stringsObject



102
103
104
# File 'lib/ldaptic/dn.rb', line 102

def rdn_strings
  Ldaptic.split(self, ?,)
end

#rdnsObject

Convert the DN to an array of RDNs.

Ldaptic::DN("cn=Thomas\\, David,dc=pragprog,dc=com").rdns
# => [{:cn=>"Thomas, David"},{:dc=>"pragprog"},{:dc=>"com"}]


98
99
100
# File 'lib/ldaptic/dn.rb', line 98

def rdns
  rdn_strings.map {|rdn| RDN.new(rdn)}
end

#to_aObject



106
107
108
109
110
111
112
113
114
# File 'lib/ldaptic/dn.rb', line 106

def to_a
  # This is really horrid, but the last hack broke.  Consider abandoning
  # this method entirely.
  if caller.first =~ /:in `Array'$/
    [self]
  else
    rdns
  end
end

#to_dnObject



67
68
69
# File 'lib/ldaptic/dn.rb', line 67

def to_dn
  self
end