LdapServer.update_object(): add relax parameter

This commit is contained in:
Benjamin Renard 2021-03-24 18:16:23 +01:00
parent 45a0b99687
commit aa2e1ee99f

View file

@ -8,6 +8,7 @@ import dateutil.parser
import dateutil.tz
import ldap
from ldap.controls import SimplePagedResultsControl
from ldap.controls.simple import RelaxRulesControl
import ldap.modlist as modlist
import pytz
@ -91,6 +92,7 @@ class LdapServer(object): # pylint: disable=useless-object-inheritance
return result[dn] if dn in result else None
def paged_search(self, basedn, filterstr, attrs, scope='sub', pagesize=500):
assert not self.v2, "Paged search is not available on LDAP version 2"
# Initialize SimplePagedResultsControl object
page_control = SimplePagedResultsControl(
True,
@ -170,7 +172,8 @@ class LdapServer(object): # pylint: disable=useless-object-inheritance
return False
def update_object(self, dn, old, new, ignore_attrs=None):
def update_object(self, dn, old, new, ignore_attrs=None, relax=False):
assert not relax or not self.v2, "Relax modification is not available on LDAP version 2"
ldif = modlist.modifyModlist(
old, new,
ignore_attr_types=ignore_attrs if ignore_attrs else []
@ -178,6 +181,9 @@ class LdapServer(object): # pylint: disable=useless-object-inheritance
if ldif == []:
return True
try:
if relax:
self.con.modify_ext_s(dn, ldif, serverctrls=[RelaxRulesControl()])
else:
self.con.modify_s(dn, ldif)
return True
except ldap.LDAPError as e: # pylint: disable=no-member