Skip to content

Commit 61f2bcc

Browse files
committed
Add EqualFold function on AttributeTypeAndValue.
Since v3 ldap we have case insensitive search in parallel to a case sensitive search: I would like to propose to add an EqualFold function in parallel to Equal function in dn.go on AttributeTypeAndValue. So we can not only search in case sensitive but also compare RDNs.
1 parent c9551dc commit 61f2bcc

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

dn.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,50 @@ func (r *RelativeDN) hasAllAttributes(attrs []*AttributeTypeAndValue) bool {
205205
func (a *AttributeTypeAndValue) Equal(other *AttributeTypeAndValue) bool {
206206
return strings.EqualFold(a.Type, other.Type) && a.Value == other.Value
207207
}
208+
209+
// AncestorOfFold returns true if the other DN consists of at least one RDN followed by all the RDNs of the current DN.
210+
// Case of the attribute type and value is not significant
211+
func (d *DN) AncestorOfFold(other *DN) bool {
212+
if len(d.RDNs) >= len(other.RDNs) {
213+
return false
214+
}
215+
// Take the last `len(d.RDNs)` RDNs from the other DN to compare against
216+
otherRDNs := other.RDNs[len(other.RDNs)-len(d.RDNs):]
217+
for i := range d.RDNs {
218+
if !d.RDNs[i].EqualFold(otherRDNs[i]) {
219+
return false
220+
}
221+
}
222+
return true
223+
}
224+
225+
// Equal returns true if the RelativeDNs are equal as defined by rfc4517 4.2.15 (distinguishedNameMatch).
226+
// Case of the attribute type is not significant
227+
func (r *RelativeDN) EqualFold(other *RelativeDN) bool {
228+
if len(r.Attributes) != len(other.Attributes) {
229+
return false
230+
}
231+
return r.hasAllAttributesFold(other.Attributes) && other.hasAllAttributesFold(r.Attributes)
232+
}
233+
234+
func (r *RelativeDN) hasAllAttributesFold(attrs []*AttributeTypeAndValue) bool {
235+
for _, attr := range attrs {
236+
found := false
237+
for _, myattr := range r.Attributes {
238+
if myattr.EqualFold(attr) {
239+
found = true
240+
break
241+
}
242+
}
243+
if !found {
244+
return false
245+
}
246+
}
247+
return true
248+
}
249+
250+
// EqualFold returns true if the AttributeTypeAndValue is equivalent to the specified AttributeTypeAndValue
251+
// Case of the attribute type and value is not significant
252+
func (a *AttributeTypeAndValue) EqualFold(other *AttributeTypeAndValue) bool {
253+
return strings.EqualFold(a.Type, other.Type) && strings.EqualFold(a.Value, other.Value)
254+
}

0 commit comments

Comments
 (0)