def rename(python_data: LdapObject, new_base_dn: str = None, database: Optional[Database] = None, **kwargs) -> LdapObject: """ Move/rename a LdapObject in the database. """ table = type(python_data) dn = python_data.get_as_single('dn') assert dn is not None database = get_database(database) connection = database.connection # extract key and value from kwargs if len(kwargs) == 1: name, value = list(kwargs.items())[0] # work out the new rdn of the object split_new_rdn = [[(name, value, 1)]] field = _get_field_by_name(table, name) assert field.db_field python_data = python_data.merge({ name: value, }) elif len(kwargs) == 0: split_new_rdn = [str2dn(dn)[0]] else: assert False new_rdn = dn2str(split_new_rdn) connection.rename( dn, new_rdn, new_base_dn, ) if new_base_dn is not None: split_base_dn = str2dn(new_base_dn) else: split_base_dn = str2dn(dn)[1:] tmp_list = [split_new_rdn[0]] tmp_list.extend(split_base_dn) new_dn = dn2str(tmp_list) python_data = python_data.merge({ 'dn': new_dn, }) return python_data
def rdn_to_dn(changes: Changeset, name: str, base_dn: str) -> Changeset: """ Convert the rdn to a fully qualified DN for the specified LDAP connection. :param changes: The changes object to lookup. :param name: rdn to convert. :param base_dn: The base_dn to lookup. :return: fully qualified DN. """ dn = changes.get_value_as_single('dn') if dn is not None: return changes value = changes.get_value_as_single(name) if value is None: raise tldap.exceptions.ValidationError( "Cannot use %s in dn as it is None" % name) if isinstance(value, list): raise tldap.exceptions.ValidationError( "Cannot use %s in dn as it is a list" % name) assert base_dn is not None split_base = str2dn(base_dn) split_new_dn = [[(name, value, 1)]] + split_base new_dn = dn2str(split_new_dn) return changes.set('dn', new_dn)
def create_base(self, datastore, create_dn): base_dn = dn2str(str2dn(create_dn)[1:]) try: organizationalUnit.objects.using( using=datastore._using, settings=datastore._settings) \ .base_dn(base_dn) \ .get(dn=create_dn) except organizationalUnit.DoesNotExist: print("Creating %s" % create_dn) organizationalUnit.objects.using( using=datastore._using, settings=datastore._settings) \ .base_dn(base_dn) \ .create(dn=create_dn)
def _eq(dn1, dn2): dn1 = dn2str(str2dn(dn1)).lower() dn2 = dn2str(str2dn(dn2)).lower() return dn1 == dn2