import re
import sys
+from numbers import Number
+
+from functools import cmp_to_key
+
# Third party modules
from fb_tools.xlate import format_list
from .ldap import LdapAppError, FatalLDAPError
from .ldap import BaseLdapApplication
-__version__ = '0.2.0'
+__version__ = '0.3.0'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
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 "
# -------------------------------------------------------------------------
def _verify_instances(self):
- super(EvalDuplicateAttribsApplication, self)._verify_instances(
- is_admin=False, readonly=True)
+ super(EvalDuplicateAttribsApplication, self)._verify_instances()
# -------------------------------------------------------------------------
def post_init(self):
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):
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]
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__":