class LdapQuery::Filter

Used to create return LDAP query stings depending on what attribute you want to filter

Public Class Methods

auth(username) click to toggle source

Filter used to authenticate a validate CN & Person entry

@param username [String]

# File lib/ldap_query/filter.rb, line 52
def self.auth(username)
  Net::LDAP::Filter.join(cn(username), object_class)
end
clean_str(str, wildcard: false) click to toggle source

If you want to wildcard it this turns all spaces into '*' and adds '*' at the beginning and end of the str as well

@param str [String] the query str @attr wildcard [Boolean] used to determine if the filter should be wildcard match @return [String] either the original str, or a value prepared to wildcard when hitting ldap

# File lib/ldap_query/filter.rb, line 61
def self.clean_str(str, wildcard: false)
  str = str&.strip
  return str unless wildcard && str.is_a?(String)

  str = str.split(/\s/).compact.join('*').squeeze('*')
  "*#{str}*"
end
object_class(str = 'person', wildcard: false) click to toggle source

Generally most ldap queries are again person, sometimes other types will be used for service accounts

@param str [String] @attr wildcard [Boolean] used to determine if the filter should be wildcard match

# File lib/ldap_query/filter.rb, line 26
def self.object_class(str = 'person', wildcard: false)
  Net::LDAP::Filter.eq('objectClass', clean_str(str, wildcard: wildcard))
end
other(attr, val, wildcard: false) click to toggle source

Used to filter LDAP accounts against a custom attribute and valuess

@param attr [String] a custom attribute to query again @param val [String] a user specified value to filter against @attr wildcard [Boolean] used to determine if the filter should be wildcard match

# File lib/ldap_query/filter.rb, line 35
def self.other(attr, val, wildcard: false)
  Net::LDAP::Filter.eq(attr, clean_str(val, wildcard: wildcard))
end
person(username, wildcard: false) click to toggle source

Filter user based on CN attribute (CN is a required attribute)

@param username [String] @attr wildcard [Boolean] used to determine if the filter should be wildcard match

# File lib/ldap_query/filter.rb, line 43
def self.person(username, wildcard: false)
  cn_filter = LdapQuery::Filter.cn(username, wildcard: wildcard)
  user_filter = LdapQuery::Filter.object_class
  Net::LDAP::Filter.join(cn_filter, user_filter)
end