]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding method get_all_entries() to lib/pp_admintools/app/ldap.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 10 May 2023 13:53:09 +0000 (15:53 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 10 May 2023 13:53:09 +0000 (15:53 +0200)
lib/pp_admintools/app/ldap.py

index d19ef19278442b05447299bbf99fbf392731fe15..23dd5f8de9cab903fc4edbd2ed067fbb93a84305 100644 (file)
@@ -54,7 +54,7 @@ from ..config.ldap import LdapConnectionInfo, LdapConfiguration
 # rom ..config.ldap import DEFAULT_PORT_LDAP, DEFAULT_PORT_LDAPS
 from ..config.ldap import DEFAULT_TIMEOUT
 
-__version__ = '0.11.1'
+__version__ = '0.11.2'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -578,6 +578,9 @@ class BaseLdapApplication(BaseDPXApplication):
                     continue
             filtered_instances.append(inst.lower())
 
+        if self.verbose > 2:
+            LOG.debug(_("Filtered instances:") + ' ' + pp(filtered_instances))
+
         self._validate_given_instances(filtered_instances)
 
         if self.verbose > 1:
@@ -770,6 +773,64 @@ class BaseLdapApplication(BaseDPXApplication):
                 LOG.debug(_("Disconnecting from LDAP server {!r} ...").format(connect_info.url))
             del self.ldap_server[inst]
 
+    # -------------------------------------------------------------------------
+    def get_all_entries(self, inst, base_dn=None, ldap_filter=None, attributes=None):
+        """Get all LDAP entries bellow the given BaseDN and the given LDAP filter.
+        If no attributes are given, all attributes are given back.
+        The result is a hash with the DNs if the resulting entries as keys, and a hash
+        with the resulting attributes as values.
+        """
+        connect_info = self.cfg.ldap_connection[inst]
+        ldap = self.ldap_connection[inst]
+
+        result = {}
+
+        if not base_dn:
+            base_dn = connect_info.base_dn
+        if attributes is None:
+            attributes = [ALL_ATTRIBUTES]
+        if ldap_filter is None:
+            ldap_filter = '(objectClass=*)'
+
+        if self.verbose > 2:
+            msg = _(
+                "Searching in {uri}/{bdn} for all entries with filter {fltr!r}, "
+                "giving attributes:").format(uri=connect_info.url, bdn=base_dn, fltr=ldap_filter)
+            msg += ' ' + format_list(attributes, do_repr=True)
+            LOG.debug(msg)
+
+        req_status, req_result, req_response, req_whatever = ldap.search(
+            search_base=base_dn, search_scope=SUBTREE, attributes=attributes,
+            search_filter=ldap_filter, time_limit=self.cfg.ldap_timeout)
+
+        if req_status:
+            if self.verbose > 4:
+                LOG.debug(_("Result of searching:") + '\n' + pp(req_result))
+
+            for entry in req_response:
+                dn = entry['dn']
+                if self.verbose > 3:
+                    LOG.debug(_("Found entry {!r}.").format(dn))
+                result[dn] = self.normalized_attributes(entry)
+
+            if self.verbose > 2:
+                msg = ngettext(
+                    "Found one entry with filter {fltr!r} in {uri}/{bdn}.",
+                    "Found {nr} enries with filter {fltr!r} in {uri}/{bdn}.",
+                    len(result)).format(nr=len(result), uri=connect_info.url,
+                    bdn=base_dn, fltr=ldap_filter)
+                LOG.debug(msg)
+            if self.verbose > 4:
+                LOG.debug(_("Got response entries:") + '\n' + pp(result))
+
+        else:
+            if self.verbose > 3:
+                msg = _("No entry found with filter {fltr!r} in {uri}/{bdn}.").format(
+                    uri=connect_info.url, bdn=base_dn, fltr=ldap_filter)
+                LOG.debug(msg)
+
+        return result
+
     # -------------------------------------------------------------------------
     def get_all_entry_dns(self, inst, ldap_filter=None):
         """Get DNs of all entries in the given LDAP instance and sort them."""
@@ -779,6 +840,7 @@ class BaseLdapApplication(BaseDPXApplication):
         ldap = self.ldap_connection[inst]
 
         result = []
+
         attributes = ['dn']
         if not ldap_filter:
             ldap_filter = '(objectClass=*)'