]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Start rewriting specialized search methods to use the general search() method.
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 31 Jan 2024 07:44:03 +0000 (08:44 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 31 Jan 2024 07:44:03 +0000 (08:44 +0100)
lib/pp_admintools/app/ldap.py

index 9ddbb49db6b9742b002e31fca65eb6d40d144990..32f4acd232f0ea74e419036956a9d1ba747f1abd 100644 (file)
@@ -60,7 +60,7 @@ from ..errors import DpxLdapSessionError
 from ..errors import DpxWriteLdapItemError
 from ..xlate import XLATOR, format_list
 
-__version__ = '1.3.0'
+__version__ = '1.3.1'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -1076,31 +1076,22 @@ class BaseLdapApplication(BaseDPXApplication):
         if self.verbose > 1:
             LOG.debug(_('Using LDAP filter: {!r}').format(ldap_filter))
 
-        if inst not in self.ldap_connection:
-            self.connect_instance(inst)
-        ldap = self.ldap_connection[inst]
-
-        try:
-            req_status, req_result, req_response, req_whatever = ldap.search(
-                search_base=base_dn, search_scope=SUBTREE, search_filter=ldap_filter,
-                get_operational_attributes=False, attributes=attributes,
-                time_limit=self.cfg.ldap_timeout)
-        finally:
-            if not self.single_session:
-                self.disconnect_instance(inst)
+        (search_status, search_result, search_response) = self.search(
+            inst, search_base=base_dn, ldap_filter=ldap_filter, attributes=attributes,
+            scope=scope)
 
-        if req_status:
+        if search_status:
             if self.verbose > 2:
                 msg = ngettext(
                     'Found one entry with filter {fltr!r} in {uri}/{bdn}.',
                     'Found {nr} entries with filter {fltr!r} in {uri}/{bdn}.',
-                    len(req_response)).format(
-                    nr=len(req_response), uri=connect_info.url, bdn=base_dn, fltr=ldap_filter)
+                    len(search_response)).format(
+                    nr=len(search_response), uri=connect_info.url, bdn=base_dn, fltr=ldap_filter)
                 LOG.debug(msg)
             if self.verbose > 5:
                 msg = _('Result of searching for DNs of all entries:')
-                LOG.debug(msg + '\n' + pp(req_result))
-            for entry in req_response:
+                LOG.debug(msg + '\n' + pp(search_result))
+            for entry in search_response:
                 if self.verbose > 4:
                     LOG.debug(_('Got a response entry:') + ' ' + pp(entry))
                 result.append(entry['dn'])
@@ -1121,10 +1112,11 @@ class BaseLdapApplication(BaseDPXApplication):
         return result
 
     # -------------------------------------------------------------------------
-    def get_all_entry_dns_hash(self, inst, ldap_filter=None):
+    def get_all_entry_dns_hash(self, inst, ldap_filter=None, base_dn=None):
         """Get Object classes and DNs of all entries in the given LDAP instance."""
         connect_info = self.cfg.ldap_connection[inst]
-        base_dn = connect_info.base_dn
+        if not base_dn:
+            base_dn = connect_info.base_dn
 
         result = CIDict()
         attributes = ['objectClass']
@@ -1137,24 +1129,14 @@ class BaseLdapApplication(BaseDPXApplication):
         if self.verbose > 1:
             LOG.debug(_('Using LDAP filter: {!r}').format(ldap_filter))
 
-        if inst not in self.ldap_connection:
-            self.connect_instance(inst)
-        ldap = self.ldap_connection[inst]
-
-        try:
-            req_status, req_result, req_response, req_whatever = ldap.search(
-                search_base=base_dn, search_scope=SUBTREE, search_filter=ldap_filter,
-                get_operational_attributes=False, attributes=attributes,
-                time_limit=self.cfg.ldap_timeout)
-        finally:
-            if not self.single_session:
-                self.disconnect_instance(inst)
+        (search_status, search_result, search_response) = self.search(
+            inst, search_base=base_dn, ldap_filter=ldap_filter, attributes=attributes)
 
-        if req_status:
+        if search_status:
             if self.verbose > 5:
                 msg = _('Result of searching for DNs of all entries:')
-                LOG.debug(msg + '\n' + pp(req_result))
-            for entry in req_response:
+                LOG.debug(msg + '\n' + pp(search_result))
+            for entry in search_response:
                 if self.verbose > 4:
                     LOG.debug(_('Got a response entry:') + ' ' + pp(entry))
 
@@ -1168,7 +1150,7 @@ class BaseLdapApplication(BaseDPXApplication):
                 }
 
         else:
-            LOG.warn('Got no entry DNs.')
+            LOG.warn(_('Got no entry DNs.'))
 
         return result