Class: LDAP::Server::DN

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ldap/server/dn.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dn) ⇒ DN

Returns a new instance of DN.



18
19
20
# File 'lib/ldap/server/dn.rb', line 18

def initialize(dn)
  @dname = LDAP::Server::Operation.split_dn(dn)
end

Instance Attribute Details

#dnameObject (readonly)

Returns the value of attribute dname.



9
10
11
# File 'lib/ldap/server/dn.rb', line 9

def dname
  @dname
end

Class Method Details

.join(elements) ⇒ Object

Combines a set of elements to a syntactically correct DN elements is [elements, …] where elements can be either { attr => val } or [attr, val]



14
15
16
# File 'lib/ldap/server/dn.rb', line 14

def self.join(elements)
  LDAP::Server::Operation.join_dn(elements)
end

Instance Method Details

#each(&block) ⇒ Object



209
210
211
# File 'lib/ldap/server/dn.rb', line 209

def each(&block)
  @dname.each(&block)
end

#end_with?(dn) ⇒ Boolean

Whether or not the DN ends with dn (top-down) dn is a string

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ldap/server/dn.rb', line 99

def end_with?(dn)
  needle = LDAP::Server::Operation.split_dn(dn)

  # Needle is longer than haystack
  return false if needle.length > @dname.length

  needle_index = needle.length - 1
  haystack_index = @dname.length - 1

  while needle_index >= 0
    return false if @dname[haystack_index] != needle[needle_index]
    needle_index -= 1
    haystack_index -= 1
  end
  true
end

#end_with_format?(dn) ⇒ Boolean

Whether or not the DN ends with format (top-down) (values are ignored) dn is a string

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ldap/server/dn.rb', line 118

def end_with_format?(dn)
  needle = LDAP::Server::Operation.split_dn(dn)

  # Needle is longer than haystack
  return false if needle.length > @dname.length

  needle_index = needle.length - 1
  haystack_index = @dname.length - 1

  while needle_index >= 0
    return false if @dname[haystack_index].keys != needle[needle_index].keys
    needle_index -= 1
    haystack_index -= 1
  end
  true
end

#equal?(dn) ⇒ Boolean

Whether or not the DN equals dn (values are case sensitive) dn is a string

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
145
146
# File 'lib/ldap/server/dn.rb', line 137

def equal?(dn)
  split_dn = LDAP::Server::Operation.split_dn(dn)

  return false if split_dn.length != @dname.length

  @dname.each_with_index do |pair, index|
    return false if pair != split_dn[index]
  end
  true
end

#equal_format?(dn) ⇒ Boolean

Whether or not the DN equals dn’s format (values are ignored) (case insensitive) dn is a string

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
159
# File 'lib/ldap/server/dn.rb', line 150

def equal_format?(dn)
  split_dn = LDAP::Server::Operation.split_dn(dn)

  return false if split_dn.length != @dname.length

  @dname.each_with_index do |pair, index|
    return false if pair.keys != split_dn[index].keys
  end
  true
end

#find(attr) ⇒ Object

Returns all values of all occurrences of attr (bottom-up)



39
40
41
42
43
44
45
# File 'lib/ldap/server/dn.rb', line 39

def find(attr)
  result = []
  @dname.each do |pair|
    result << pair[attr.to_s] if pair[attr.to_s]
  end
  result
end

#find_first(attr) ⇒ Object

Returns the value of the first occurrence of attr (bottom-up)



23
24
25
26
27
28
# File 'lib/ldap/server/dn.rb', line 23

def find_first(attr)
  @dname.each do |pair|
    return pair[attr.to_s] if pair[attr.to_s]
  end
  nil
end

#find_last(attr) ⇒ Object

Returns the value of the last occurrence of attr (bottom-up)



31
32
33
34
35
36
# File 'lib/ldap/server/dn.rb', line 31

def find_last(attr)
  @dname.reverse_each do |pair|
    return pair[attr.to_s] if pair[attr.to_s]
  end
  nil
end

#find_nth(attr, n) ⇒ Object

Returns the value of the n-th occurrence of attr (top-down, 0 is first element)



48
49
50
51
52
53
54
55
56
57
# File 'lib/ldap/server/dn.rb', line 48

def find_nth(attr, n)
  i = 0
  @dname.each do |pair|
    if pair[attr.to_s]
      return pair[attr.to_s] if i == n
      i += 1
    end
  end
  nil
end

#include?(dn) ⇒ Boolean

Whether or not the DN constains a substring equal to dn (values are case sensitive) dn is a string

Returns:

  • (Boolean)


163
164
165
166
167
# File 'lib/ldap/server/dn.rb', line 163

def include?(dn)
  split_dn = LDAP::Server::Operation.split_dn(dn)
  return false if split_dn.length > @dname.length
  LDAP::Server::Operation.join_dn(@dname).include?(LDAP::Server::Operation.join_dn(split_dn))
end

#include_format?(dn) ⇒ Boolean

Whether or not the DN constains a substring format equal to dn (values are ignored) (case insensitive) dn is a string

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ldap/server/dn.rb', line 171

def include_format?(dn)
  split_dn = LDAP::Server::Operation.split_dn(dn)

  return false if split_dn.length > @dname.length

  haystack = []
  @dname.each { |pair| haystack << pair.keys }

  needle = []
  split_dn.each { |pair| needle << pair.keys }

  haystack.join.include?(needle.join)
end

#parse(template_dn) ⇒ Object

Generates a mapping for variables For example: > dn = LDAP::Server.DN.new(“uid=user,ou=Users,dc=mydomain,dc=com”) > dn.parse(“uid=:uid, ou=:category, dc=mydomain, dc=com”)

> { :uid => “user”, :category => “Users” }



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ldap/server/dn.rb', line 190

def parse(template_dn)
  result = {}
  template = LDAP::Server::Operation.split_dn(template_dn)
  template.reverse.zip(@dname.reverse).each do |temp, const|
    break if const and temp.keys.first != const.keys.first
    if temp.values.first.start_with?(':')
      sym = temp.values.first[1..-1].to_sym
      if const
        result[sym] = const.values.first unless result[sym]
      else
        result[sym] = nil
      end
    elsif temp.values.first != const.values.first
      break
    end
  end
  result
end

#reverse_each(&block) ⇒ Object



213
214
215
# File 'lib/ldap/server/dn.rb', line 213

def reverse_each(&block)
  @dname.reverse_each(&block)
end

#start_with?(dn) ⇒ Boolean

Whether or not the DN starts with dn (bottom-up) dn is a string

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ldap/server/dn.rb', line 61

def start_with?(dn)
  needle = LDAP::Server::Operation.split_dn(dn)

  # Needle is longer than haystack
  return false if needle.length > @dname.length

  needle_index = 0
  haystack_index = 0

  while needle_index < needle.length
    return false if @dname[haystack_index] != needle[needle_index]
    needle_index += 1
    haystack_index += 1
  end
  true
end

#start_with_format?(dn) ⇒ Boolean

Whether or not the DN starts with a format (bottom-up) (values are ignored) dn is a string

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ldap/server/dn.rb', line 80

def start_with_format?(dn)
  needle = LDAP::Server::Operation.split_dn(dn)

  # Needle is longer than haystack
  return false if needle.length > @dname.length

  needle_index = 0
  haystack_index = 0

  while needle_index < needle.length
    return false if @dname[haystack_index].keys != needle[needle_index].keys
    needle_index += 1
    haystack_index += 1
  end
  true
end