Skip to content

Commit ddc7297

Browse files
committed
fix: hotfix LDAP Search
1 parent 27e134f commit ddc7297

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

app/ldap_protocol/filter_interpreter.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
User,
3232
)
3333
from ldap_protocol.utils.helpers import ft_to_dt
34+
from ldap_protocol.utils.queries import get_path_filter, get_search_path
3435
from repo.pg.tables import groups_table, queryable_attr as qa, users_table
3536

3637
from .asn1parser import ASN1Row, TagNumbers
@@ -398,6 +399,14 @@ def _cast_item(self, item: ASN1Row) -> UnaryExpression | ColumnElement: # noqa:
398399

399400
is_substring = item.tag_id == TagNumbers.SUBSTRING
400401

402+
if attr == "distinguishedname" and not is_substring:
403+
try:
404+
dn_search_path = get_search_path(right.value)
405+
except Exception: # noqa: S110
406+
pass
407+
else:
408+
return get_path_filter(dn_search_path)
409+
401410
if attr == "anr":
402411
if is_substring:
403412
expr = right.value[0]

app/ldap_protocol/ldap_requests/search.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ def is_sid_requested(self) -> bool:
153153
def is_guid_requested(self) -> bool:
154154
return self.all_attrs or "objectguid" in self.requested_attrs
155155

156+
@property
157+
def is_objectclass_requested(self) -> bool:
158+
return self.all_attrs or "objectclass" in self.requested_attrs
159+
156160
@cached_property
157161
def all_attrs(self) -> bool:
158162
return "*" in self.requested_attrs or not self.requested_attrs
@@ -417,11 +421,16 @@ def _mutate_query_with_attributes_to_load(
417421
if attr not in _ATTRS_TO_CLEAN
418422
}
419423

424+
cond = or_(
425+
func.lower(Attribute.name).in_(attrs),
426+
func.lower(Attribute.name) == "objectclass",
427+
)
428+
420429
return query.options(
421430
selectinload(qa(Directory.attributes)),
422431
with_loader_criteria(
423432
Attribute,
424-
func.lower(Attribute.name).in_(attrs),
433+
cond,
425434
),
426435
)
427436

@@ -534,7 +543,7 @@ async def _fill_attrs(
534543
attrs: dict[str, list[str]],
535544
session: AsyncSession,
536545
) -> None:
537-
if "distinguishedname" not in self.requested_attrs or self.all_attrs:
546+
if "distinguishedname" in self.requested_attrs or self.all_attrs:
538547
attrs["distinguishedName"].append(distinguished_name)
539548

540549
if "whenCreated" in self.requested_attrs or self.all_attrs:
@@ -572,10 +581,6 @@ async def _fill_attrs(
572581
attrs["memberOf"].append(group.directory.path_dn)
573582

574583
if self.token_groups and "user" in obj_classes:
575-
attrs["tokenGroups"].append(
576-
str(string_to_sid(directory.object_sid)),
577-
)
578-
579584
group_directories = await get_all_parent_group_directories(
580585
directory.groups,
581586
session,
@@ -584,7 +589,7 @@ async def _fill_attrs(
584589
if group_directories is not None:
585590
async for directory_ in group_directories:
586591
attrs["tokenGroups"].append(
587-
str(string_to_sid(directory_.object_sid)),
592+
string_to_sid(directory_.object_sid), # type: ignore
588593
)
589594

590595
if self.member and "group" in obj_classes and directory.group:
@@ -638,6 +643,9 @@ async def tree_view( # noqa: C901
638643

639644
if attr.name.lower() == "objectclass":
640645
obj_classes.append(value)
646+
if self.is_objectclass_requested:
647+
attrs[attr.name].append(value)
648+
continue
641649

642650
attrs[attr.name].append(value)
643651

0 commit comments

Comments
 (0)