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