Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

Commit

Permalink
🐛 Fix bug with identifying student id/role
Browse files Browse the repository at this point in the history
  • Loading branch information
danieladugyan committed Nov 21, 2023
1 parent 153e2c8 commit c55c7e5
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions backend/services/core/src/datasources/Access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ export function isRole(role: string): boolean {
}

export function isStudentId(studentId: string): boolean {
const oldStudentIdRegex = /^[a-z]{3}\d{2}[a-z]{3}$/;
const studentIdRegex = /^[a-z]{2}\d{4}[a-z]{2}(-s)?$/;

return studentIdRegex.test(studentId) || oldStudentIdRegex.test(studentId);
const regexes = [
/^[a-z]{2}\d{4}[a-z]{2}(-s)?$/, // ab1234cd-s
/^[a-z]{3}\d{2}[a-z]{3}$/, // dat12abc
/^dic\d{2}[a-z]{2}$/, // dic12ab
/^d\d{2}[a-z]{2}$/, // d12ab
];

return regexes.some((regex) => regex.test(studentId));
}

type Policy = sql.CreateDoorAccessPolicy | sql.CreateApiAccessPolicy;

export function withWho<T extends Policy>(create: T, who: string): T {
const policy = { ...create };
if (isRole(who)) {
policy.role = who;
} else if (isStudentId(who)) {
if (isStudentId(who)) {
policy.student_id = who;
} else if (isRole(who)) {
policy.role = who;
} else {
throw new UserInputError('field "who" does not match the format of a role or student id.');
}
Expand Down

0 comments on commit c55c7e5

Please sign in to comment.