Class: LdapQuery::Query
- Inherits:
-
Object
- Object
- LdapQuery::Query
- Defined in:
- lib/ldap_query/query.rb
Overview
Used to build LDAP filters and query the host based on the configuration passed
Constant Summary collapse
- REQUIRED_QUERY_ATTRS =
%i[attr val].freeze
- FILTER_METHODS =
%i[cn displayname memberof object_class mail samaccountname person].freeze
Class Method Summary collapse
-
.attach_filter(query, wildcard: false) ⇒ Object
Used to associate and LDAP filter to the connection based on the attr and value supplied.
- .ensure_limit_set(limit = 20) ⇒ Object
-
.ldap_connection(credentials) ⇒ Net::LDAP
Establish an ldap connection with the supplied credentials.
-
.perform(credentials, attr: nil, val: nil, limit: 20, wildcard: false) ⇒ Object
Establish LDAP connection, apply filters, and return results.
-
.sort_by_displayname(entries = []) ⇒ Hash
Sorters.
Class Method Details
.attach_filter(query, wildcard: false) ⇒ Object
Used to associate and LDAP filter to the connection based on the attr and value supplied
32 33 34 35 36 37 38 39 |
# File 'lib/ldap_query/query.rb', line 32 def self.attach_filter(query, wildcard: false) if FILTER_METHODS.include?(query[:attr].to_sym) # Add the filter for the specific LdapQuery::Filter.public_send(query[:attr], query[:val], wildcard: wildcard) else LdapQuery::Filter.other(query[:attr], query[:val], wildcard: wildcard) end end |
.ensure_limit_set(limit = 20) ⇒ Object
65 66 67 68 69 |
# File 'lib/ldap_query/query.rb', line 65 def self.ensure_limit_set(limit = 20) return limit if limit.is_a?(Integer) && limit.positive? 20 end |
.ldap_connection(credentials) ⇒ Net::LDAP
Establish an ldap connection with the supplied credentials
45 46 47 |
# File 'lib/ldap_query/query.rb', line 45 def self.ldap_connection(credentials) LdapQuery::Connection.new(credentials).link end |
.perform(credentials, attr: nil, val: nil, limit: 20, wildcard: false) ⇒ Object
Establish LDAP connection, apply filters, and return results
18 19 20 21 22 23 24 25 26 |
# File 'lib/ldap_query/query.rb', line 18 def self.perform(credentials, attr: nil, val: nil, limit: 20, wildcard: false) raise(AttributeError, 'a valid attribute name and value are required in order to make an ldap query.') if attr.nil? || val.nil? config = LdapQuery::Config.new(credentials) filter = attach_filter({ attr: attr, val: val }, wildcard: wildcard) ldap = ldap_connection(config.hash) entries = ldap.search(filter: filter, size: ensure_limit_set(limit)) entries.nil? ? EMPTY_ARRAY : sort_by_displayname(entries) end |
.sort_by_displayname(entries = []) ⇒ Hash
Sorters
Sort results by their displayanmes
55 56 57 58 59 60 61 62 63 |
# File 'lib/ldap_query/query.rb', line 55 def self.sort_by_displayname(entries = []) return EMPTY_ARRAY if entries.blank? # the begin/rescue is in place because some service accounts are missing the displayname and causes issues when sorting # => if they are missing this attribute they should be sorted last ie: the 'zzzzzzzzzzzz' value entries.sort_by do |entry| entry.respond_to?(:displayname) ? entry&.displayname.first.downcase : 'zzzzzzzzzzz' end end |