From cbc5af2b5b7902ebcad1153d86bbe2df2b5dd26b Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Fri, 5 May 2017 10:48:40 +0200 Subject: [PATCH] Working on issue #981 - Improved performance to search only needed arguments of a LDAP entry. --- lib/import/ldap/user_factory.rb | 8 +++++++- lib/ldap.rb | 4 +++- lib/ldap/group.rb | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/import/ldap/user_factory.rb b/lib/import/ldap/user_factory.rb index edd1b8a5c..2cdc8cf1c 100644 --- a/lib/import/ldap/user_factory.rb +++ b/lib/import/ldap/user_factory.rb @@ -24,7 +24,13 @@ module Import import_job = kargs[:import_job] import_job_count = 0 - @ldap.search(config[:user_filter]) do |entry| + + # limit the fetched attributes for an entry to only + # those which are needed to improve the performance + relevant_attributes = config[:user_attributes].keys + relevant_attributes.push('dn') + + @ldap.search(config[:user_filter], attributes: relevant_attributes) do |entry| backend_instance = create_instance(entry, config, user_roles, signup_role_ids, kargs) post_import_hook(entry, backend_instance, config, user_roles, signup_role_ids, kargs) diff --git a/lib/ldap.rb b/lib/ldap.rb index f96398c08..461cd025c 100644 --- a/lib/ldap.rb +++ b/lib/ldap.rb @@ -54,6 +54,7 @@ class Ldap # @param filter [String] The filter that should get applied to the search. # @param base [String] The base DN on which the search should get executed. Default is initialization parameter. # @param scope [Net::LDAP::SearchScope] The search scope as defined in Net::LDAP SearchScopes. Default is WholeSubtree. + # @param attributes [Array] Limits the requested entry attributes to the given list of attributes which increses the performance. # # @example # ldap.search('(objectClass=group)') do |entry| @@ -62,7 +63,7 @@ class Ldap # #=> # # @return [true] Returns always true - def search(filter, base: nil, scope: nil) + def search(filter, base: nil, scope: nil, attributes: nil) base ||= base_dn() scope ||= Net::LDAP::SearchScope_WholeSubtree @@ -71,6 +72,7 @@ class Ldap base: base, filter: filter, scope: scope, + attributes: attributes, return_result: false, # improves performance ) do |entry| # needed for the #entries? method -> returns nil on break diff --git a/lib/ldap/group.rb b/lib/ldap/group.rb index d7c5f2bf7..eb3a10cc7 100644 --- a/lib/ldap/group.rb +++ b/lib/ldap/group.rb @@ -58,7 +58,7 @@ class Ldap return {} if filter.blank? groups = {} - @ldap.search(filter, base: base_dn) { |entry| + @ldap.search(filter, base: base_dn, attributes: %w(dn)) { |entry| groups[entry.dn.downcase] = entry.dn.downcase } groups @@ -80,7 +80,7 @@ class Ldap filter ||= filter() result = {} - @ldap.search(filter) do |entry| + @ldap.search(filter, attributes: %w(dn member)) do |entry| members = entry[:member] next if members.blank?