Class: LdapQuery::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/ldap_query/filter.rb

Overview

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

Class Method Summary collapse

Class Method Details

.auth(username) ⇒ Object

Filter used to authenticate a validate CN & Person entry

Parameters:

  • username (String)


52
53
54
# 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) ⇒ String

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

Parameters:

  • str (String)

    the query str

Returns:

  • (String)

    either the original str, or a value prepared to wildcard when hitting ldap



61
62
63
64
65
66
67
# 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) ⇒ Object

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

Parameters:

  • str (String) (defaults to: 'person')


26
27
28
# 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) ⇒ Object

Used to filter LDAP accounts against a custom attribute and valuess

Parameters:

  • attr (String)

    a custom attribute to query again

  • val (String)

    a user specified value to filter against



35
36
37
# 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) ⇒ Object

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

Parameters:

  • username (String)


43
44
45
46
47
# 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