Skip to content

Commit

Permalink
Merge pull request #240 from MultiDirectoryLab/fix_modife_dn
Browse files Browse the repository at this point in the history
fix modify_dn
  • Loading branch information
Mastermind-U authored May 28, 2024
2 parents ba70c86 + 67a7941 commit a1bb8fa
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions app/ldap_protocol/ldap_requests/modify_dn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from typing import AsyncGenerator, ClassVar

from sqlalchemy import update
from sqlalchemy import func, update, text
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy.orm import selectinload
Expand Down Expand Up @@ -106,14 +106,14 @@ async def handle(self, ldap_session: Session, session: AsyncSession) ->\
return

dn_is_base = self.new_superior.lower() == base_dn.lower()
dn, name = self.newrdn.split('=')
if dn_is_base:
new_directory = Directory(
object_class=directory.object_class,
name=self.newrdn.split('=')[1],
name=name,
depth=1,
)
new_path = new_directory.create_path(
dn=new_directory.get_dn_prefix())
new_path = new_directory.create_path(dn=dn)
else:
new_base_directory = await session.scalar(new_sup_query)
if not new_base_directory:
Expand All @@ -122,11 +122,11 @@ async def handle(self, ldap_session: Session, session: AsyncSession) ->\

new_directory = Directory(
object_class=directory.object_class,
name=self.newrdn.split('=')[1],
name=name,
parent=new_base_directory,
depth=len(new_base_directory.path.path)+1,
)
new_path = new_directory.create_path(new_base_directory)
new_path = new_directory.create_path(new_base_directory, dn=dn)

async with session.begin_nested():
session.add_all([new_directory, new_path])
Expand All @@ -138,13 +138,6 @@ async def handle(self, ldap_session: Session, session: AsyncSession) ->\
.where(Directory.parent == directory)
.values(parent_id=new_directory.id))

q = update(Path)\
.values({Path.path[directory.depth]: self.newrdn})\
.where(Path.directories.any(id=directory.id))

await session.execute(
q, execution_options={"synchronize_session": 'fetch'})

await session.commit()

async with session.begin_nested():
Expand All @@ -155,13 +148,21 @@ async def handle(self, ldap_session: Session, session: AsyncSession) ->\
.values(directory_id=new_directory.id))

async with session.begin_nested():
# TODO: replace text with slice
await session.execute(
update(Path)
.where(Path.path[new_directory.depth] == directory.get_dn(
dn=directory.get_dn_prefix()))
.where(
get_path_filter(
directory.path.path,
column=Path.path[1:directory.depth],
),
)
.values(
{Path.path[directory.depth]: new_directory.get_dn(
dn=new_directory.get_dn_prefix())},
path=func.array_cat(
new_directory.path.path,
text("path[:depth :]").bindparams(
depth=directory.depth+1),
),
),
execution_options={"synchronize_session": 'fetch'},
)
Expand Down

0 comments on commit a1bb8fa

Please sign in to comment.