From 38eeb41cb1a4ce4d9440b8a441ecab7a253ee811 Mon Sep 17 00:00:00 2001
From: Julian Rother <julian@jrother.eu>
Date: Wed, 8 Dec 2021 04:10:03 +0100
Subject: [PATCH] Search constraint extraction for single-valued OR

EntryTemplate.extract_search_constraints now returns constraints for
"(|(FILTER))" as if it were "(FILTER)".
---
 ldapserver/entries.py | 2 ++
 tests/test_entries.py | 1 +
 2 files changed, 3 insertions(+)

diff --git a/ldapserver/entries.py b/ldapserver/entries.py
index 9bd34b7..06b0727 100644
--- a/ldapserver/entries.py
+++ b/ldapserver/entries.py
@@ -493,6 +493,8 @@ class EntryTemplate(AttributeDict):
 				for name, values in self.__extract_filter_constraints(subfilter).items():
 					result[name] += values
 			return result
+		if isinstance(filter_obj, ldap.FilterOr) and len(filter_obj.filters) == 1:
+			return self.__extract_filter_constraints(filter_obj.filters[0])
 		return AttributeDict(self.schema)
 
 	def match_search(self, base_obj, scope, filter_obj):
diff --git a/tests/test_entries.py b/tests/test_entries.py
index 1947dc6..ff44c31 100644
--- a/tests/test_entries.py
+++ b/tests/test_entries.py
@@ -743,6 +743,7 @@ class TestEntryTemplate(unittest.TestCase):
 		template = EntryTemplate(schema, 'dc=example,dc=com', 'cn', objectclass=['top'], cn=WILDCARD, uid=['foobar'])
 		self.assertEqual(dict(template.extract_search_constraints('dc=exapmle,dc=com', ldap.SearchScope.wholeSubtree, ldap.FilterEqual('cn', b'foo')).items()), {'cn': ['foo']})
 		self.assertEqual(dict(template.extract_search_constraints('dc=exapmle,dc=com', ldap.SearchScope.wholeSubtree, ldap.FilterAnd([ldap.FilterEqual('objectclass', b'top'), ldap.FilterEqual('cn', b'foo')])).items()), {'cn': ['foo'], 'objectClass': ['top']})
+		self.assertEqual(dict(template.extract_search_constraints('dc=exapmle,dc=com', ldap.SearchScope.wholeSubtree, ldap.FilterOr([ldap.FilterEqual('cn', b'foo')])).items()), {'cn': ['foo']})
 		self.assertEqual(dict(template.extract_search_constraints('cn=foo,dc=example,dc=com', ldap.SearchScope.baseObject, ldap.FilterPresent('objectClass')).items()), {'cn': ['foo']})
 		self.assertEqual(dict(template.extract_search_constraints('dc=example,dc=com', ldap.SearchScope.baseObject, ldap.FilterPresent('objectClass')).items()), {})
 
-- 
GitLab