2021-11-22 12:47:00 -05:00
|
|
|
import { Client } from "ldapts";
|
|
|
|
|
|
|
|
export interface Member {
|
|
|
|
name: string;
|
|
|
|
id: string;
|
|
|
|
program: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getMembers(
|
|
|
|
year: string,
|
|
|
|
term: string
|
|
|
|
): Promise<Member[]> {
|
|
|
|
if (term !== "winter" && term !== "spring" && term !== "fall") {
|
|
|
|
throw new Error(`[getMembers] Not a valid term: "${term}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.USE_LDAP?.toLowerCase() !== "true") {
|
|
|
|
return dummyMembers;
|
|
|
|
}
|
|
|
|
|
|
|
|
let members: Member[] = [];
|
|
|
|
|
|
|
|
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.slice(0, 1)}${year}))`,
|
|
|
|
});
|
|
|
|
|
|
|
|
members = searchEntries
|
|
|
|
.map((item) => {
|
|
|
|
return {
|
|
|
|
name: item.cn as string,
|
|
|
|
id: item.uid as string,
|
|
|
|
program: item.program === undefined ? "" : (item.program as string),
|
|
|
|
};
|
|
|
|
})
|
2022-01-17 00:15:39 -05:00
|
|
|
.sort((item1: Member, item2: Member) =>
|
|
|
|
item1.name.localeCompare(item2.name)
|
|
|
|
);
|
2021-11-22 12:47:00 -05:00
|
|
|
} finally {
|
|
|
|
await client.unbind();
|
|
|
|
}
|
|
|
|
|
|
|
|
return members;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dummyMembers: Member[] = [
|
|
|
|
{
|
|
|
|
name: "John Smith",
|
|
|
|
id: "j12smith",
|
|
|
|
program: "MAT/Mathematics Computer Science",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Jane Smith",
|
|
|
|
id: "j34smith",
|
|
|
|
program: "MAT/Mathematics Computer Science",
|
|
|
|
},
|
|
|
|
];
|