LdapServer: fix handing new superior DN in rename_object() and add delete_old parameter

This commit is contained in:
Benjamin Renard 2020-12-16 10:07:37 +01:00 committed by root
parent 4135d00dd3
commit 933efd4086

View file

@ -205,13 +205,38 @@ class LdapServer(object):
msg.append('%s - %s %s: %s' % (prefix, op, attr, val))
return '\n'.join(msg)
def rename_object(self,dn,new_rdn):
def rename_object(self, dn, new_rdn, new_sup=None, delete_old=True):
# If new_rdn is a complete DN, split new RDN and new superior DN
if len(new_rdn.split(',')) > 1:
self.logger.debug(
"LdapServer - Rename with a full new DN detected (%s): split new RDN and new superior DN",
new_rdn
)
assert new_sup is None, "You can't provide a complete DN as new_rdn and also provide new_sup parameter"
new_dn_parts = new_rdn.split(',')
new_sup = ','.join(new_dn_parts[1:])
new_rdn = new_dn_parts[0]
try:
self.logger.debug("LdapServer - Rename %s in %s" % (dn,new_rdn))
self.con.rename_s(dn,new_rdn)
self.logger.debug(
"LdapServer - Rename %s in %s (new superior: %s, delete old: %s)",
dn,
new_rdn,
"same" if new_sup is None else new_sup,
delete_old
)
self.con.rename_s(dn, new_rdn, newsuperior=new_sup, delold=delete_old)
return True
except ldap.LDAPError, e:
self._error("LdapServer - Error renaming %s in %s : %s" % (dn,new_rdn,e), logging.ERROR)
self._error(
"LdapServer - Error renaming %s in %s (new superior: %s, delete old: %s): %s" % (
dn,
new_rdn,
"same" if new_sup is None else new_sup,
delete_old,
e
),
logging.ERROR
)
return False