www-new/lib/ldap.ts

49 lines
1.1 KiB
TypeScript

import { Client } from "ldapts";
interface MemberItem {
name: string;
id: string;
program: string;
}
// note: the term parameter is different due to the way the LDAP database is structured
export async function getMembers(
year: string,
term: "w" | "s" | "f"
): Promise<MemberItem[]> {
let members: MemberItem[] = [];
const url = "ldap://ldap1.csclub.uwaterloo.ca";
const searchDN = "ou=People,dc=csclub,dc=uwaterloo,dc=ca";
const client = new Client({
url,
});
try {
await client.bind("", "");
const { searchEntries } = await client.search(searchDN, {
scope: "sub",
filter: `(&(objectClass=member)(term=${term}${year}))`,
});
members = searchEntries
.map((item) => {
return {
name: item.cn as string,
id: item.uid as string,
program: item.program === undefined ? "" : (item.program as string),
};
})
.sort((item1: MemberItem, item2: MemberItem) =>
item1.id.localeCompare(item2.id)
);
}
// catch (ex) {
// throw ex;
// }
finally {
await client.unbind();
}
return members;
}