🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
refactor(api): replace godMode checks with admin role verification
Bill Yang471c316db027
diff --git a/server/src/api/admin/getAdminSites.ts b/server/src/api/admin/getAdminSites.tsindex e5d9ff83a..d3cd5fd13 100644--- a/server/src/api/admin/getAdminSites.ts+++ b/server/src/api/admin/getAdminSites.ts@@ -3,8 +3,8 @@ import { FastifyReply, FastifyRequest } from "fastify";import { DateTime } from "luxon";import clickhouse from "../../db/clickhouse/clickhouse.js";import { db } from "../../db/postgres/postgres.js";-import { member, user, sites } from "../../db/postgres/schema.js";-import { getSessionFromReq } from "../../lib/auth-utils.js";+import { member, user } from "../../db/postgres/schema.js";+import { getIsUserAdmin } from "../../lib/auth-utils.js";// Define event count result typeinterface EventCountResult {@@ -16,15 +16,9 @@ export async function getAdminSites(request: FastifyRequest,reply: FastifyReply) {- const session = await getSessionFromReq(request);+ const isAdmin = await getIsUserAdmin(request);- const userRecord = await db- .select({ godMode: user.godMode })- .from(user)- .where(eq(user.id, session?.user.id ?? ""))- .limit(1);-- if (!userRecord || !userRecord[0].godMode) {+ if (!isAdmin) {return reply.status(401).send({ error: "Unauthorized" });}diff --git a/server/src/api/admin/getAdminUsers.ts b/server/src/api/admin/getAdminUsers.tsindex ccf35f9fb..ec9ccfd8b 100644--- a/server/src/api/admin/getAdminUsers.ts+++ b/server/src/api/admin/getAdminUsers.ts@@ -2,21 +2,15 @@ import { eq, inArray } from "drizzle-orm";import { FastifyReply, FastifyRequest } from "fastify";import { db } from "../../db/postgres/postgres.js";import { member, sites, user } from "../../db/postgres/schema.js";-import { getSessionFromReq } from "../../lib/auth-utils.js";+import { getIsUserAdmin } from "../../lib/auth-utils.js";export async function getAdminUsers(request: FastifyRequest,reply: FastifyReply) {- const session = await getSessionFromReq(request);+ const isAdmin = await getIsUserAdmin(request);- const userRecord = await db- .select({ godMode: user.godMode })- .from(user)- .where(eq(user.id, session?.user.id ?? ""))- .limit(1);-- if (!userRecord || !userRecord[0].godMode) {+ if (!isAdmin) {return reply.status(401).send({ error: "Unauthorized" });}diff --git a/server/src/api/sites/getSitesFromOrg.ts b/server/src/api/sites/getSitesFromOrg.tsindex 2afbe09ac..59ed03b3d 100644--- a/server/src/api/sites/getSitesFromOrg.ts+++ b/server/src/api/sites/getSitesFromOrg.ts@@ -3,7 +3,7 @@ import { FastifyRequest, FastifyReply } from "fastify";import clickhouse from "../../db/clickhouse/clickhouse.js";import { db } from "../../db/postgres/postgres.js";import { sites, member, organization } from "../../db/postgres/schema.js";-import { getSessionFromReq, getUserGodMode } from "../../lib/auth-utils.js";+import { getSessionFromReq, getIsUserAdmin } from "../../lib/auth-utils.js";import { IS_CLOUD, DEFAULT_EVENT_LIMIT } from "../../lib/const.js";import { processResults } from "../analytics/utils.js";import { getSubscriptionInner } from "../stripe/getSubscription.js";@@ -25,11 +25,10 @@ export async function getSitesFromOrg(return res.status(401).send({ error: "Unauthorized" });}- // Check if user has god mode- const godMode = await getUserGodMode(req);+ const isAdmin = await getIsUserAdmin(req);- // If not god mode, verify user is a member of the organization- if (!godMode) {+ // If not admin, verify user is a member of the organization+ if (!isAdmin) {const memberCheck = await db.select().from(member)diff --git a/server/src/db/postgres/schema.ts b/server/src/db/postgres/schema.tsindex 4c13d4ddb..5c4aa01d0 100644--- a/server/src/db/postgres/schema.ts+++ b/server/src/db/postgres/schema.ts@@ -30,8 +30,6 @@ export const user = pgTable(stripeCustomerId: text(),overMonthlyLimit: boolean().default(false),monthlyEventCount: integer().default(0),- // can view all sites. for internal use only.- godMode: boolean().default(false),},(table) => [unique("user_username_unique").on(table.username),diff --git a/server/src/lib/auth-utils.ts b/server/src/lib/auth-utils.tsindex 9bae22c14..9883d1347 100644--- a/server/src/lib/auth-utils.ts+++ b/server/src/lib/auth-utils.ts@@ -23,7 +23,7 @@ export async function getSessionFromReq(req: FastifyRequest) {return session;}-export async function getUserGodMode(req: FastifyRequest) {+export async function getIsUserAdmin(req: FastifyRequest) {const session = await getSessionFromReq(req);const userId = session?.user.id;@@ -32,11 +32,11 @@ export async function getUserGodMode(req: FastifyRequest) {}const userRecord = await db- .select({ godMode: user.godMode })+ .select({ role: user.role }).from(user).where(eq(user.id, userId)).limit(1);- return userRecord.length > 0 && userRecord[0].godMode;+ return userRecord.length > 0 && userRecord[0].role === "admin";}const sitesAccessCache = new NodeCache({@@ -76,22 +76,15 @@ export async function getSitesUserHasAccessTo(// Create new promise and cache itconst promise = (async () => {try {- // Fetch user godMode status and member records in parallel- const [userRecord, memberRecords] = await Promise.all([- db- .select({ godMode: user.godMode })- .from(user)- .where(eq(user.id, userId))- .limit(1),+ const [isAdmin, memberRecords] = await Promise.all([+ getIsUserAdmin(req),db.select({ organizationId: member.organizationId, role: member.role }).from(member).where(eq(member.userId, userId)),]);- const hasGodMode = userRecord.length > 0 && userRecord[0].godMode;- // If user has godMode, return all sites- if (hasGodMode) {+ if (isAdmin) {const allSites = await db.select().from(sites);return allSites;}