]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Completing lib/pp_admintools/app/duplicate_attribs.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 10 May 2023 13:54:14 +0000 (15:54 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 10 May 2023 13:54:14 +0000 (15:54 +0200)
lib/pp_admintools/app/duplicate_attribs.py

index ffad4bdec65d6e81a59432246a5d15643cc6f6e3..7469cedfa7d394440c0e7dfe89a26e6e0171408b 100644 (file)
@@ -13,6 +13,10 @@ import copy
 import re
 import sys
 
+from numbers import Number
+
+from functools import cmp_to_key
+
 # Third party modules
 from fb_tools.xlate import format_list
 
@@ -24,7 +28,7 @@ from ..xlate import XLATOR
 from .ldap import LdapAppError, FatalLDAPError
 from .ldap import BaseLdapApplication
 
-__version__ = '0.2.0'
+__version__ = '0.3.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -51,6 +55,8 @@ class EvalDuplicateAttribsApplication(BaseLdapApplication):
         self.uniq_attributes = copy.copy(self.default_uniq_attributes)
         self.dependend_uniq_attribs = copy.copy(self.default_dependend_uniq_attribs)
         self.result = {}
+        self.instance = None
+        self.connect_info = None
 
         desc = _(
             "Evaluating all LDAP entries, which are using duplicate attributes, which "
@@ -92,8 +98,7 @@ class EvalDuplicateAttribsApplication(BaseLdapApplication):
     # -------------------------------------------------------------------------
     def _verify_instances(self):
 
-        super(EvalDuplicateAttribsApplication, self)._verify_instances(
-            is_admin=False, readonly=True)
+        super(EvalDuplicateAttribsApplication, self)._verify_instances()
 
     # -------------------------------------------------------------------------
     def post_init(self):
@@ -135,6 +140,9 @@ class EvalDuplicateAttribsApplication(BaseLdapApplication):
                 self.exit(1)
             self.dependend_uniq_attribs = dep_attribs
 
+        self.instance = self.ldap_instances[0]
+        self.connect_info = self.cfg.ldap_connection[self.instance]
+
     # -------------------------------------------------------------------------
     def _run(self):
 
@@ -150,6 +158,8 @@ class EvalDuplicateAttribsApplication(BaseLdapApplication):
                     msg2, 'cyan'))
             print(msg_c)
             self.line(len(msg_oc))
+            self.empty_line()
+            self.get_duplicates(attrib)
 
         for attrib in self.dependend_uniq_attribs.keys():
             flter = self.dependend_uniq_attribs[attrib]
@@ -163,8 +173,52 @@ class EvalDuplicateAttribsApplication(BaseLdapApplication):
             print(msg_c)
             self.line(len(msg_oc))
             print(_('Used filter:') + ' ' + flter)
+            self.empty_line()
+            self.get_duplicates(attrib, flter)
+
+        # self.empty_line()
+
+    # -------------------------------------------------------------------------
+    def get_duplicates(self, attrib, flter=None):
+        """Get all duplicates of the given attribute."""
+        used_filter = ''
+        if flter:
+            used_filter = '(&({attrib}=*)({flter}))'.format(attrib=attrib, flter=flter)
+        else:
+            used_filter = '({attrib}=*)'.format(attrib=attrib)
+
+        attributes = [attrib]
+
+        res = {}
+
+        result = self.get_all_entries(
+            self.instance, ldap_filter=used_filter, attributes=attributes)
+
+        for dn in sorted(result.keys(), key=cmp_to_key(self.compare_ldap_dns)):
+            for attr in result[dn].keys():
+                if attr.lower() == attrib.lower():
+                    for value in result[dn][attr]:
+                        if not isinstance(value, Number):
+                            value = str(value).lower()
+                        if value not in res:
+                            res[value] = []
+                        res[value].append(dn)
+
+        duplicates = {}
+        for value in res.keys():
+            if len(res[value]) > 1:
+                duplicates[value] = res[value]
+
+        if duplicates:
+            for value in duplicates.keys():
+                print('{} :'.format(value))
+                for dn in duplicates[value]:
+                    print(' * {}'.format(dn))
+                self.empty_line()
+        else:
+            print(_("No duplicates for attribute {!r} found.").format(attrib))
+            self.empty_line()
 
-        self.empty_line()
 
 # =============================================================================
 if __name__ == "__main__":