Generate members list (#354)
#301 https://csclub.uwaterloo.ca/~a3thakra/csc/j285he-ldap/about/members/ Co-authored-by: Jared He <66887902+jaredjhe@users.noreply.github.com> Co-authored-by: Jared He <j285he@uwaterloo.ca> Reviewed-on: #354 Reviewed-by: n3parikh <n3parikh@csclub.uwaterloo.ca> Co-authored-by: j285he <j285he@localhost> Co-committed-by: j285he <j285he@localhost>pull/366/head
parent
85e5080012
commit
e6315cf906
@ -0,0 +1,61 @@ |
||||
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), |
||||
}; |
||||
}) |
||||
.sort((item1: Member, item2: Member) => item1.id.localeCompare(item2.id)); |
||||
} 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", |
||||
}, |
||||
]; |
@ -0,0 +1,6 @@ |
||||
.table { |
||||
display: table; |
||||
width: 100%; |
||||
margin-top: calc(20rem / 16); |
||||
margin-bottom: calc(60rem / 16); |
||||
} |
@ -0,0 +1,65 @@ |
||||
import { GetStaticProps } from "next"; |
||||
import React from "react"; |
||||
|
||||
import { Link } from "@/components/Link"; |
||||
import { Table } from "@/components/Table"; |
||||
import { getCurrentTerm } from "@/lib/events"; |
||||
import { getMembers, Member } from "@/lib/members"; |
||||
import { capitalize } from "@/utils"; |
||||
|
||||
import styles from "./members.module.css"; |
||||
|
||||
interface Props { |
||||
members: Member[]; |
||||
year: string; |
||||
term: string; |
||||
} |
||||
|
||||
export default function Members(props: Props) { |
||||
return ( |
||||
<> |
||||
<h1>Members</h1> |
||||
<p> |
||||
{`The members for ${capitalize(props.term)} ${ |
||||
props.year |
||||
} are listed here. We currently |
||||
have ${props.members.length} members. Use of this list for solicitation |
||||
of any form is prohibited, if you wish to get in touch with the |
||||
membership as a whole please contact the Executive.`}
|
||||
</p> |
||||
<Table className={styles.table}> |
||||
<thead> |
||||
<tr> |
||||
<th>Name / Webpage</th> |
||||
<th>Program</th> |
||||
<th>Userid</th> |
||||
</tr> |
||||
</thead> |
||||
<tbody> |
||||
{props.members.map((member) => ( |
||||
<tr key={member.id}> |
||||
<td> |
||||
<Link href={`https://csclub.uwaterloo.ca/~${member.id}/`}> |
||||
{member.name} |
||||
</Link> |
||||
</td> |
||||
<td>{member.program}</td> |
||||
<td>{member.id}</td> |
||||
</tr> |
||||
))} |
||||
</tbody> |
||||
</Table> |
||||
</> |
||||
); |
||||
} |
||||
|
||||
export const getStaticProps: GetStaticProps<Props> = async () => { |
||||
const curTerm = getCurrentTerm(); |
||||
return { |
||||
props: { |
||||
year: curTerm.year, |
||||
term: curTerm.term, |
||||
members: await getMembers(curTerm.year, curTerm.term), |
||||
}, |
||||
}; |
||||
}; |
Loading…
Reference in new issue