Skip to content
Snippets Groups Projects
Commit 57824bc1 authored by Hague Matthew UXAC009's avatar Hague Matthew UXAC009
Browse files

add(ldap): speed up student search when exact

parent 2d16efc5
Branches
No related tags found
No related merge requests found
......@@ -62,12 +62,25 @@ ATTRS = [
'cn', 'department', 'description', 'memberOf', 'mail',
'extensionAttribute3'
]
search_disjuncts = [
f"({field}=*{term}*)"
for field, term in product(ATTRS, search_terms)
]
# detect exact student searches for faster LDAP lookup
# first full email, then student ID
one_arg = (len(search_terms) == 1)
if one_arg and re.match(r"^[\w.-]+@(live.)?rhul.ac.uk", search_terms[0]):
print("Searching by exact email..")
ldap_filter = f"(mail={search_terms[0]})"
elif one_arg and re.match(r"\d{9}", search_terms[0]):
print("Searching by exact student ID..")
ldap_filter = f"(extensionAttribute3={search_terms[0]})"
else:
search_disjuncts = [
f"({field}=*{term}*)"
for field, term in product(ATTRS, search_terms)
]
ldap_filter = "(|" + "".join(search_disjuncts) + ")"
# added late because can't be used for a search term when building ldap_filter
ATTRS.append("whenCreated")
ldap_filter = "(|" + "".join(search_disjuncts) + ")"
password = subprocess.check_output(PASS_CMD, stderr=subprocess.DEVNULL) \
.decode("ascii") \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment