Skip to content

Commit

Permalink
PR Review
Browse files Browse the repository at this point in the history
  • Loading branch information
boomermath committed Mar 11, 2025
1 parent 87bcb9c commit c7f223f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions apps/web/app/(sidebar)/scout/actions/submitForm.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"use server";

import { StandFormData, standFormSchema } from "../data/schema";
import { _getTeamsInMatch, TeamInMatch } from "./teamsInMatch";
import { StandFormSubmissionErrors } from "./utils";
import { StandFormData, standFormSchema } from "../data/schema";

import {
createServerAction,
ServerActionError,
ServerActionErrorWithCustomData
} from "@/lib/actions/actions-utils";
import { auth } from "@/lib/auth";
import { AuthErrors, isSessionAuthorized } from "@/lib/auth/utils";
import { AuthErrors, checkSession } from "@/lib/auth/utils";
import { db } from "@/lib/database";
import {
match,
Expand Down Expand Up @@ -103,15 +103,15 @@ async function insertStandForm(
async function _submitStandForm(data: StandFormData) {
const session = await auth();

if (!session) throw new ServerActionError(AuthErrors.UNAUTHORIZED);

const { success, data: validatedData } = standFormSchema.safeParse(data);

if (!success) {
throw new ServerActionError("Invalid form data");
}

if (!session || !isSessionAuthorized(UserRole.USER, session)) {
throw new ServerActionError(AuthErrors.UNAUTHORIZED);
}
await checkSession(UserRole.USER, session);

const teamMatches = await _getTeamsInMatch(data.match_detail.match_number);

Expand Down
6 changes: 3 additions & 3 deletions apps/web/lib/auth/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { auth } from "./auth";

import { UserRole } from "@/lib/database/schema";
import { redirect } from "next/navigation";
import { UserRole, userRoleOrdering } from "@/lib/database/schema";
import { Session } from "next-auth";
import { DiscordProfile } from "next-auth/providers/discord";
import { redirect } from "next/navigation";

const YETI_GUILD_ID = "408711970305474560";
const AVALANCHE_GUILD_ID = "1241008226598649886";
Expand Down Expand Up @@ -81,7 +81,7 @@ export function redirectError(error: AuthErrors) {
}

function roleIndex(userRole: UserRole) {
return Object.values(UserRole).indexOf(userRole);
return userRoleOrdering.indexOf(userRole);
}

export function isSessionAuthorized(requiredRole: UserRole, session: Session) {
Expand Down
6 changes: 3 additions & 3 deletions apps/web/lib/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import {
} from "drizzle-orm/pg-core";
import type { AdapterAccountType } from "next-auth/adapters";


// the order of this enum is important and is used
// to check permissions for a user to access resources
export enum UserRole {
ADMIN = "admin",
USER = "user",
GUEST = "guest",
BANISHED = "banished",
}

// ordering of user roles from most to least permissive
export const userRoleOrdering: UserRole[] = [UserRole.ADMIN, UserRole.USER, UserRole.GUEST, UserRole.BANISHED];

export const userRoleEnum = pgEnum("user_role", enumToPgEnum(UserRole));

export const users = pgTable("user", {
Expand Down

0 comments on commit c7f223f

Please sign in to comment.