Repository navigation
Ban people from hackathons by email #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { z } from "zod"; | ||
| import { desc, eq, inArray } from "drizzle-orm"; | ||
| import { hackathonBans, users } from "@query/db"; | ||
| import type { DrizzleDB } from "@query/db"; | ||
| import { createTRPCRouter } from "../../trpc"; | ||
| import { hackathonBanCacheKey, isAdmin } from "../../middleware/procedures"; | ||
| import { recordAdminAction } from "../../middleware/audit"; | ||
|
|
||
| const normalize = (email: string) => email.trim().toLowerCase(); | ||
|
|
||
| /** | ||
| * Hackathon bans. Staff can bar someone from taking part in hackathons | ||
| * (registering, the interest list, teams, submitting, judging) without | ||
| * touching their club membership. Enforcement is notHackathonBanned in | ||
| * middleware/procedures.ts; this router only manages the list. Bug testers | ||
| * can read it and nothing else, like every isAdmin mutation. | ||
| */ | ||
| export const hackathonBansRouter = createTRPCRouter({ | ||
| listBans: isAdmin.query(async ({ ctx }) => { | ||
| const db = ctx.db as DrizzleDB; | ||
| const bans = await db.query.hackathonBans.findMany({ | ||
| orderBy: [desc(hackathonBans.createdAt)], | ||
| }); | ||
|
|
||
| // Names for the people involved, in one read rather than one per row. | ||
| const emails = bans.map((b) => b.email); | ||
| const staffIds = bans | ||
| .map((b) => b.bannedBy) | ||
| .filter((id): id is string => !!id); | ||
| const [accounts, staff] = await Promise.all([ | ||
| emails.length | ||
| ? db.query.users.findMany({ | ||
| where: inArray(users.email, emails), | ||
| columns: { email: true, name: true }, | ||
| }) | ||
| : [], | ||
| staffIds.length | ||
| ? db.query.users.findMany({ | ||
| where: inArray(users.id, staffIds), | ||
| columns: { id: true, name: true, email: true }, | ||
| }) | ||
| : [], | ||
| ]); | ||
|
|
||
| return bans.map((ban) => { | ||
| const account = accounts.find((a) => normalize(a.email) === ban.email); | ||
| const by = staff.find((s) => s.id === ban.bannedBy); | ||
| return { | ||
| id: ban.id, | ||
| email: ban.email, | ||
| name: account?.name ?? null, | ||
| hasAccount: !!account, | ||
| reason: ban.reason, | ||
| bannedBy: by?.name ?? by?.email ?? null, | ||
| createdAt: ban.createdAt, | ||
| }; | ||
| }); | ||
| }), | ||
|
|
||
| banFromHackathons: isAdmin | ||
| .input( | ||
| z.object({ | ||
| email: z.string().trim().email().max(320), | ||
| reason: z.string().trim().min(3).max(500), | ||
| }), | ||
| ) | ||
| .mutation(async ({ ctx, input }) => { | ||
| const db = ctx.db as DrizzleDB; | ||
| const email = normalize(input.email); | ||
|
|
||
| // Re-banning updates the reason instead of failing on the unique email. | ||
| const [ban] = await db | ||
| .insert(hackathonBans) | ||
| .values({ email, reason: input.reason, bannedBy: ctx.userId }) | ||
| .onConflictDoUpdate({ | ||
| target: hackathonBans.email, | ||
| set: { reason: input.reason, bannedBy: ctx.userId }, | ||
| }) | ||
| .returning(); | ||
|
|
||
| ctx.cache.delete(hackathonBanCacheKey(email)); | ||
|
|
||
| await recordAdminAction(db, { | ||
| userId: ctx.userId, | ||
| action: "hackathon.ban", | ||
| resourceId: email, | ||
| severity: "warn", | ||
| metadata: { reason: input.reason }, | ||
| }); | ||
|
|
||
| return ban; | ||
| }), | ||
|
|
||
| liftHackathonBan: isAdmin | ||
| .input(z.object({ email: z.string().trim().email().max(320) })) | ||
| .mutation(async ({ ctx, input }) => { | ||
| const db = ctx.db as DrizzleDB; | ||
| const email = normalize(input.email); | ||
|
|
||
| const removed = await db | ||
| .delete(hackathonBans) | ||
| .where(eq(hackathonBans.email, email)) | ||
| .returning({ id: hackathonBans.id }); | ||
|
|
||
| ctx.cache.delete(hackathonBanCacheKey(email)); | ||
|
|
||
| if (removed.length) { | ||
| await recordAdminAction(db, { | ||
| userId: ctx.userId, | ||
| action: "hackathon.unban", | ||
| resourceId: email, | ||
| }); | ||
| } | ||
|
|
||
| return { lifted: removed.length > 0 }; | ||
| }), | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ban list misses mixed-case accounts
Low Severity
listBansmatches accounts with a case-sensitiveinArrayonusers.emailagainst already-lowercased ban emails. OAuth still stores the provider's original casing, so those people show as having no account and lose their name even though the ban itself is in force.Additional Locations (1)
packages/api/src/routers/hackathon/bans.ts#L44-L46Reviewed by Cursor Bugbot for commit 5a67e1e. Configure here.