ldap.LdapServer: add format_modify_modlist() and factorize format_changes() & update_need()

This commit is contained in:
Benjamin Renard 2023-05-03 11:14:18 +02:00
parent 5693cf8f8a
commit ebd73812bc
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -281,14 +281,10 @@ class LdapServer:
)
return False
@staticmethod
def update_need(old, new, ignore_attrs=None, encode=False):
@classmethod
def update_need(cls, old, new, ignore_attrs=None, encode=False):
"""Check if an update is need on a LDAP object based on its old and new attributes values"""
ldif = modlist.modifyModlist(
encode_ldap_value(old) if encode else old,
encode_ldap_value(new) if encode else new,
ignore_attr_types=ignore_attrs if ignore_attrs else [],
)
ldif = cls.get_changes(old, new, ignore_attrs=ignore_attrs, encode=encode)
if not ldif:
return False
return True
@ -302,19 +298,23 @@ class LdapServer:
ignore_attr_types=ignore_attrs if ignore_attrs else [],
)
@staticmethod
def format_changes(old, new, ignore_attrs=None, prefix=None, encode=False):
@classmethod
def format_changes(cls, old, new, ignore_attrs=None, prefix=None, encode=False):
"""
Format changes (modlist) on an object based on its old and new attributes values to
display/log it
"""
return cls.format_modify_modlist(
cls.get_changes(old, new, ignore_attrs=ignore_attrs, encode=encode),
prefix=prefix,
)
@staticmethod
def format_modify_modlist(ldif, prefix=None):
"""Format modify modlist to display/log it"""
msg = []
prefix = prefix if prefix else ""
for op, attr, val in modlist.modifyModlist(
encode_ldap_value(old) if encode else old,
encode_ldap_value(new) if encode else new,
ignore_attr_types=ignore_attrs if ignore_attrs else [],
):
for op, attr, val in ldif:
if op == ldap.MOD_ADD: # pylint: disable=no-member
op = "ADD"
elif op == ldap.MOD_DELETE: # pylint: disable=no-member